UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

32 lines (31 loc) 1.49 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useFormikContext } from 'formik'; import { FormattedMessage, useIntl } from 'react-intl'; import { hasOwnProperty } from '../../types'; import { Checkbox } from '../formio'; const hasDefaultValue = (component) => { return hasOwnProperty(component, 'defaultValue'); }; function Multiple({ updateDefaultValue = true }) { const intl = useIntl(); const { values, getFieldProps, setFieldValue } = useFormikContext(); const { onChange: formikOnChange } = getFieldProps('multiple'); const tooltip = intl.formatMessage({ id: "WDRqIp", defaultMessage: [{ type: 0, value: "Are there multiple values possible for this field?" }] }); const onChange = (e) => { formikOnChange(e); // only touch default value if we're allowed to if (!updateDefaultValue) return; // update the default value, if there is any if (!hasDefaultValue(values)) return; const { defaultValue } = values; if (defaultValue === undefined) return; const multiEnabled = e.target.checked; const newDefaultValue = multiEnabled ? [defaultValue] : defaultValue[0]; setFieldValue('defaultValue', newDefaultValue); }; return (_jsx(Checkbox, { name: "multiple", label: _jsx(FormattedMessage, { id: '7XdV9f', defaultMessage: [{ type: 0, value: "Multiple values" }] }), tooltip: tooltip, onChange: onChange })); } export default Multiple;