import React, {useState} from 'react'; type Props = { values: string[]; setValue: (field: string, value: string[]) => Promise; field: string; options: {[key: string]: string}; placeholder?: string; invert?: boolean; } const EditableSelection: React.FC = ({ setValue, field, values: currentValues, options, placeholder, invert = false }) => { const [active, setActive] = useState(false); currentValues = currentValues || []; function onClick(ev) { ev.stopPropagation(); setActive(!active); } function addOption(optionKey: string, selected: boolean) { if (selected) { if (currentValues.indexOf(optionKey) < 0) { setValue(field, [...currentValues, optionKey]); } } else { setValue(field, currentValues.filter(value => value !== optionKey)); } } const selection = !invert ? currentValues : (currentValues.length ? Object.keys(options).filter(option => currentValues?.indexOf(option) < 0) : []); return (<> {selection.join(', ') || placeholder} {active &&
    {Object.keys(options).map(key => { const label = options[key]; return (
  • -1) !== invert} value="1" onChange={(ev) => addOption(key, ev.target.checked !== invert)} />
  • ); })}
} ); }; export default EditableSelection;