-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#52] Add base for selectboxes edit form
* Added zod validation schema * Added base form layout (without options table & dynamic toggle) * Wired up edit form to storybook
- Loading branch information
1 parent
0a7285d
commit f99e436
Showing
7 changed files
with
257 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {IntlShape} from 'react-intl'; | ||
import {z} from 'zod'; | ||
|
||
import {buildCommonSchema, jsonSchema, optionSchema} from '@/registry/validation'; | ||
|
||
const manualValuesSchema = z.object({ | ||
openForms: z.object({ | ||
dataSrc: z.literal('manual'), | ||
}), | ||
values: optionSchema.array().min(1), | ||
}); | ||
|
||
const variableValuesSchema = z.object({ | ||
openForms: z.object({ | ||
dataSrc: z.literal('variable'), | ||
// TODO: wire up infernologic type checking | ||
itemsExpression: jsonSchema, | ||
}), | ||
}); | ||
|
||
const valuesSchema = manualValuesSchema.or(variableValuesSchema); | ||
|
||
const schema = (intl: IntlShape) => buildCommonSchema(intl).and(valuesSchema); | ||
|
||
export default schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import {SelectboxesComponentSchema} from '@open-formulieren/types'; | ||
import {useFormikContext} from 'formik'; | ||
import {FormattedMessage, useIntl} from 'react-intl'; | ||
|
||
import { | ||
BuilderTabs, | ||
ClearOnHide, | ||
Description, | ||
Hidden, | ||
IsSensitiveData, | ||
Key, | ||
Label, | ||
PresentationConfig, | ||
Registration, | ||
SimpleConditional, | ||
Tooltip, | ||
Translations, | ||
Validate, | ||
ValuesTable, | ||
useDeriveComponentKey, | ||
} from '@/components/builder'; | ||
import {LABELS} from '@/components/builder/messages'; | ||
import {TabList, TabPanel, Tabs} from '@/components/formio'; | ||
import {getErrorNames} from '@/utils/errors'; | ||
|
||
import {EditFormDefinition} from '../types'; | ||
|
||
/** | ||
* Form to configure a Formio 'selectboxes' type component. | ||
*/ | ||
const EditForm: EditFormDefinition<SelectboxesComponentSchema> = () => { | ||
const intl = useIntl(); | ||
const [isKeyManuallySetRef, generatedKey] = useDeriveComponentKey(); | ||
const {values, errors} = useFormikContext<SelectboxesComponentSchema>(); | ||
|
||
const erroredFields = Object.keys(errors).length | ||
? getErrorNames<SelectboxesComponentSchema>(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<SelectboxesComponentSchema>(['required']); | ||
|
||
return ( | ||
<Tabs> | ||
<TabList> | ||
<BuilderTabs.Basic | ||
hasErrors={hasAnyError( | ||
'label', | ||
'key', | ||
'description', | ||
'tooltip', | ||
'showInSummary', | ||
'showInEmail', | ||
'showInPDF', | ||
'hidden', | ||
'clearOnHide', | ||
'isSensitiveData', | ||
'openForms.dataSrc', | ||
'openForms.itemsExpression', | ||
'values', | ||
'defaultValue' | ||
)} | ||
/> | ||
<BuilderTabs.Advanced hasErrors={hasAnyError('conditional')} /> | ||
<BuilderTabs.Validation hasErrors={hasAnyError('validate')} /> | ||
<BuilderTabs.Registration hasErrors={hasAnyError('registration')} /> | ||
<BuilderTabs.Translations hasErrors={hasAnyError('openForms.translations')} /> | ||
</TabList> | ||
|
||
{/* Basic tab */} | ||
<TabPanel> | ||
<Label /> | ||
<Key isManuallySetRef={isKeyManuallySetRef} generatedValue={generatedKey} /> | ||
<Description /> | ||
<Tooltip /> | ||
<PresentationConfig /> | ||
<Hidden /> | ||
<ClearOnHide /> | ||
<IsSensitiveData /> | ||
<ValuesTable<SelectboxesComponentSchema> name="values" /> | ||
{/*<DefaultValue multiple={!!values.multiple} />*/} | ||
</TabPanel> | ||
|
||
{/* Advanced tab */} | ||
<TabPanel> | ||
<SimpleConditional /> | ||
</TabPanel> | ||
|
||
{/* Validation tab */} | ||
<TabPanel> | ||
<Validate.Required /> | ||
<Validate.ValidatorPluginSelect /> | ||
<Validate.ValidationErrorTranslations /> | ||
</TabPanel> | ||
|
||
{/* Registration tab */} | ||
<TabPanel> | ||
<Registration.RegistrationAttributeSelect /> | ||
</TabPanel> | ||
|
||
{/* Translations */} | ||
<TabPanel> | ||
<Translations.ComponentTranslations<SelectboxesComponentSchema> | ||
propertyLabels={{ | ||
label: intl.formatMessage(LABELS.label), | ||
description: intl.formatMessage(LABELS.description), | ||
tooltip: intl.formatMessage(LABELS.tooltip), | ||
}} | ||
/> | ||
</TabPanel> | ||
</Tabs> | ||
); | ||
}; | ||
|
||
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: '', | ||
}, | ||
}; | ||
|
||
export default EditForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
// 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: '', | ||
// default empty value for Formik - this ignores any manually configured options! | ||
defaultValue: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters