UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

77 lines (76 loc) 4.55 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useFormikContext } from 'formik'; import { isEqual } from 'lodash'; import { useLayoutEffect } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { BuilderTabs, ClearOnHide, Description, Hidden, IsSensitiveData, Key, Label, PresentationConfig, Registration, SimpleConditional, Tooltip, Translations, Validate, ValuesConfig, ValuesTranslations, useDeriveComponentKey, } from '../../components/builder'; import { LABELS } from '../../components/builder/messages'; import { Radio, TabList, TabPanel, Tabs } from '../../components/formio'; import { useErrorChecker } from '../../utils/errors'; import { checkIsManualOptions } from './helpers'; /** * Form to configure a Formio 'radio' type component. */ const EditForm = () => { const intl = useIntl(); const [isKeyManuallySetRef, generatedKey] = useDeriveComponentKey(); const { values, setFieldValue } = useFormikContext(); const { hasAnyError } = useErrorChecker(); const { openForms: { dataSrc }, defaultValue, } = values; Validate.useManageValidatorsTranslations(['required']); const isManualOptions = checkIsManualOptions(values); const options = isManualOptions ? values.values || [] : []; // Ensure that form state is reset if the values source changes. useLayoutEffect(() => { if (dataSrc !== 'variable' || isEqual(defaultValue, {})) return; setFieldValue('defaultValue', ''); }, [dataSrc]); return (_jsxs(Tabs, { children: [_jsxs(TabList, { children: [_jsx(BuilderTabs.Basic, { hasErrors: hasAnyError('label', 'key', 'description', 'tooltip', 'showInSummary', 'showInEmail', 'showInPDF', 'hidden', 'clearOnHide', 'isSensitiveData', 'openForms.dataSrc', 'openForms.itemsExpression', 'values', 'defaultValue') }), _jsx(BuilderTabs.Advanced, { hasErrors: hasAnyError('conditional') }), _jsx(BuilderTabs.Validation, { hasErrors: hasAnyError('validate') }), _jsx(BuilderTabs.Registration, { hasErrors: hasAnyError('registration') }), _jsx(BuilderTabs.Translations, { hasErrors: hasAnyError('openForms.translations') })] }), _jsxs(TabPanel, { children: [_jsx(Label, {}), _jsx(Key, { isManuallySetRef: isKeyManuallySetRef, generatedValue: generatedKey }), _jsx(Description, {}), _jsx(Tooltip, {}), _jsx(PresentationConfig, {}), _jsx(Hidden, {}), _jsx(ClearOnHide, {}), _jsx(IsSensitiveData, {}), _jsx(ValuesConfig, { name: "values", withOptionDescription: true }), isManualOptions && _jsx(DefaultValue, { options: options })] }), _jsx(TabPanel, { children: _jsx(SimpleConditional, {}) }), _jsxs(TabPanel, { children: [_jsx(Validate.Required, {}), _jsx(Validate.ValidatorPluginSelect, {}), _jsx(Validate.ValidationErrorTranslations, {})] }), _jsx(TabPanel, { children: _jsx(Registration.RegistrationAttributeSelect, {}) }), _jsx(TabPanel, { children: _jsx(Translations.ComponentTranslations, Object.assign({ propertyLabels: { label: intl.formatMessage(LABELS.label), description: intl.formatMessage(LABELS.description), tooltip: intl.formatMessage(LABELS.tooltip), } }, { children: _jsx(ValuesTranslations, { name: "values", withOptionDescription: true }) })) })] })); }; EditForm.defaultValues = { // basic tab label: '', key: '', description: '', tooltip: '', showInSummary: true, showInEmail: false, showInPDF: true, hidden: false, clearOnHide: true, isSensitiveData: false, openForms: { dataSrc: 'manual', translations: {}, }, values: [{ value: '', label: '' }], // TODO: check that the initial values are set based on component.values // TODO: at some point we can allow an itemsExpression for this too defaultValue: '', // Advanced tab conditional: { show: undefined, when: '', eq: '', }, // Validation tab validate: { required: false, plugins: [], }, translatedErrors: {}, registration: { attribute: '', }, }; const DefaultValue = ({ options }) => { const intl = useIntl(); const tooltip = intl.formatMessage({ id: "FffJxu", defaultMessage: [{ type: 0, value: "This will be the initial value for this field before user interaction." }] }); return (_jsx(Radio, { name: "defaultValue", options: options, label: _jsx(FormattedMessage, Object.assign({}, LABELS.defaultValue)), tooltip: tooltip, isClearable: true })); }; export default EditForm;