index.js
49 lines
| 1 | /** |
| 2 | * Internal dependencies |
| 3 | */ |
| 4 | const {withInstanceId, BaseControl, FormToggle} = wp.components, |
| 5 | {Component} = wp.element; |
| 6 | |
| 7 | class GiveToggleControl extends Component { |
| 8 | constructor(props) { |
| 9 | super( ...props ); |
| 10 | |
| 11 | this.onChange = this.onChange.bind( this ); |
| 12 | } |
| 13 | |
| 14 | onChange( event ) { |
| 15 | if ( this.props.onChange ) { |
| 16 | this.props.onChange(event); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | render() { |
| 21 | const { label, checked, help, instanceId, name } = this.props; |
| 22 | const id = `give-inspector-toggle-control-${ instanceId }`; |
| 23 | |
| 24 | let describedBy; |
| 25 | if ( help ) { |
| 26 | describedBy = id + '__help'; |
| 27 | } |
| 28 | |
| 29 | return ( |
| 30 | <BaseControl |
| 31 | label={ label } |
| 32 | id={ id } |
| 33 | help={ help } |
| 34 | className="blocks-toggle-control" |
| 35 | > |
| 36 | <FormToggle |
| 37 | id={ id } |
| 38 | name={ name } |
| 39 | checked={ checked } |
| 40 | onChange={ this.onChange } |
| 41 | aria-describedby={ describedBy } |
| 42 | /> |
| 43 | </BaseControl> |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | export default withInstanceId( GiveToggleControl ); |
| 49 |