| 1 |
import { Button, TextControl } from '@wordpress/components'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Field Options Class. |
| 6 |
* |
| 7 |
* @param props |
| 8 |
*/ |
| 9 |
export default function FieldOptions(props) { |
| 10 |
const { options, onChange, multiple } = props; |
| 11 |
|
| 12 |
return ( |
| 13 |
<div className="ghostkit-field-options"> |
| 14 |
{options.map((data, i) => { |
| 15 |
const fieldName = `option-${i}`; |
| 16 |
return ( |
| 17 |
<div |
| 18 |
className="ghostkit-field-options-item" |
| 19 |
key={fieldName} |
| 20 |
> |
| 21 |
<input |
| 22 |
type={multiple ? 'checkbox' : 'radio'} |
| 23 |
checked={data.selected} |
| 24 |
onClick={() => { |
| 25 |
const newOpts = [...options]; |
| 26 |
|
| 27 |
newOpts[i].selected = !data.selected; |
| 28 |
|
| 29 |
if (!multiple) { |
| 30 |
newOpts.forEach((newData, k) => { |
| 31 |
if (i !== k) { |
| 32 |
newOpts[k].selected = false; |
| 33 |
} |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
onChange(newOpts); |
| 38 |
}} |
| 39 |
/> |
| 40 |
<TextControl |
| 41 |
placeholder={__('Write label…', 'ghostkit')} |
| 42 |
value={data.value} |
| 43 |
onChange={(newVal) => { |
| 44 |
const newOpts = [...options]; |
| 45 |
|
| 46 |
newOpts[i].value = newVal; |
| 47 |
newOpts[i].label = newVal; |
| 48 |
|
| 49 |
onChange(newOpts); |
| 50 |
}} |
| 51 |
/> |
| 52 |
<Button |
| 53 |
onClick={() => { |
| 54 |
if (options.length > 1) { |
| 55 |
const newOpts = [...options]; |
| 56 |
newOpts.splice(i, 1); |
| 57 |
|
| 58 |
onChange(newOpts); |
| 59 |
} |
| 60 |
}} |
| 61 |
className="components-icon-button" |
| 62 |
> |
| 63 |
<svg |
| 64 |
aria-hidden="true" |
| 65 |
role="img" |
| 66 |
focusable="false" |
| 67 |
className="dashicon dashicons-trash" |
| 68 |
xmlns="http://www.w3.org/2000/svg" |
| 69 |
width="20" |
| 70 |
height="20" |
| 71 |
viewBox="0 0 20 20" |
| 72 |
> |
| 73 |
<path d="M12 4h3c.6 0 1 .4 1 1v1H3V5c0-.6.5-1 1-1h3c.2-1.1 1.3-2 2.5-2s2.3.9 2.5 2zM8 4h3c-.2-.6-.9-1-1.5-1S8.2 3.4 8 4zM4 7h11l-.9 10.1c0 .5-.5.9-1 .9H5.9c-.5 0-.9-.4-1-.9L4 7z" /> |
| 74 |
</svg> |
| 75 |
</Button> |
| 76 |
</div> |
| 77 |
); |
| 78 |
})} |
| 79 |
<Button |
| 80 |
onClick={() => { |
| 81 |
onChange([ |
| 82 |
...options, |
| 83 |
{ |
| 84 |
value: '', |
| 85 |
label: '', |
| 86 |
selected: false, |
| 87 |
}, |
| 88 |
]); |
| 89 |
}} |
| 90 |
className="components-icon-button" |
| 91 |
> |
| 92 |
<svg |
| 93 |
xmlns="http://www.w3.org/2000/svg" |
| 94 |
viewBox="0 0 24 24" |
| 95 |
width="24" |
| 96 |
height="24" |
| 97 |
role="img" |
| 98 |
focusable="false" |
| 99 |
> |
| 100 |
<path d="M18 11.2h-5.2V6h-1.6v5.2H6v1.6h5.2V18h1.6v-5.2H18z" /> |
| 101 |
</svg> |
| 102 |
|
| 103 |
{__('Add Option', 'ghostkit')} |
| 104 |
</Button> |
| 105 |
</div> |
| 106 |
); |
| 107 |
} |
| 108 |
|