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