index.js
101 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { Component } from '@wordpress/element'; |
| 5 | import { BaseControl } from '@wordpress/components'; |
| 6 | import jquery from "jquery"; |
| 7 | |
| 8 | /** |
| 9 | * Render ChosenSelect Control |
| 10 | */ |
| 11 | class ChosenSelect extends Component { |
| 12 | constructor( props ) { |
| 13 | super( props ); |
| 14 | |
| 15 | this.saveSetting = this.saveSetting.bind( this ); |
| 16 | this.saveState = this.saveState.bind( this ); |
| 17 | } |
| 18 | |
| 19 | saveSetting( name, value ) { |
| 20 | this.props.setAttributes( { |
| 21 | [ name ]: value, |
| 22 | } ); |
| 23 | } |
| 24 | |
| 25 | saveState( name, value ) { |
| 26 | this.setState( { |
| 27 | [ name ]: value, |
| 28 | } ); |
| 29 | } |
| 30 | |
| 31 | componentDidMount() { |
| 32 | const { value } = this.props; |
| 33 | |
| 34 | this.$el = jquery( this.el ); |
| 35 | this.$el.val( value ); |
| 36 | |
| 37 | this.$input = this.$el.chosen( { |
| 38 | width: '100%', |
| 39 | } ).data( 'chosen' ); |
| 40 | |
| 41 | this.handleChange = this.handleChange.bind( this ); |
| 42 | |
| 43 | this.$el.on( 'change', this.handleChange ); |
| 44 | } |
| 45 | |
| 46 | componentWillUnmount() { |
| 47 | this.$el.off( 'change', this.handleChange ); |
| 48 | this.$el.chosen( 'destroy' ); |
| 49 | } |
| 50 | |
| 51 | handleChange( e ) { |
| 52 | this.props.onChange( e.target.value ); |
| 53 | } |
| 54 | |
| 55 | componentDidUpdate() { |
| 56 | const $searchField = jquery( '.chosen-base-control' ).closest( '.chosen-container' ).find( '.chosen-search-input' ); |
| 57 | this.$input.search_field.autocomplete( { |
| 58 | source: function( request, response ) { |
| 59 | const data = { |
| 60 | action: 'give_block_donation_form_search_results', |
| 61 | search: request.term, |
| 62 | }; |
| 63 | const chosenBlock = jquery( '.give-block-chosen-select' ); |
| 64 | |
| 65 | jquery.post( ajaxurl, data, ( responseData ) => { |
| 66 | chosenBlock.empty(); |
| 67 | responseData = JSON.parse( responseData ); |
| 68 | |
| 69 | if ( responseData.length > 0 ) { |
| 70 | response( jquery.map( responseData, function( item ) { |
| 71 | chosenBlock.append( `<option value="${item.id}">${item.name}</option>` ); |
| 72 | } ) ); |
| 73 | |
| 74 | chosenBlock.trigger( 'chosen:updated' ); |
| 75 | $searchField.val( request.term ); |
| 76 | } |
| 77 | } ); |
| 78 | }, |
| 79 | } ); |
| 80 | } |
| 81 | |
| 82 | render() { |
| 83 | return ( |
| 84 | <BaseControl className="give-chosen-base-control"> |
| 85 | <select className="give-select give-select-chosen give-block-chosen-select" ref={ el => this.el = el }> |
| 86 | { this.props.options.map( ( option, index ) => |
| 87 | <option |
| 88 | key={ `${ option.label }-${ option.value }-${ index }` } |
| 89 | value={ option.value } |
| 90 | > |
| 91 | { option.label } |
| 92 | </option> |
| 93 | ) } |
| 94 | </select> |
| 95 | </BaseControl> |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | export default ChosenSelect; |
| 101 |