@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
117 lines (116 loc) • 7.46 kB
JavaScript
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 { NumberField, SelectBoxes, TabList, TabPanel, Tabs } from '../../components/formio';
import { useErrorChecker } from '../../utils/errors';
import { checkIsManualOptions } from './helpers';
/**
* Form to configure a Formio 'selectboxes' 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',
'minSelectedCount',
'maxSelectedCount',
]);
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(MinSelectedCheckboxes, {}), _jsx(MaxSelectedCheckboxes, {}), _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 { getFieldProps, setFieldValue } = useFormikContext();
const { value = {} } = getFieldProps('defaultValue');
const tooltip = intl.formatMessage({ id: "FffJxu", defaultMessage: [{ type: 0, value: "This will be the initial value for this field before user interaction." }] });
// This layout effect uses a non-primitive dependency. It works *because of Formik*
// implementation details, wich uses refs internally and changes the identity of the
// options field only when mutations are made to it (add, change items, re-ordering...)
useLayoutEffect(() => {
const optionValues = options.map(opt => opt.value);
const defaultValueKeys = new Set(Object.keys(value));
// if all the option values are present in the default value map, there is nothing
// to do and we bail early to prevent further form state mutations.
if (defaultValueKeys === new Set(optionValues))
return;
// If no default value is present for an option, make it explicitly false.
// Checking/unchecking persist the state either way, so we only need to do this once
// if an option is present yet.
//
// Additionally, we start with an empty object so that we can drop/discard any default
// values for options that were removed.
const explicitDefaults = {};
optionValues.forEach(optionValue => {
// if a value is specified already in the form state, use it, otherwise default to "unchecked".
const defaultForOption = value.hasOwnProperty(optionValue) ? value[optionValue] : false;
explicitDefaults[optionValue] = defaultForOption;
});
setFieldValue('defaultValue', explicitDefaults);
}, [options]);
return (_jsx(SelectBoxes, { name: "defaultValue", options: options, label: _jsx(FormattedMessage, Object.assign({}, LABELS.defaultValue)), tooltip: tooltip }));
};
const MinSelectedCheckboxes = () => {
const intl = useIntl();
const tooltip = intl.formatMessage({ id: "BVuE94", defaultMessage: [{ type: 0, value: "If specified, the user must check at least this many options." }] });
return (_jsx(NumberField, { name: "validate.minSelectedCount", label: _jsx(FormattedMessage, { id: 'dbAJfK', defaultMessage: [{ type: 0, value: "Minimum selected checkboxes" }] }), placeholder: intl.formatMessage({ id: "oGBVo2", defaultMessage: [{ type: 0, value: "Minimum selected checkboxes (e.g. 1)" }] }), tooltip: tooltip, min: 1, step: 1 }));
};
const MaxSelectedCheckboxes = () => {
const intl = useIntl();
const tooltip = intl.formatMessage({ id: "sR9GVQ", defaultMessage: [{ type: 0, value: "If specified, the user must check at most this many options." }] });
return (_jsx(NumberField, { name: "validate.maxSelectedCount", label: _jsx(FormattedMessage, { id: 'aWwaAq', defaultMessage: [{ type: 0, value: "Maximum selected checkboxes" }] }), placeholder: intl.formatMessage({ id: "VNGD7o", defaultMessage: [{ type: 0, value: "Maximum selected checkboxes (e.g. 1)" }] }), tooltip: tooltip, min: 1, step: 1 }));
};
export default EditForm;