From 29247f8cb9d1d233c77f1744f94b20416d051a2c Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Wed, 25 Oct 2023 16:05:28 +0200 Subject: [PATCH] :sparkles: Implement edit form form file component --- .../ComponentConfiguration.stories.tsx | 30 ++++ src/registry/file/edit-validation.ts | 7 + src/registry/file/edit.tsx | 164 ++++++++++++++++++ src/registry/file/index.ts | 8 +- 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 src/registry/file/edit-validation.ts create mode 100644 src/registry/file/edit.tsx diff --git a/src/components/ComponentConfiguration.stories.tsx b/src/components/ComponentConfiguration.stories.tsx index 080793ad..34e82a6f 100644 --- a/src/components/ComponentConfiguration.stories.tsx +++ b/src/components/ComponentConfiguration.stories.tsx @@ -756,3 +756,33 @@ export const PhoneNumber: Story = { expect(args.onSubmit).toHaveBeenCalled(); }, }; + +export const FileUpload: Story = { + render: Template, + name: 'type: file', + + args: { + component: { + id: 'kiweljhr', + storage: 'url', + url: '', + type: 'file', + key: 'file', + label: 'A file upload', + file: { + name: '', + type: [], + allowedTypesLabels: [], + }, + filePattern: '', + }, + + builderInfo: { + title: 'File upload', + icon: '', + group: 'file', + weight: 10, + schema: {}, + }, + }, +}; diff --git a/src/registry/file/edit-validation.ts b/src/registry/file/edit-validation.ts new file mode 100644 index 00000000..1cb4c6eb --- /dev/null +++ b/src/registry/file/edit-validation.ts @@ -0,0 +1,7 @@ +import {IntlShape} from 'react-intl'; + +import {buildCommonSchema} from '@/registry/validation'; + +const schema = (intl: IntlShape) => buildCommonSchema(intl); + +export default schema; diff --git a/src/registry/file/edit.tsx b/src/registry/file/edit.tsx new file mode 100644 index 00000000..879584d3 --- /dev/null +++ b/src/registry/file/edit.tsx @@ -0,0 +1,164 @@ +import {FileComponentSchema} from '@open-formulieren/types'; +import {useFormikContext} from 'formik'; +import {FormattedMessage, useIntl} from 'react-intl'; + +import { + BuilderTabs, + ClearOnHide, + Description, + Hidden, + IsSensitiveData, + Key, + Label, + Multiple, + PresentationConfig, + SimpleConditional, + Tooltip, + Translations, + Validate, + useDeriveComponentKey, +} from '@/components/builder'; +import {LABELS} from '@/components/builder/messages'; +import {Checkbox, Tab, TabList, TabPanel, Tabs, TextField} from '@/components/formio'; +import {getErrorNames} from '@/utils/errors'; + +import {EditFormDefinition} from '../types'; + +/** + * Form to configure a Formio 'file' type component. + */ +const EditForm: EditFormDefinition = () => { + const intl = useIntl(); + const [isKeyManuallySetRef, generatedKey] = useDeriveComponentKey(); + const {errors} = useFormikContext(); + + const erroredFields = Object.keys(errors).length + ? getErrorNames(errors) + : []; + // TODO: pattern match instead of just string inclusion? + // TODO: move into more generically usuable utility when we implement other component + // types + const hasAnyError = (...fieldNames: string[]): boolean => { + if (!erroredFields.length) return false; + return fieldNames.some(name => erroredFields.includes(name)); + }; + + Validate.useManageValidatorsTranslations(['required']); + + return ( + + + + + + + + + + + + + {/* Basic tab */} + + + + {/* Advanced tab */} + + + + + {/* Validation tab */} + + + + + + {/* File tab */} + + + {/* Registration tab */} + TODO + + {/* Translations */} + + + propertyLabels={{ + label: intl.formatMessage(LABELS.label), + description: intl.formatMessage(LABELS.description), + tooltip: intl.formatMessage(LABELS.tooltip), + }} + /> + + + ); +}; + +EditForm.defaultValues = { + storage: 'url', + url: '', + // basic tab + label: '', + key: '', + description: '', + tooltip: '', + showInSummary: true, + showInEmail: false, + showInPDF: true, + multiple: false, + hidden: false, + clearOnHide: true, + isSensitiveData: true, + // Advanced tab + conditional: { + show: undefined, + when: '', + eq: '', + }, + // Validation tab + validate: { + required: false, + }, + translatedErrors: {}, + // file tab + file: { + name: '', + type: [], + allowedTypesLabels: [], + }, + filePattern: '', + // registration tab + registration: { + informatieobjecttype: '', + bronorganisatie: '', + docVertrouwelijkheidaanduiding: '', + titel: '', + }, +}; + +export default EditForm; diff --git a/src/registry/file/index.ts b/src/registry/file/index.ts index 2741dadc..2c39637f 100644 --- a/src/registry/file/index.ts +++ b/src/registry/file/index.ts @@ -1,10 +1,10 @@ -// import EditForm from './edit'; -// import validationSchema from './edit-validation'; +import EditForm from './edit'; +import validationSchema from './edit-validation'; import Preview from './preview'; export default { - // edit: EditForm, - // editSchema: validationSchema, + edit: EditForm, + editSchema: validationSchema, preview: Preview, defaultValue: [], };