UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

104 lines (102 loc) 7.93 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { TextField } from '../../components/formio'; import { useContext } from 'react'; import { FormattedMessage, defineMessage, useIntl } from 'react-intl'; import { BuilderTabs, ClearOnHide, Description, Hidden, IsSensitiveData, Key, Label, PresentationConfig, Registration, SimpleConditional, Tooltip, Translations, Validate, useDeriveComponentKey, } from '../../components/builder'; import { LABELS } from '../../components/builder/messages'; import { Checkbox } from '../../components/formio'; import { DataMap, Panel, Tab, TabList, TabPanel, Tabs } from '../../components/formio'; import { Select } from '../../components/formio'; import { BuilderContext } from '../../context'; import { useErrorChecker } from '../../utils/errors'; export const SubcomponentValidation = ({ prefix, component, label, tooltip, placeholder, }) => { const { supportedLanguageCodes } = useContext(BuilderContext); return (_jsxs(_Fragment, { children: [_jsx(TextField, { name: `${prefix}.${component}.validate.pattern`, label: label, tooltip: tooltip, placeholder: placeholder }), _jsxs(Tabs, { children: [_jsx(TabList, { children: supportedLanguageCodes.map(code => (_jsx(Tab, { children: code.toUpperCase() }, code))) }), supportedLanguageCodes.map(code => (_jsx(TabPanel, { children: _jsx(DataMap, { name: `${prefix}.${component}.translatedErrors.${code}`, keyLabel: _jsx(FormattedMessage, { id: 'OlPsaw', defaultMessage: [{ type: 0, value: "Error code" }] }), ariaLabelMessage: defineMessage({ id: "hEVgKd", defaultMessage: [{ type: 0, value: "Error message for \"" }, { type: 1, value: "key" }, { type: 0, value: "\"" }] }), valueComponent: _jsx(TextField, { name: "message", label: _jsx(FormattedMessage, { id: 'k9tAyW', defaultMessage: [{ type: 0, value: "Error message" }] }) }) }) }, code)))] })] })); }; const DeriveAddress = () => { const intl = useIntl(); const tooltip = intl.formatMessage({ id: "2sbkGI", defaultMessage: [{ type: 0, value: "When enabled, the street name and city are derived from the entered postcode and house number." }] }); return (_jsx(Checkbox, { name: "deriveAddress", label: _jsx(FormattedMessage, { id: 'toTZ0C', defaultMessage: [{ type: 0, value: "Derive address" }] }), tooltip: tooltip })); }; const ColumnsLayout = () => { const intl = useIntl(); const singleColumn = intl.formatMessage({ id: "GsAN0n", defaultMessage: [{ type: 0, value: "Single column" }] }); const doubleColumn = intl.formatMessage({ id: "T2dCIS", defaultMessage: [{ type: 0, value: "Double column" }] }); return (_jsx(Select, { name: "layout", label: _jsx(FormattedMessage, { id: 'Tw9+0D', defaultMessage: [{ type: 0, value: "Layout" }] }), options: [ { value: 'singleColumn', label: singleColumn }, { value: 'doubleColumn', label: doubleColumn }, ], isClearable: true })); }; /** * Form to configure a Formio 'address' type component. */ const EditForm = () => { const intl = useIntl(); const [isKeyManuallySetRef, generatedKey] = useDeriveComponentKey(); const { hasAnyError } = useErrorChecker(); Validate.useManageValidatorsTranslations(['required']); Validate.useManageValidatorsTranslations(['pattern'], `openForms.components.postcode`); Validate.useManageValidatorsTranslations(['pattern'], `openForms.components.city`); return (_jsxs(Tabs, { children: [_jsxs(TabList, { children: [_jsx(BuilderTabs.Basic, { hasErrors: hasAnyError('label', 'key', 'description', 'tooltip', 'hidden', 'clearOnHide', 'isSensitiveData', 'showInSummary', 'showInEmail', 'showInPDF') }), _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(ColumnsLayout, {}), _jsx(DeriveAddress, {}), _jsx(Hidden, {}), _jsx(ClearOnHide, {}), _jsx(IsSensitiveData, {})] }), _jsx(TabPanel, { children: _jsx(SimpleConditional, {}) }), _jsxs(TabPanel, { children: [_jsx(Validate.Required, {}), _jsx(Validate.ValidatorPluginSelect, {}), _jsx(Validate.ValidationErrorTranslations, {}), _jsx(Panel, Object.assign({ title: _jsx(FormattedMessage, { id: 'rW1edF', defaultMessage: [{ type: 0, value: "Postcode" }] }), tooltip: intl.formatMessage({ id: "0FD2pY", defaultMessage: [{ type: 0, value: "Validation for the postcode field" }] }), collapsible: true, initialCollapsed: true }, { children: _jsx(SubcomponentValidation, { prefix: "openForms.components", component: "postcode", label: _jsx(FormattedMessage, { id: '4DrI94', defaultMessage: [{ type: 0, value: "Regular expression for postcode" }] }), tooltip: intl.formatMessage({ id: "WxwqZJ", defaultMessage: [{ type: 0, value: "The regular expression pattern test that the postcode field value must pass before the form can be submitted." }] }), placeholder: intl.formatMessage({ id: "dD9O3Q", defaultMessage: [{ type: 0, value: "Regular expression for postcode" }] }) }) })), _jsx(Panel, Object.assign({ title: _jsx(FormattedMessage, { id: 'fhVFUY', defaultMessage: [{ type: 0, value: "City" }] }), tooltip: intl.formatMessage({ id: "mck25o", defaultMessage: [{ type: 0, value: "Validation for the city field" }] }), collapsible: true, initialCollapsed: true }, { children: _jsx(SubcomponentValidation, { prefix: "openForms.components", component: "city", label: _jsx(FormattedMessage, { id: 'swKpQE', defaultMessage: [{ type: 0, value: "Regular expression for city" }] }), tooltip: intl.formatMessage({ id: "osl4X2", defaultMessage: [{ type: 0, value: "The regular expression pattern test that the city field value must pass before the form can be submitted." }] }), placeholder: intl.formatMessage({ id: "0D+m56", defaultMessage: [{ type: 0, value: "Regular expression for city" }] }) }) }))] }), _jsx(TabPanel, { children: _jsx(Registration.RegistrationAttributeSelect, {}) }), _jsx(TabPanel, { children: _jsx(Translations.ComponentTranslations, { propertyLabels: { label: intl.formatMessage(LABELS.label), description: intl.formatMessage(LABELS.description), tooltip: intl.formatMessage(LABELS.tooltip), } }) })] })); }; /* Making this introspected or declarative doesn't seem advisable, as React is calling React.Children and related API's legacy API - this may get removed in future versions. Explicitly specifying the schema and default values is therefore probbaly best, at the cost of some repetition. */ EditForm.defaultValues = { // basic tab label: '', key: '', description: '', tooltip: '', showInSummary: true, showInEmail: false, showInPDF: true, hidden: false, clearOnHide: true, isSensitiveData: true, deriveAddress: false, layout: 'doubleColumn', defaultValue: { postcode: '', houseNumber: '', houseLetter: '', houseNumberAddition: '', }, // Advanced tab conditional: { show: undefined, when: '', eq: '', }, // Validation tab validate: { required: false, plugins: [], }, translatedErrors: {}, registration: { attribute: '', }, openForms: { translations: {}, components: { postcode: { validate: { pattern: '' }, translatedErrors: {}, }, city: { validate: { pattern: '' }, translatedErrors: {}, }, }, }, }; export default EditForm;