| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import classnames from 'classnames/dedupe'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Internal dependencies |
| 8 |
*/ |
| 9 |
import FieldLabel from '../../field-label'; |
| 10 |
import FieldDescription from '../../field-description'; |
| 11 |
import FieldOptions from '../../field-options'; |
| 12 |
import { getFieldAttributes, FieldDefaultSettings } from '../../field-attributes'; |
| 13 |
|
| 14 |
/** |
| 15 |
* WordPress dependencies |
| 16 |
*/ |
| 17 |
const { __ } = wp.i18n; |
| 18 |
|
| 19 |
const { applyFilters } = wp.hooks; |
| 20 |
|
| 21 |
const { Component, Fragment } = wp.element; |
| 22 |
|
| 23 |
const { PanelBody, SelectControl, ToggleControl } = wp.components; |
| 24 |
|
| 25 |
const { InspectorControls } = wp.blockEditor; |
| 26 |
|
| 27 |
/** |
| 28 |
* Block Edit Class. |
| 29 |
*/ |
| 30 |
class BlockEdit extends Component { |
| 31 |
render() { |
| 32 |
const { attributes, setAttributes, isSelected } = this.props; |
| 33 |
|
| 34 |
const { multiple } = attributes; |
| 35 |
|
| 36 |
let { options } = attributes; |
| 37 |
|
| 38 |
let { className = '' } = this.props; |
| 39 |
|
| 40 |
className = classnames('ghostkit-form-field ghostkit-form-field-select', className); |
| 41 |
|
| 42 |
className = applyFilters('ghostkit.editor.className', className, this.props); |
| 43 |
|
| 44 |
if (!options || !options.length) { |
| 45 |
options = [ |
| 46 |
{ |
| 47 |
label: '', |
| 48 |
value: '', |
| 49 |
selected: false, |
| 50 |
}, |
| 51 |
]; |
| 52 |
} |
| 53 |
|
| 54 |
let selectVal = multiple ? [] : ''; |
| 55 |
|
| 56 |
options.forEach((data) => { |
| 57 |
if (multiple && data.selected) { |
| 58 |
selectVal.push(data.value); |
| 59 |
} else if (!multiple && !selectVal && data.selected) { |
| 60 |
selectVal = data.value; |
| 61 |
} |
| 62 |
}); |
| 63 |
|
| 64 |
return ( |
| 65 |
<Fragment> |
| 66 |
<InspectorControls> |
| 67 |
<PanelBody> |
| 68 |
<FieldDefaultSettings {...this.props} defaultCustom={' '} placeholderCustom={' '} /> |
| 69 |
<ToggleControl |
| 70 |
label={__('Multiple', 'ghostkit')} |
| 71 |
checked={multiple} |
| 72 |
onChange={() => { |
| 73 |
if (multiple) { |
| 74 |
const newOptions = [...options]; |
| 75 |
let singleSelected = false; |
| 76 |
|
| 77 |
newOptions.forEach((data, i) => { |
| 78 |
if (data.selected) { |
| 79 |
if (singleSelected) { |
| 80 |
newOptions[i].selected = false; |
| 81 |
} |
| 82 |
|
| 83 |
singleSelected = true; |
| 84 |
} |
| 85 |
}); |
| 86 |
|
| 87 |
setAttributes({ |
| 88 |
multiple: !multiple, |
| 89 |
options: newOptions, |
| 90 |
}); |
| 91 |
} else { |
| 92 |
setAttributes({ multiple: !multiple }); |
| 93 |
} |
| 94 |
}} |
| 95 |
/> |
| 96 |
</PanelBody> |
| 97 |
</InspectorControls> |
| 98 |
<div className={className}> |
| 99 |
<FieldLabel {...this.props} /> |
| 100 |
|
| 101 |
{isSelected ? ( |
| 102 |
<FieldOptions |
| 103 |
options={options} |
| 104 |
multiple={multiple} |
| 105 |
onChange={(val) => setAttributes({ options: val })} |
| 106 |
/> |
| 107 |
) : ( |
| 108 |
<SelectControl |
| 109 |
{...getFieldAttributes(attributes)} |
| 110 |
value={selectVal} |
| 111 |
options={(() => { |
| 112 |
if (!multiple) { |
| 113 |
let addNullOption = true; |
| 114 |
|
| 115 |
Object.keys(options).forEach((data) => { |
| 116 |
if (data.selected) { |
| 117 |
addNullOption = false; |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
if (addNullOption) { |
| 122 |
return [ |
| 123 |
{ |
| 124 |
label: __('--- Select ---', 'ghostkit'), |
| 125 |
value: '', |
| 126 |
selected: true, |
| 127 |
}, |
| 128 |
...options, |
| 129 |
]; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return options; |
| 134 |
})()} |
| 135 |
/> |
| 136 |
)} |
| 137 |
|
| 138 |
<FieldDescription {...this.props} /> |
| 139 |
</div> |
| 140 |
</Fragment> |
| 141 |
); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
export default BlockEdit; |
| 146 |
|