| 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, 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 { slug, inline } = attributes; |
| 35 |
|
| 36 |
let { options } = attributes; |
| 37 |
|
| 38 |
let { className = '' } = this.props; |
| 39 |
|
| 40 |
className = classnames( |
| 41 |
'ghostkit-form-field ghostkit-form-field-radio', |
| 42 |
inline ? 'ghostkit-form-field-radio-inline' : '', |
| 43 |
className |
| 44 |
); |
| 45 |
|
| 46 |
className = applyFilters('ghostkit.editor.className', className, this.props); |
| 47 |
|
| 48 |
if (!options || !options.length) { |
| 49 |
options = [ |
| 50 |
{ |
| 51 |
label: '', |
| 52 |
value: '', |
| 53 |
selected: false, |
| 54 |
}, |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
let selectVal = ''; |
| 59 |
|
| 60 |
options.forEach((data) => { |
| 61 |
if (!selectVal && data.selected) { |
| 62 |
selectVal = data.value; |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
return ( |
| 67 |
<Fragment> |
| 68 |
<InspectorControls> |
| 69 |
<PanelBody> |
| 70 |
<FieldDefaultSettings {...this.props} defaultCustom={' '} placeholderCustom={' '} /> |
| 71 |
<ToggleControl |
| 72 |
label={__('Inline', 'ghostkit')} |
| 73 |
checked={inline} |
| 74 |
onChange={() => setAttributes({ inline: !inline })} |
| 75 |
/> |
| 76 |
</PanelBody> |
| 77 |
</InspectorControls> |
| 78 |
<div className={className}> |
| 79 |
<FieldLabel {...this.props} /> |
| 80 |
|
| 81 |
{isSelected ? ( |
| 82 |
<FieldOptions options={options} onChange={(val) => setAttributes({ options: val })} /> |
| 83 |
) : ( |
| 84 |
<div className="ghostkit-form-field-radio-items"> |
| 85 |
{options.map((data, i) => { |
| 86 |
const fieldName = `${slug}-item-${i}`; |
| 87 |
return ( |
| 88 |
<label |
| 89 |
key={fieldName} |
| 90 |
htmlFor={fieldName} |
| 91 |
className="ghostkit-form-field-radio-item" |
| 92 |
> |
| 93 |
<input |
| 94 |
type="radio" |
| 95 |
checked={selectVal === data.value} |
| 96 |
onChange={() => {}} |
| 97 |
{...getFieldAttributes(attributes)} |
| 98 |
/> |
| 99 |
{data.label} |
| 100 |
</label> |
| 101 |
); |
| 102 |
})} |
| 103 |
</div> |
| 104 |
)} |
| 105 |
|
| 106 |
<FieldDescription {...this.props} /> |
| 107 |
</div> |
| 108 |
</Fragment> |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
export default BlockEdit; |
| 114 |
|