index.js
56 lines
| 1 | /** |
| 2 | * Internal dependencies |
| 3 | */ |
| 4 | const {BaseControl} = wp.components, |
| 5 | {withInstanceId} = wp.compose, |
| 6 | {Component} = wp.element, |
| 7 | {isEmpty} = _; |
| 8 | |
| 9 | class GiveSelectControl extends Component{ |
| 10 | constructor(props){ |
| 11 | super(props); |
| 12 | |
| 13 | this.onChangeHandler = this.onChangeHandler.bind( this ); |
| 14 | } |
| 15 | |
| 16 | onChangeHandler(event){ |
| 17 | if ( this.props.onChange ) { |
| 18 | this.props.onChange(event); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | |
| 23 | // Disable reason: A select with an onchange throws a warning |
| 24 | |
| 25 | render(){ |
| 26 | const {label, help, instanceId, options = [], name, ...props } = this.props; |
| 27 | const id = `give-inspector-select-control-${ instanceId }`; |
| 28 | |
| 29 | /* eslint-disable jsx-a11y/no-onchange */ |
| 30 | return ! isEmpty( options ) && ( |
| 31 | <BaseControl label={ label } id={ id } help={ help }> |
| 32 | <select |
| 33 | id={ id } |
| 34 | name={ name } |
| 35 | className="blocks-select-control__input" |
| 36 | onChange={ this.onChangeHandler } |
| 37 | aria-describedby={ !! help ? id + '__help' : undefined } |
| 38 | {...props} |
| 39 | > |
| 40 | { options.map( ( option ) => |
| 41 | <option |
| 42 | key={ option.value } |
| 43 | value={ option.value } |
| 44 | > |
| 45 | { option.label } |
| 46 | </option> |
| 47 | ) } |
| 48 | </select> |
| 49 | </BaseControl> |
| 50 | ); |
| 51 | /* eslint-enable jsx-a11y/no-onchange */ |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | export default withInstanceId( GiveSelectControl ); |
| 56 |