UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

113 lines (112 loc) 7.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const formik_1 = require("formik"); const lodash_1 = require("lodash"); const react_1 = require("react"); const react_intl_1 = require("react-intl"); const builder_1 = require("../../components/builder"); const messages_1 = require("../../components/builder/messages"); const formio_1 = require("../../components/formio"); const errors_1 = require("../../utils/errors"); const helpers_1 = require("./helpers"); /** * Form to configure a Formio 'select' type component. */ const EditForm = () => { var _a; const intl = (0, react_intl_1.useIntl)(); const [isKeyManuallySetRef, generatedKey] = (0, builder_1.useDeriveComponentKey)(); const { values, setFieldValue } = (0, formik_1.useFormikContext)(); const { hasAnyError } = (0, errors_1.useErrorChecker)(); const { openForms: { dataSrc }, defaultValue, multiple, } = values; (0, react_1.useEffect)(() => { var _a; if (defaultValue === undefined) return; let newDefaultValue; if (multiple) { // `defaultValue` can either be: // - an non-empty string, meaning the user provided a value for it // - `''` or `null`: no default value was provided. Ideally, this should only // be `''`, but Formio is overridding it to `null` :/. That's why we check // for a truthy value here: newDefaultValue = defaultValue ? [defaultValue] : []; } else { if (Array.isArray(defaultValue)) { // if switching from multiple=true->false newDefaultValue = (_a = defaultValue[0]) !== null && _a !== void 0 ? _a : ''; } else { // if the component just got rendered. In this case, `defaultValue` // can be `null` or a string (empty or not). newDefaultValue = defaultValue || ''; } // `defaultValue` is guaranteed to be a an (empty) array thanks to the `if` branch above } setFieldValue('defaultValue', newDefaultValue); }, [multiple]); builder_1.Validate.useManageValidatorsTranslations(['required']); const isManualOptions = (0, helpers_1.checkIsManualOptions)(values); const options = isManualOptions ? ((_a = values.data) === null || _a === void 0 ? void 0 : _a.values) || [] : []; // Ensure that form state is reset if the values source changes. (0, react_1.useLayoutEffect)(() => { const emptyDefaultValue = multiple ? [] : ''; if (dataSrc !== 'variable' || (0, lodash_1.isEqual)(defaultValue, emptyDefaultValue)) return; setFieldValue('defaultValue', emptyDefaultValue); }, [dataSrc]); return ((0, jsx_runtime_1.jsxs)(formio_1.Tabs, { children: [(0, jsx_runtime_1.jsxs)(formio_1.TabList, { children: [(0, jsx_runtime_1.jsx)(builder_1.BuilderTabs.Basic, { hasErrors: hasAnyError('label', 'key', 'description', 'tooltip', 'showInSummary', 'showInEmail', 'showInPDF', 'multiple', 'hidden', 'clearOnHide', 'isSensitiveData', 'openForms.dataSrc', 'openForms.itemsExpression', 'data.values', 'defaultValue') }), (0, jsx_runtime_1.jsx)(builder_1.BuilderTabs.Advanced, { hasErrors: hasAnyError('conditional') }), (0, jsx_runtime_1.jsx)(builder_1.BuilderTabs.Validation, { hasErrors: hasAnyError('validate') }), (0, jsx_runtime_1.jsx)(builder_1.BuilderTabs.Registration, { hasErrors: hasAnyError('registration') }), (0, jsx_runtime_1.jsx)(builder_1.BuilderTabs.Translations, { hasErrors: hasAnyError('openForms.translations') })] }), (0, jsx_runtime_1.jsxs)(formio_1.TabPanel, { children: [(0, jsx_runtime_1.jsx)(builder_1.Label, {}), (0, jsx_runtime_1.jsx)(builder_1.Key, { isManuallySetRef: isKeyManuallySetRef, generatedValue: generatedKey }), (0, jsx_runtime_1.jsx)(builder_1.Description, {}), (0, jsx_runtime_1.jsx)(builder_1.Tooltip, {}), (0, jsx_runtime_1.jsx)(builder_1.PresentationConfig, {}), (0, jsx_runtime_1.jsx)(builder_1.Multiple, { updateDefaultValue: false }), (0, jsx_runtime_1.jsx)(builder_1.Hidden, {}), (0, jsx_runtime_1.jsx)(builder_1.ClearOnHide, {}), (0, jsx_runtime_1.jsx)(builder_1.IsSensitiveData, {}), (0, jsx_runtime_1.jsx)(builder_1.ValuesConfig, { name: "data.values" }), isManualOptions && (0, jsx_runtime_1.jsx)(DefaultValue, { options: options, multiple: !!values.multiple })] }), (0, jsx_runtime_1.jsx)(formio_1.TabPanel, { children: (0, jsx_runtime_1.jsx)(builder_1.SimpleConditional, {}) }), (0, jsx_runtime_1.jsxs)(formio_1.TabPanel, { children: [(0, jsx_runtime_1.jsx)(builder_1.Validate.Required, {}), (0, jsx_runtime_1.jsx)(builder_1.Validate.ValidatorPluginSelect, {}), (0, jsx_runtime_1.jsx)(builder_1.Validate.ValidationErrorTranslations, {})] }), (0, jsx_runtime_1.jsx)(formio_1.TabPanel, { children: (0, jsx_runtime_1.jsx)(builder_1.Registration.RegistrationAttributeSelect, {}) }), (0, jsx_runtime_1.jsx)(formio_1.TabPanel, { children: (0, jsx_runtime_1.jsx)(builder_1.Translations.ComponentTranslations, Object.assign({ propertyLabels: { label: intl.formatMessage(messages_1.LABELS.label), description: intl.formatMessage(messages_1.LABELS.description), tooltip: intl.formatMessage(messages_1.LABELS.tooltip), } }, { children: (0, jsx_runtime_1.jsx)(builder_1.ValuesTranslations, { name: "data.values" }) })) })] })); }; EditForm.defaultValues = { // basic tab label: '', key: '', description: '', tooltip: '', showInSummary: true, showInEmail: false, showInPDF: true, hidden: false, clearOnHide: true, isSensitiveData: false, openForms: { dataSrc: 'manual', translations: {}, }, // fixed, this is what itemsExpression results in via the backend. Do not confuse with // openForms.dataSrc! dataSrc: 'values', dataType: 'string', data: { values: [{ value: '', label: '' }] }, // TODO: at some point we can allow an itemsExpression for this too // Note: Formio will override this to `null`! So be careful when dealing with // the default values of the form defaultValue: '', // Advanced tab conditional: { show: undefined, when: '', eq: '', }, // Validation tab validate: { required: false, plugins: [], }, translatedErrors: {}, registration: { attribute: '', }, }; const DefaultValue = ({ options, multiple }) => { const intl = (0, react_intl_1.useIntl)(); const tooltip = intl.formatMessage({ id: "FffJxu", defaultMessage: [{ type: 0, value: "This will be the initial value for this field before user interaction." }] }); return ((0, jsx_runtime_1.jsx)(formio_1.Select, { name: "defaultValue", options: options, label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, Object.assign({}, messages_1.LABELS.defaultValue)), tooltip: tooltip, isMulti: multiple })); }; exports.default = EditForm;