@digigov/form 0.10.8 → 0.10.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -1
- package/Form.stories.playwright.json +0 -72
- package/FormBuilder/FormBuilder.mdx +1 -1
- package/MultiplicityField/__stories__/PreviewDisplay.js +6 -6
- package/Questions/__stories__/Default.js +2 -2
- package/Questions/index.mdx +1 -1
- package/es/Form.stories.playwright.json +0 -72
- package/es/FormBuilder/FormBuilder.mdx +1 -1
- package/es/MultiplicityField/__stories__/PreviewDisplay.js +6 -6
- package/es/Questions/__stories__/Default.js +1 -1
- package/es/Questions/index.mdx +1 -1
- package/esm/Form.stories.playwright.json +0 -72
- package/esm/FormBuilder/FormBuilder.mdx +1 -1
- package/esm/MultiplicityField/__stories__/PreviewDisplay.js +6 -6
- package/esm/Questions/__stories__/Default.js +1 -1
- package/esm/Questions/index.mdx +1 -1
- package/esm/index.js +1 -1
- package/package.json +4 -4
- package/src/Field/FieldBase.tsx +99 -0
- package/src/Field/FieldBaseContainer.tsx +57 -0
- package/src/Field/FieldConditional.tsx +75 -0
- package/src/Field/index.mdx +6 -0
- package/src/Field/index.tsx +92 -0
- package/src/Field/types.tsx +102 -0
- package/src/Field/utils.ts +164 -0
- package/src/FieldArray/FieldArray.stories.js +8 -0
- package/src/FieldArray/FieldArray.stories.playwright.json +353 -0
- package/src/FieldArray/__stories__/Default.tsx +95 -0
- package/src/FieldArray/__stories__/WithExactLength.tsx +95 -0
- package/src/FieldArray/index.tsx +83 -0
- package/src/FieldObject/index.tsx +92 -0
- package/src/Fieldset/FieldsetWithContext.tsx +41 -0
- package/src/Fieldset/index.tsx +40 -0
- package/src/Fieldset/types.tsx +6 -0
- package/src/Form.stories.js +5 -0
- package/src/Form.stories.playwright.json +71 -0
- package/src/FormBuilder/FormBuilder.mdx +271 -0
- package/src/FormBuilder/FormBuilder.stories.js +7 -0
- package/src/FormBuilder/FormBuilder.stories.playwright.json +52 -0
- package/src/FormBuilder/FormBuilder.tsx +165 -0
- package/src/FormBuilder/__stories__/Default.tsx +23 -0
- package/src/FormBuilder/index.tsx +3 -0
- package/src/FormContext.tsx +8 -0
- package/src/MultiplicityField/MultiplicityField.mdx +580 -0
- package/src/MultiplicityField/MultiplicityField.stories.js +12 -0
- package/src/MultiplicityField/MultiplicityField.stories.playwright.json +1370 -0
- package/src/MultiplicityField/__stories__/Default.tsx +100 -0
- package/src/MultiplicityField/__stories__/PreviewDisplay.tsx +160 -0
- package/src/MultiplicityField/__stories__/WithExactLength.tsx +99 -0
- package/src/MultiplicityField/__stories__/WithMaxLength.tsx +102 -0
- package/src/MultiplicityField/__stories__/WithMinAndMaxLength.tsx +103 -0
- package/src/MultiplicityField/__stories__/WithMinLength.tsx +102 -0
- package/src/MultiplicityField/add-objects.tsx +186 -0
- package/src/MultiplicityField/index.tsx +166 -0
- package/src/Questions/Questions.stories.js +7 -0
- package/src/Questions/Questions.tsx +74 -0
- package/src/Questions/QuestionsContext.tsx +9 -0
- package/src/Questions/Step/ReviewStep.tsx +63 -0
- package/src/Questions/Step/Step.tsx +67 -0
- package/src/Questions/Step/StepArrayReview.tsx +68 -0
- package/src/Questions/Step/StepContext.tsx +21 -0
- package/src/Questions/Step/StepDescription.tsx +33 -0
- package/src/Questions/Step/StepForm.tsx +60 -0
- package/src/Questions/Step/StepQuote.tsx +8 -0
- package/src/Questions/Step/StepTitle.tsx +60 -0
- package/src/Questions/Step/getAddMoreFields.tsx +28 -0
- package/src/Questions/Step/index.ts +13 -0
- package/src/Questions/Step/types.tsx +32 -0
- package/src/Questions/__snapshots__/index.spec.tsx.snap +887 -0
- package/src/Questions/__stories__/Default.tsx +130 -0
- package/src/Questions/getNextStep.tsx +24 -0
- package/src/Questions/index.mdx +418 -0
- package/src/Questions/index.spec.tsx +72 -0
- package/src/Questions/index.tsx +5 -0
- package/src/Questions/types.tsx +25 -0
- package/src/__stories__/AutoCompleteField.tsx +45 -0
- package/src/__stories__/IntField.tsx +38 -0
- package/src/create-simple-form.mdx +518 -0
- package/src/index.mdx +44 -0
- package/src/index.ts +3 -0
- package/src/inputs/AutoComplete/index.tsx +85 -0
- package/src/inputs/Checkboxes/Checkboxes.stories.js +7 -0
- package/src/inputs/Checkboxes/Checkboxes.stories.playwright.json +69 -0
- package/src/inputs/Checkboxes/__stories__/Default.tsx +39 -0
- package/src/inputs/Checkboxes/index.mdx +0 -0
- package/src/inputs/Checkboxes/index.tsx +77 -0
- package/src/inputs/DateInput/DateInput.stories.js +7 -0
- package/src/inputs/DateInput/DateInput.stories.playwright.json +72 -0
- package/src/inputs/DateInput/__stories__/Default.tsx +41 -0
- package/src/inputs/DateInput/index.tsx +130 -0
- package/src/inputs/FileInput/FileInput.stories.js +7 -0
- package/src/inputs/FileInput/FileInput.stories.playwright.json +75 -0
- package/src/inputs/FileInput/__stories__/Default.tsx +23 -0
- package/src/inputs/FileInput/index.tsx +76 -0
- package/src/inputs/Input/Input.stories.js +14 -0
- package/src/inputs/Input/Input.stories.playwright.json +376 -0
- package/src/inputs/Input/__stories__/AFM.tsx +24 -0
- package/src/inputs/Input/__stories__/Boolean.tsx +26 -0
- package/src/inputs/Input/__stories__/Default.tsx +25 -0
- package/src/inputs/Input/__stories__/IBAN.tsx +26 -0
- package/src/inputs/Input/__stories__/Integer.tsx +25 -0
- package/src/inputs/Input/__stories__/MobilePhone.tsx +24 -0
- package/src/inputs/Input/__stories__/PhoneNumber.tsx +24 -0
- package/src/inputs/Input/__stories__/PostalCode.tsx +25 -0
- package/src/inputs/Input/index.mdx +8 -0
- package/src/inputs/Input/index.tsx +62 -0
- package/src/inputs/Label/Label.stories.js +7 -0
- package/src/inputs/Label/Label.stories.playwright.json +40 -0
- package/src/inputs/Label/__stories__/Default.tsx +28 -0
- package/src/inputs/Label/index.mdx +0 -0
- package/src/inputs/Label/index.tsx +44 -0
- package/src/inputs/Radio/Radio.stories.js +7 -0
- package/src/inputs/Radio/Radio.stories.playwright.json +57 -0
- package/src/inputs/Radio/__stories__/Default.tsx +42 -0
- package/src/inputs/Radio/index.mdx +0 -0
- package/src/inputs/Radio/index.tsx +57 -0
- package/src/inputs/Select/Select.stories.js +7 -0
- package/src/inputs/Select/Select.stories.playwright.json +22 -0
- package/src/inputs/Select/__stories__/Default.tsx +47 -0
- package/src/inputs/Select/index.tsx +37 -0
- package/src/inputs/index.ts +7 -0
- package/src/installation.mdx +72 -0
- package/src/internal.ts +34 -0
- package/src/registry.js +134 -0
- package/src/types.tsx +110 -0
- package/src/utils.ts +78 -0
- package/src/validators/index.ts +203 -0
- package/src/validators/types.ts +2 -0
- package/src/validators/utils/afm.ts +37 -0
- package/src/validators/utils/file.ts +25 -0
- package/src/validators/utils/iban.ts +119 -0
- package/src/validators/utils/index.ts +94 -0
- package/src/validators/utils/int.ts +22 -0
- package/src/validators/utils/number.ts +18 -0
- package/src/validators/utils/phone.ts +120 -0
- package/src/validators/utils/postal_code.ts +33 -0
- package/src/validators/utils/uuid4.ts +21 -0
- package/src/validators/validators.spec.ts +122 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import FieldContainer from '@digigov/react-core/FieldContainer';
|
|
3
|
+
import LabelContainer from '@digigov/react-core/LabelContainer';
|
|
4
|
+
import CoreFieldset from '@digigov/react-core/Fieldset';
|
|
5
|
+
import Hint from '@digigov/react-core/Hint';
|
|
6
|
+
import FieldsetLegend from '@digigov/react-core/FieldsetLegend';
|
|
7
|
+
import ErrorMessage from '@digigov/react-core/ErrorMessage';
|
|
8
|
+
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
9
|
+
import { FieldContainerProps } from '@digigov/form/Field/types';
|
|
10
|
+
import Label from '@digigov/form/inputs/Label';
|
|
11
|
+
|
|
12
|
+
export const FieldBaseContainer: React.FC<FieldContainerProps> = ({
|
|
13
|
+
name,
|
|
14
|
+
wrapper,
|
|
15
|
+
label,
|
|
16
|
+
children,
|
|
17
|
+
error,
|
|
18
|
+
layout,
|
|
19
|
+
}) => {
|
|
20
|
+
const { t } = useTranslation();
|
|
21
|
+
const errorMessage = error?.message.key
|
|
22
|
+
? error.message.key
|
|
23
|
+
: error?.message || '';
|
|
24
|
+
const errorContext = error?.message.context || {};
|
|
25
|
+
if (wrapper === 'fieldset') {
|
|
26
|
+
return (
|
|
27
|
+
<FieldContainer error={!!error} {...layout}>
|
|
28
|
+
<CoreFieldset>
|
|
29
|
+
<FieldsetLegend size="s">{label && label.primary}</FieldsetLegend>
|
|
30
|
+
{label && label.secondary && <Hint>{t(label.secondary)}</Hint>}
|
|
31
|
+
{error && (
|
|
32
|
+
<ErrorMessage id={`${name}-error`}>
|
|
33
|
+
{t(errorMessage, errorContext)}
|
|
34
|
+
</ErrorMessage>
|
|
35
|
+
)}
|
|
36
|
+
{children}
|
|
37
|
+
</CoreFieldset>
|
|
38
|
+
</FieldContainer>
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
return (
|
|
42
|
+
<FieldContainer error={!!error} {...layout}>
|
|
43
|
+
<LabelContainer>
|
|
44
|
+
{label && <Label label={label} />}
|
|
45
|
+
{error && (
|
|
46
|
+
<ErrorMessage id={`${name}-error`}>
|
|
47
|
+
{t(errorMessage, errorContext)}
|
|
48
|
+
</ErrorMessage>
|
|
49
|
+
)}
|
|
50
|
+
{children}
|
|
51
|
+
</LabelContainer>
|
|
52
|
+
</FieldContainer>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export default FieldBaseContainer;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useWatch } from 'react-hook-form';
|
|
3
|
+
import { evaluateFieldWithConditions } from '@digigov/form/Field/utils';
|
|
4
|
+
import { FieldBase } from '@digigov/form/Field/FieldBase';
|
|
5
|
+
import {
|
|
6
|
+
ChildFieldMemoProps,
|
|
7
|
+
FieldConditionalProps,
|
|
8
|
+
} from '@digigov/form/Field/types';
|
|
9
|
+
|
|
10
|
+
const ChildFieldMemo = React.memo(
|
|
11
|
+
function ChildField({
|
|
12
|
+
dependencies,
|
|
13
|
+
field,
|
|
14
|
+
control,
|
|
15
|
+
reset,
|
|
16
|
+
register,
|
|
17
|
+
error,
|
|
18
|
+
}: ChildFieldMemoProps) {
|
|
19
|
+
const newField = evaluateFieldWithConditions(
|
|
20
|
+
field,
|
|
21
|
+
dependencies as FormData
|
|
22
|
+
);
|
|
23
|
+
return (
|
|
24
|
+
<FieldBase
|
|
25
|
+
{...newField}
|
|
26
|
+
name={newField.key}
|
|
27
|
+
reset={reset}
|
|
28
|
+
control={control}
|
|
29
|
+
register={register}
|
|
30
|
+
error={error}
|
|
31
|
+
/>
|
|
32
|
+
);
|
|
33
|
+
},
|
|
34
|
+
(prev, next) => {
|
|
35
|
+
if (!prev || !prev.dependencies) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (prev.error !== next.error) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
for (const dep in next.dependencies) {
|
|
42
|
+
if (next.dependencies[dep] !== prev.dependencies[dep]) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
export const FieldConditional: React.FC<FieldConditionalProps> = ({
|
|
51
|
+
control,
|
|
52
|
+
field,
|
|
53
|
+
register,
|
|
54
|
+
reset,
|
|
55
|
+
error,
|
|
56
|
+
}) => {
|
|
57
|
+
const dependencyKeys = Object.keys(field.condition);
|
|
58
|
+
const variables = useWatch({
|
|
59
|
+
name: dependencyKeys,
|
|
60
|
+
control,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<ChildFieldMemo
|
|
65
|
+
dependencies={variables}
|
|
66
|
+
field={field}
|
|
67
|
+
control={control}
|
|
68
|
+
register={register}
|
|
69
|
+
reset={reset}
|
|
70
|
+
error={error}
|
|
71
|
+
/>
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export default FieldConditional;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import { FieldBase } from '@digigov/form/Field/FieldBase';
|
|
3
|
+
import {
|
|
4
|
+
FieldWithCondition,
|
|
5
|
+
CalculatedField,
|
|
6
|
+
FieldProps,
|
|
7
|
+
} from '@digigov/form/Field/types';
|
|
8
|
+
import FieldConditional from '@digigov/form/Field/FieldConditional';
|
|
9
|
+
import { useField, calculateField } from '@digigov/form/Field/utils';
|
|
10
|
+
import FieldArray from '@digigov/form/FieldArray';
|
|
11
|
+
import Multiplicity from '@digigov/form/MultiplicityField';
|
|
12
|
+
|
|
13
|
+
export const Field: React.FC<FieldProps> = ({
|
|
14
|
+
name,
|
|
15
|
+
children,
|
|
16
|
+
...customField
|
|
17
|
+
}) => {
|
|
18
|
+
const {
|
|
19
|
+
field,
|
|
20
|
+
control,
|
|
21
|
+
register,
|
|
22
|
+
reset,
|
|
23
|
+
error,
|
|
24
|
+
componentRegistry,
|
|
25
|
+
formState,
|
|
26
|
+
setValue,
|
|
27
|
+
getValues,
|
|
28
|
+
unregister,
|
|
29
|
+
trigger,
|
|
30
|
+
clearErrors,
|
|
31
|
+
} = useField(name, customField?.type ? customField : null);
|
|
32
|
+
const calculatedField: CalculatedField = useMemo(
|
|
33
|
+
() => calculateField(children, field, componentRegistry),
|
|
34
|
+
[field]
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
if (calculatedField.condition) {
|
|
38
|
+
return (
|
|
39
|
+
<FieldConditional
|
|
40
|
+
control={control}
|
|
41
|
+
reset={reset}
|
|
42
|
+
register={register}
|
|
43
|
+
field={calculatedField as FieldWithCondition}
|
|
44
|
+
error={error}
|
|
45
|
+
/>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (calculatedField.type === 'array' && !calculatedField.multiplicity) {
|
|
49
|
+
calculatedField.name = name;
|
|
50
|
+
return (
|
|
51
|
+
<FieldArray
|
|
52
|
+
control={control}
|
|
53
|
+
register={register}
|
|
54
|
+
error={error}
|
|
55
|
+
formState={formState}
|
|
56
|
+
{...calculatedField}
|
|
57
|
+
/>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
if (
|
|
61
|
+
calculatedField.type === 'array' &&
|
|
62
|
+
calculatedField.multiplicity === true
|
|
63
|
+
) {
|
|
64
|
+
calculatedField.name = name;
|
|
65
|
+
return (
|
|
66
|
+
<Multiplicity
|
|
67
|
+
control={control}
|
|
68
|
+
register={register}
|
|
69
|
+
trigger={trigger}
|
|
70
|
+
clearErrors={clearErrors}
|
|
71
|
+
error={error}
|
|
72
|
+
formState={formState}
|
|
73
|
+
setValue={setValue}
|
|
74
|
+
getValues={getValues}
|
|
75
|
+
unregister={unregister}
|
|
76
|
+
{...calculatedField}
|
|
77
|
+
/>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return (
|
|
81
|
+
<FieldBase
|
|
82
|
+
{...calculatedField}
|
|
83
|
+
name={name}
|
|
84
|
+
control={control}
|
|
85
|
+
register={register}
|
|
86
|
+
reset={reset}
|
|
87
|
+
error={error}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export default Field;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ErrorOption, UseFormReturn } from 'react-hook-form';
|
|
3
|
+
import { FieldSpec } from '@digigov/form/types';
|
|
4
|
+
import { FieldCondition, FormData } from '@digigov/form/types';
|
|
5
|
+
|
|
6
|
+
export interface ChildFieldMemoProps extends FieldConditionalProps {
|
|
7
|
+
dependencies: FormData;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface FieldWithCondition extends Omit<CalculatedField, 'condition'> {
|
|
11
|
+
condition: Record<string, FieldCondition>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface FieldConditionalProps {
|
|
15
|
+
field: FieldWithCondition;
|
|
16
|
+
control: UseFormReturn['control'];
|
|
17
|
+
register: UseFormReturn['register'];
|
|
18
|
+
reset: UseFormReturn['reset'];
|
|
19
|
+
error?: ErrorOption;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type FieldComponentItem = {
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
component: any;
|
|
25
|
+
controlled?: boolean;
|
|
26
|
+
wrapper?: FieldSpec['wrapper'];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export interface FieldContainerProps {
|
|
30
|
+
name?: FieldSpec['key'];
|
|
31
|
+
layout?: FieldSpec['layout'];
|
|
32
|
+
label?: FieldSpec['label'];
|
|
33
|
+
error?: ErrorOption | Record<string, any>;
|
|
34
|
+
children?: React.ReactNode;
|
|
35
|
+
wrapper?: FieldSpec['wrapper'];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type FieldComponentRegistry = Record<string, FieldComponentItem>;
|
|
39
|
+
|
|
40
|
+
export type OmittedFieldSpec = Omit<FieldSpec, 'component'>;
|
|
41
|
+
|
|
42
|
+
export type UnknownValue = never | never[];
|
|
43
|
+
|
|
44
|
+
export interface ControlledFieldProps {
|
|
45
|
+
type?: string;
|
|
46
|
+
value: UnknownValue;
|
|
47
|
+
onChange: (v: UnknownValue) => void;
|
|
48
|
+
defaultValue?: UnknownValue;
|
|
49
|
+
onBlur?: (e: Event) => void;
|
|
50
|
+
extra?: never;
|
|
51
|
+
error?: boolean;
|
|
52
|
+
name?: string;
|
|
53
|
+
disabled?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface UncontrolledFieldProps {
|
|
57
|
+
name: string;
|
|
58
|
+
register: FieldBaseProps['register'];
|
|
59
|
+
control?: FieldBaseProps['control'];
|
|
60
|
+
error?: boolean;
|
|
61
|
+
extra?: Record<string, never>;
|
|
62
|
+
type?: string;
|
|
63
|
+
disabled?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface CalculatedField extends OmittedFieldSpec {
|
|
67
|
+
name: string;
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
69
|
+
component: any;
|
|
70
|
+
defaultValue?: never;
|
|
71
|
+
error?: ErrorOption;
|
|
72
|
+
wrapper?: FieldComponentItem['wrapper'];
|
|
73
|
+
multiplicity?: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface FieldBaseProps extends CalculatedField {
|
|
77
|
+
control: UseFormReturn['control'];
|
|
78
|
+
register: UseFormReturn['register'];
|
|
79
|
+
reset: UseFormReturn['reset'];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface FieldProps extends FieldSpec {
|
|
83
|
+
name: string;
|
|
84
|
+
children?: React.ReactNode;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface UseFieldProps {
|
|
88
|
+
field: FieldSpec;
|
|
89
|
+
control: FieldBaseProps['control'];
|
|
90
|
+
register: FieldBaseProps['register'];
|
|
91
|
+
reset: FieldBaseProps['reset'];
|
|
92
|
+
watch: UseFormReturn['watch'];
|
|
93
|
+
formState: UseFormReturn['formState'];
|
|
94
|
+
getFieldState: UseFormReturn['getFieldState'];
|
|
95
|
+
setValue: UseFormReturn['setValue'];
|
|
96
|
+
getValues: UseFormReturn['getValues'];
|
|
97
|
+
unregister: UseFormReturn['unregister'];
|
|
98
|
+
trigger: UseFormReturn['trigger'];
|
|
99
|
+
clearErrors: UseFormReturn['clearErrors'];
|
|
100
|
+
error?: ErrorOption;
|
|
101
|
+
componentRegistry?: FieldComponentRegistry;
|
|
102
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { lazy, useContext, useMemo } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
CalculatedField,
|
|
4
|
+
FieldComponentRegistry,
|
|
5
|
+
FieldProps,
|
|
6
|
+
FieldWithCondition,
|
|
7
|
+
UseFieldProps,
|
|
8
|
+
} from '@digigov/form/Field/types';
|
|
9
|
+
import { FieldSpec } from '@digigov/form/types';
|
|
10
|
+
|
|
11
|
+
import Input from '@digigov/form/inputs/Input';
|
|
12
|
+
import Checkboxes from '@digigov/form/inputs/Checkboxes';
|
|
13
|
+
import Radio from '@digigov/form/inputs/Radio';
|
|
14
|
+
import Select from '@digigov/form/inputs/Select';
|
|
15
|
+
import FileInput from '@digigov/form/inputs/FileInput';
|
|
16
|
+
import DateInput from '@digigov/form/inputs/DateInput';
|
|
17
|
+
import { FormContext } from '@digigov/form/FormContext';
|
|
18
|
+
const AutoComplete = lazy(() => import('@digigov/form/inputs/AutoComplete'));
|
|
19
|
+
|
|
20
|
+
const FIELD_COMPONENTS: FieldComponentRegistry = {
|
|
21
|
+
text: {
|
|
22
|
+
component: Input,
|
|
23
|
+
},
|
|
24
|
+
string: {
|
|
25
|
+
component: Input,
|
|
26
|
+
},
|
|
27
|
+
file: {
|
|
28
|
+
wrapper: 'fieldset',
|
|
29
|
+
component: FileInput,
|
|
30
|
+
},
|
|
31
|
+
date: {
|
|
32
|
+
wrapper: 'fieldset',
|
|
33
|
+
controlled: true,
|
|
34
|
+
component: DateInput,
|
|
35
|
+
},
|
|
36
|
+
'choice:multiple': {
|
|
37
|
+
wrapper: 'fieldset',
|
|
38
|
+
controlled: true,
|
|
39
|
+
component: Checkboxes,
|
|
40
|
+
},
|
|
41
|
+
'choice:single': {
|
|
42
|
+
wrapper: 'fieldset',
|
|
43
|
+
controlled: false,
|
|
44
|
+
component: Radio,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const ALTERNATIVE_COMPONENTS: FieldComponentRegistry = {
|
|
49
|
+
Select: {
|
|
50
|
+
component: Select,
|
|
51
|
+
controlled: false,
|
|
52
|
+
},
|
|
53
|
+
AutoComplete: {
|
|
54
|
+
component: AutoComplete,
|
|
55
|
+
controlled: true,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export function calculateField(
|
|
60
|
+
children: FieldProps['children'],
|
|
61
|
+
field: FieldSpec,
|
|
62
|
+
componentRegistry?: FieldComponentRegistry
|
|
63
|
+
): CalculatedField {
|
|
64
|
+
const calculatedField = { ...field };
|
|
65
|
+
const fieldComponentRegistry = { ...FIELD_COMPONENTS, ...componentRegistry };
|
|
66
|
+
if (children) {
|
|
67
|
+
calculatedField.component = children;
|
|
68
|
+
calculatedField.controlled = true;
|
|
69
|
+
} else if (typeof field.component === 'function') {
|
|
70
|
+
// leave as is
|
|
71
|
+
} else if (!field.component && !field.type) {
|
|
72
|
+
calculatedField.component = fieldComponentRegistry.string.component;
|
|
73
|
+
calculatedField.controlled =
|
|
74
|
+
fieldComponentRegistry.string?.controlled || false;
|
|
75
|
+
} else if (
|
|
76
|
+
typeof field?.extra?.component === 'string' &&
|
|
77
|
+
ALTERNATIVE_COMPONENTS[field.extra.component]
|
|
78
|
+
) {
|
|
79
|
+
calculatedField.controlled =
|
|
80
|
+
ALTERNATIVE_COMPONENTS[field.extra.component]?.controlled || false;
|
|
81
|
+
calculatedField.component =
|
|
82
|
+
ALTERNATIVE_COMPONENTS[field.extra.component].component;
|
|
83
|
+
} else if (
|
|
84
|
+
!field.component &&
|
|
85
|
+
field.type &&
|
|
86
|
+
fieldComponentRegistry[field.type]
|
|
87
|
+
) {
|
|
88
|
+
calculatedField.component = fieldComponentRegistry[field.type].component;
|
|
89
|
+
calculatedField.wrapper = fieldComponentRegistry[field.type].wrapper;
|
|
90
|
+
calculatedField.controlled =
|
|
91
|
+
fieldComponentRegistry[field.type]?.controlled || false;
|
|
92
|
+
} else {
|
|
93
|
+
calculatedField.component = fieldComponentRegistry.string.component;
|
|
94
|
+
calculatedField.controlled =
|
|
95
|
+
fieldComponentRegistry.string?.controlled || false;
|
|
96
|
+
}
|
|
97
|
+
return calculatedField as CalculatedField;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const useField = (
|
|
101
|
+
name: string,
|
|
102
|
+
customField: FieldSpec | null
|
|
103
|
+
): UseFieldProps => {
|
|
104
|
+
const {
|
|
105
|
+
fieldsMap,
|
|
106
|
+
control,
|
|
107
|
+
register,
|
|
108
|
+
reset,
|
|
109
|
+
trigger,
|
|
110
|
+
clearErrors,
|
|
111
|
+
errors,
|
|
112
|
+
registerField,
|
|
113
|
+
watch,
|
|
114
|
+
componentRegistry,
|
|
115
|
+
getFieldState,
|
|
116
|
+
setValue,
|
|
117
|
+
getValues,
|
|
118
|
+
unregister,
|
|
119
|
+
formState,
|
|
120
|
+
} = useContext(FormContext);
|
|
121
|
+
useMemo(
|
|
122
|
+
() => customField?.type && registerField({ ...customField, key: name }),
|
|
123
|
+
[customField, name, registerField]
|
|
124
|
+
);
|
|
125
|
+
return {
|
|
126
|
+
field: customField || fieldsMap[name],
|
|
127
|
+
control,
|
|
128
|
+
register,
|
|
129
|
+
reset,
|
|
130
|
+
trigger,
|
|
131
|
+
watch,
|
|
132
|
+
componentRegistry,
|
|
133
|
+
getFieldState,
|
|
134
|
+
setValue,
|
|
135
|
+
clearErrors,
|
|
136
|
+
getValues,
|
|
137
|
+
unregister,
|
|
138
|
+
formState,
|
|
139
|
+
error: errors[name],
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export function evaluateFieldWithConditions(
|
|
144
|
+
field: FieldWithCondition,
|
|
145
|
+
variables: FormData
|
|
146
|
+
): CalculatedField {
|
|
147
|
+
const newField = { ...field };
|
|
148
|
+
if (variables) {
|
|
149
|
+
for (const key in variables) {
|
|
150
|
+
if (field.condition[key] && field.condition[key].is === variables[key]) {
|
|
151
|
+
const then = field.condition[key].then || {};
|
|
152
|
+
for (const attr in then) {
|
|
153
|
+
newField[attr] = then[attr];
|
|
154
|
+
}
|
|
155
|
+
} else if (field.condition[key]) {
|
|
156
|
+
const otherwise = field.condition[key].else || {};
|
|
157
|
+
for (const attr in otherwise) {
|
|
158
|
+
newField[attr] = otherwise[attr];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return newField;
|
|
164
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import FieldArray from '@digigov/form/FieldArray';
|
|
2
|
+
export default {
|
|
3
|
+
title: 'Digigov Form/FieldArray',
|
|
4
|
+
component: FieldArray,
|
|
5
|
+
displayName: 'FieldArray'
|
|
6
|
+
};
|
|
7
|
+
export * from '@digigov/form/FieldArray/__stories__/Default';
|
|
8
|
+
export * from '@digigov/form/FieldArray/__stories__/WithExactLength';
|