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