default-meta.js
107 lines
| 1 | const { |
| 2 | Button, |
| 3 | TextControl, |
| 4 | } = wp.components; |
| 5 | |
| 6 | const { __ } = wp.i18n; |
| 7 | |
| 8 | class JetDefaultMetaControl extends wp.element.Component { |
| 9 | |
| 10 | constructor( props ) { |
| 11 | super( props ); |
| 12 | |
| 13 | this.addNewOption = this.addNewOption.bind( this ); |
| 14 | } |
| 15 | |
| 16 | getDefaultMeta() { |
| 17 | if ( ! this.props.defaultMeta ) { |
| 18 | return []; |
| 19 | } |
| 20 | return Array.from( this.props.defaultMeta ); |
| 21 | } |
| 22 | |
| 23 | |
| 24 | addNewOption() { |
| 25 | const items = this.getDefaultMeta(); |
| 26 | |
| 27 | items.push( { |
| 28 | key: '', |
| 29 | value: '', |
| 30 | } ); |
| 31 | |
| 32 | this.props.onChange( items ); |
| 33 | } |
| 34 | |
| 35 | removeOption( index ) { |
| 36 | const items = this.getDefaultMeta(); |
| 37 | items.splice( index, 1 ); |
| 38 | |
| 39 | this.props.onChange( items ); |
| 40 | } |
| 41 | |
| 42 | onChangeValue( { value, name, id } ) { |
| 43 | |
| 44 | const items = Array.from( this.props.defaultMeta ); |
| 45 | items[ id ][ name ] = value; |
| 46 | |
| 47 | this.props.onChange( items ); |
| 48 | } |
| 49 | |
| 50 | /* eslint-disable jsx-a11y/no-onchange */ |
| 51 | render() { |
| 52 | |
| 53 | return <div |
| 54 | className="jet-user-fields-map__list" |
| 55 | > |
| 56 | { this.getDefaultMeta().map( ( currentItem, index ) => { |
| 57 | return <div |
| 58 | className="jet-user-meta__row" |
| 59 | key={ 'jet-form-builder-repeater-item-' + index } |
| 60 | > |
| 61 | <div className='repeater-item-column jet-margin-bottom-wrapper'> |
| 62 | <TextControl |
| 63 | key='meta_key' |
| 64 | label={ __( 'Meta Key', 'jet-form-builder' ) } |
| 65 | value={ currentItem.key } |
| 66 | onChange={ ( newValue ) => { |
| 67 | this.onChangeValue( { |
| 68 | value: newValue, |
| 69 | name: 'key', |
| 70 | id: index |
| 71 | } ); |
| 72 | } } |
| 73 | /> |
| 74 | <TextControl |
| 75 | key='meta_value' |
| 76 | label={ __( 'Meta Value', 'jet-form-builder' ) } |
| 77 | value={ currentItem.value } |
| 78 | onChange={ ( newValue ) => { |
| 79 | this.onChangeValue( { |
| 80 | value: newValue, |
| 81 | name: 'value', |
| 82 | id: index |
| 83 | } ); |
| 84 | } } |
| 85 | /> |
| 86 | </div> |
| 87 | <div className='repeater-item-column'> |
| 88 | <Button |
| 89 | icon="dismiss" |
| 90 | label="Remove" |
| 91 | onClick={ () => this.removeOption( index ) } |
| 92 | /> |
| 93 | </div> |
| 94 | </div> |
| 95 | } ) } |
| 96 | <Button |
| 97 | className='button-add-option' |
| 98 | isSecondary |
| 99 | onClick={ this.addNewOption } |
| 100 | > |
| 101 | { __( 'Add New Option' ) } |
| 102 | </Button> |
| 103 | </div>; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | export default JetDefaultMetaControl; |