@digigov/form 0.10.8 → 0.10.9
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,92 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import { CalculatedField, FieldProps } from '@digigov/form/Field/types';
|
|
3
|
+
import Fieldset, { FieldsetCaption } from '@digigov/form/Fieldset';
|
|
4
|
+
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
5
|
+
import { FieldBase } from '@digigov/form/Field/FieldBase';
|
|
6
|
+
import { calculateField } from '@digigov/form/Field/utils';
|
|
7
|
+
import { FieldsetLegend } from '@digigov/react-core';
|
|
8
|
+
import { ErrorOption, UseFormReturn } from 'react-hook-form';
|
|
9
|
+
import { FieldSpec } from '@digigov/form/types';
|
|
10
|
+
|
|
11
|
+
export interface FieldObjectProps extends Omit<FieldProps, 'key'> {
|
|
12
|
+
control: UseFormReturn['control'];
|
|
13
|
+
register: UseFormReturn['register'];
|
|
14
|
+
reset: UseFormReturn['reset'];
|
|
15
|
+
formState: UseFormReturn['formState'];
|
|
16
|
+
error?: ErrorOption;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const FieldObject: React.FC<FieldObjectProps> = ({
|
|
20
|
+
name,
|
|
21
|
+
label,
|
|
22
|
+
extra,
|
|
23
|
+
control,
|
|
24
|
+
register,
|
|
25
|
+
reset,
|
|
26
|
+
formState,
|
|
27
|
+
}) => {
|
|
28
|
+
const { t } = useTranslation();
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<Fieldset>
|
|
32
|
+
{label?.primary && (
|
|
33
|
+
<FieldsetLegend size="s">{t(label.primary)}</FieldsetLegend>
|
|
34
|
+
)}
|
|
35
|
+
{label?.secondary && (
|
|
36
|
+
<FieldsetCaption>{t(label.secondary)}</FieldsetCaption>
|
|
37
|
+
)}
|
|
38
|
+
{extra?.fields.map((field) => (
|
|
39
|
+
<FieldObjectItem
|
|
40
|
+
key={`${name}.${field.key}`}
|
|
41
|
+
name={`${name}.${field.key}`}
|
|
42
|
+
control={control}
|
|
43
|
+
register={register}
|
|
44
|
+
reset={reset}
|
|
45
|
+
field={field}
|
|
46
|
+
error={formState.errors && formState.errors[field.key]}
|
|
47
|
+
formState={formState}
|
|
48
|
+
/>
|
|
49
|
+
))}
|
|
50
|
+
</Fieldset>
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export interface FieldObjectItemProps {
|
|
55
|
+
name: string;
|
|
56
|
+
field: FieldSpec;
|
|
57
|
+
control: UseFormReturn['control'];
|
|
58
|
+
register: UseFormReturn['register'];
|
|
59
|
+
reset: UseFormReturn['reset'];
|
|
60
|
+
formState: UseFormReturn['formState'];
|
|
61
|
+
error?: ErrorOption;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const FieldObjectItem: React.FC<FieldObjectItemProps> = ({
|
|
65
|
+
name,
|
|
66
|
+
children,
|
|
67
|
+
field,
|
|
68
|
+
error,
|
|
69
|
+
control,
|
|
70
|
+
formState,
|
|
71
|
+
reset,
|
|
72
|
+
register,
|
|
73
|
+
}) => {
|
|
74
|
+
const calculatedField: CalculatedField = useMemo(
|
|
75
|
+
() => calculateField(children, field),
|
|
76
|
+
[field]
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const customError = error ? error : formState?.errors[name];
|
|
80
|
+
return (
|
|
81
|
+
<FieldBase
|
|
82
|
+
{...calculatedField}
|
|
83
|
+
name={name}
|
|
84
|
+
control={control}
|
|
85
|
+
reset={reset}
|
|
86
|
+
register={register}
|
|
87
|
+
error={customError}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export default FieldObject;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { useContext, useMemo } from 'react';
|
|
2
|
+
import { FormContext } from '@digigov/form/FormContext';
|
|
3
|
+
import { FieldsetSpec } from '@digigov/form/types';
|
|
4
|
+
import Field from '@digigov/form/Field';
|
|
5
|
+
import { useTranslation } from '@digigov/ui/app/i18n';
|
|
6
|
+
import {
|
|
7
|
+
Fieldset,
|
|
8
|
+
FieldsetLabel,
|
|
9
|
+
FieldsetCaption,
|
|
10
|
+
FieldsetBody,
|
|
11
|
+
} from '@digigov/form/Fieldset/';
|
|
12
|
+
|
|
13
|
+
export const FieldsetWithContext: React.FC<{ name: string }> = ({ name }) => {
|
|
14
|
+
const { label, body, fields } = useFieldset(name);
|
|
15
|
+
const { t } = useTranslation();
|
|
16
|
+
return useMemo(
|
|
17
|
+
() => (
|
|
18
|
+
<Fieldset>
|
|
19
|
+
{label?.primary && <FieldsetLabel>{t(label.primary)}</FieldsetLabel>}
|
|
20
|
+
{label?.secondary && (
|
|
21
|
+
<FieldsetCaption>{t(label.secondary)}</FieldsetCaption>
|
|
22
|
+
)}
|
|
23
|
+
{body && <FieldsetBody>{body}</FieldsetBody>}
|
|
24
|
+
{fields.map((field) => (
|
|
25
|
+
<Field key={field} name={field} />
|
|
26
|
+
))}
|
|
27
|
+
</Fieldset>
|
|
28
|
+
),
|
|
29
|
+
[label?.primary, label?.secondary, body, fields]
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const useFieldset = (name: string): FieldsetSpec => {
|
|
34
|
+
const { fieldsetsMap } = useContext(FormContext);
|
|
35
|
+
if (fieldsetsMap && fieldsetsMap[name]) {
|
|
36
|
+
return useMemo(() => fieldsetsMap[name], [name]);
|
|
37
|
+
} else {
|
|
38
|
+
if (!fieldsetsMap) throw new Error(`Fieldsets is not defined`);
|
|
39
|
+
throw new Error(`No fieldset with name ${name} found`);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import Grid from '@digigov/ui/layouts/Grid';
|
|
3
|
+
import Fieldset from '@digigov/react-core/Fieldset';
|
|
4
|
+
import FieldsetLegend from '@digigov/react-core/FieldsetLegend';
|
|
5
|
+
import Hint from '@digigov/react-core/Hint';
|
|
6
|
+
import NormalText from '@digigov/ui/typography/NormalText';
|
|
7
|
+
import {
|
|
8
|
+
FieldsetLabelProps,
|
|
9
|
+
FieldsetCaptionProps,
|
|
10
|
+
FieldsetBodyProps,
|
|
11
|
+
} from '@digigov/form/Fieldset/types';
|
|
12
|
+
|
|
13
|
+
export const FieldsetLabel: React.FC<FieldsetLabelProps> = ({
|
|
14
|
+
children,
|
|
15
|
+
...props
|
|
16
|
+
}) => {
|
|
17
|
+
return (
|
|
18
|
+
<FieldsetLegend heading size="xl" {...props}>
|
|
19
|
+
{children}
|
|
20
|
+
</FieldsetLegend>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const FieldsetCaption: React.FC<FieldsetCaptionProps> = ({
|
|
25
|
+
children,
|
|
26
|
+
...props
|
|
27
|
+
}) => {
|
|
28
|
+
return <Hint {...props}>{children}</Hint>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const FieldsetBody: React.FC<FieldsetBodyProps> = ({ children }) => {
|
|
32
|
+
return (
|
|
33
|
+
<Grid item xs={12}>
|
|
34
|
+
<NormalText>{children}</NormalText>
|
|
35
|
+
</Grid>
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { Fieldset };
|
|
40
|
+
export default Fieldset;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FieldsetLegendProps } from "@digigov/react-core/FieldsetLegend";
|
|
2
|
+
import { HintProps } from "@digigov/react-core/Hint";
|
|
3
|
+
|
|
4
|
+
export interface FieldsetLabelProps extends FieldsetLegendProps {}
|
|
5
|
+
export interface FieldsetCaptionProps extends HintProps{}
|
|
6
|
+
export interface FieldsetBodyProps {}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "4",
|
|
3
|
+
"stories": {
|
|
4
|
+
"digigov-form--int-field": {
|
|
5
|
+
"actionSets": [
|
|
6
|
+
{
|
|
7
|
+
"actions": [
|
|
8
|
+
{
|
|
9
|
+
"name": "click",
|
|
10
|
+
"args": {
|
|
11
|
+
"selector": "html>body>div:nth-child(5)>form>button"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "type",
|
|
16
|
+
"args": {
|
|
17
|
+
"selector": "html>body>div:nth-child(5)>form>div:nth-child(1)>label>input",
|
|
18
|
+
"text": "2.2"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"name": "click",
|
|
23
|
+
"args": {
|
|
24
|
+
"selector": "html>body>div:nth-child(5)>form>button"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"id": "7wMj7iTumGC-",
|
|
29
|
+
"title": "Show validation errors"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"actions": [
|
|
33
|
+
{
|
|
34
|
+
"name": "keyboard.press",
|
|
35
|
+
"args": {
|
|
36
|
+
"key": "Tab"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"name": "keyboard.press",
|
|
41
|
+
"args": {
|
|
42
|
+
"key": "Tab"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
"id": "e7W6ZshKZmsE",
|
|
47
|
+
"title": "Focus with keyboard"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"actions": [
|
|
51
|
+
{
|
|
52
|
+
"name": "type",
|
|
53
|
+
"args": {
|
|
54
|
+
"selector": "html>body>div:nth-child(5)>form>div:nth-child(1)>label>input",
|
|
55
|
+
"text": "1"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"name": "click",
|
|
60
|
+
"args": {
|
|
61
|
+
"selector": "html>body>div:nth-child(5)>form>button"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
],
|
|
65
|
+
"id": "DBqARxwPma5a",
|
|
66
|
+
"title": "Add number"
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: FormBuilder
|
|
3
|
+
title: FormBuilder
|
|
4
|
+
sidebar_label: FormBuilder
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
`FormBuilder` is a React component that provides an easy way to efficiently create reusable form page. Forms are a flexible mechanism for collecting user input because there are suitable widgets for entering many different types of data, including text boxes, checkboxes, radio buttons, dropdowns and so on for submission to a server. Actually when we use `stepForm` for building step-forms all we do is to render `FormBuilder` component.
|
|
8
|
+
|
|
9
|
+
`stepForm` component:
|
|
10
|
+
|
|
11
|
+
<Story packageName="@digigov/form" component="FormBuilder" story="Default.tsx" />
|
|
12
|
+
|
|
13
|
+
### Importing and using FormBuilder Component
|
|
14
|
+
|
|
15
|
+
To import `FormBuilder` component simply import it from `'@digigov/form`.
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
|
|
19
|
+
import {FormBuilder} from '@digigov/form';
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
Now you can use the current component inisde of your component
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import BasicLayout, {
|
|
27
|
+
Top,
|
|
28
|
+
Content,
|
|
29
|
+
Main,
|
|
30
|
+
Bottom,
|
|
31
|
+
} from '@digigov/ui/layouts/Basic';
|
|
32
|
+
import {FormBuilder} from '@digigov/form';
|
|
33
|
+
|
|
34
|
+
export default function MyComponent({ props }) {
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<BasicLayout>
|
|
38
|
+
.
|
|
39
|
+
.
|
|
40
|
+
<Content>
|
|
41
|
+
<Main>
|
|
42
|
+
.
|
|
43
|
+
.
|
|
44
|
+
<FormBuilder></FormBuilder>
|
|
45
|
+
.
|
|
46
|
+
.
|
|
47
|
+
</Main>
|
|
48
|
+
</Content>
|
|
49
|
+
<Bottom>
|
|
50
|
+
<GovGRFooter />
|
|
51
|
+
</Bottom>
|
|
52
|
+
</BasicLayout>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Props
|
|
59
|
+
|
|
60
|
+
`FormBuilder` component accepts several arguments as input props.
|
|
61
|
+
|
|
62
|
+
| Name | Type | Default | Description |
|
|
63
|
+
| ------------- | :-----------: | :-----------: | :-----------: |
|
|
64
|
+
| children | node | | allow you to pass components as data. |
|
|
65
|
+
| steps | array | | an array which includes each step-form format data |
|
|
66
|
+
| name | string | | the name of the step. |
|
|
67
|
+
| title | string | | the title from each step |
|
|
68
|
+
| fields | array | | an array of object fields which they describe each field for each step-form |
|
|
69
|
+
| key | string | | the key value of the input field |
|
|
70
|
+
| label | object | | an object prop which describes the label primary and the secondary name of the field |
|
|
71
|
+
| primary | string | | the name of te field |
|
|
72
|
+
| required | boolean | false | describes the validation of the field |
|
|
73
|
+
| submitButton | boolean | false | if true, it creates a button to continue to the next step |
|
|
74
|
+
| type | string | | describes the format of each field, e.x `type:'string'` is a simple input format field |
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
### Field types
|
|
79
|
+
|
|
80
|
+
Below are demostrated some examples for each type of field
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
### FormBuilder implementation
|
|
84
|
+
|
|
85
|
+
The examples above are implemented by using the code below
|
|
86
|
+
|
|
87
|
+
```javascript
|
|
88
|
+
|
|
89
|
+
import React, { useState } from 'react';
|
|
90
|
+
import BasicLayout, {
|
|
91
|
+
Top,
|
|
92
|
+
Content,
|
|
93
|
+
Main,
|
|
94
|
+
Bottom,
|
|
95
|
+
} from '@digigov/ui/layouts/Basic';
|
|
96
|
+
import Header, { HeaderTitle } from '@digigov/ui/app/Header';
|
|
97
|
+
import GovGRLogo from '@digigov/ui/govgr/GovGRLogo';
|
|
98
|
+
import GovGRFooter from '@digigov/ui/govgr/Footer';
|
|
99
|
+
import Button from '@digigov/ui/core/Button';
|
|
100
|
+
import PageTitle, {
|
|
101
|
+
PageTitleSection,
|
|
102
|
+
PageTitleCaption,
|
|
103
|
+
PageTitleHeading,
|
|
104
|
+
} from '@digigov/ui/app/PageTitle';
|
|
105
|
+
import BackButton from '@digigov/ui/core/Button/BackButton';
|
|
106
|
+
import FormBuilder, { Fieldset, FieldsetLabel, Field } from '@digigov/form';
|
|
107
|
+
import { useTranslation } from '@digigov/ui/app';
|
|
108
|
+
|
|
109
|
+
const FIELDS = [
|
|
110
|
+
{
|
|
111
|
+
key: 'phone',
|
|
112
|
+
label: {
|
|
113
|
+
primary: 'Mobile phone',
|
|
114
|
+
secondary: 'A valid mobile number, eg. +306988848888',
|
|
115
|
+
},
|
|
116
|
+
required: true,
|
|
117
|
+
type: 'mobile_phone',
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
key: 'choice_single',
|
|
121
|
+
label: { primary: 'Is this true or not' },
|
|
122
|
+
type: 'choice:single',
|
|
123
|
+
required: true,
|
|
124
|
+
extra: {
|
|
125
|
+
options: [
|
|
126
|
+
{
|
|
127
|
+
label: { primary: 'Yes' },
|
|
128
|
+
value: 'yes',
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
label: { primary: 'No', secondary: 'Explanation about no option' },
|
|
132
|
+
value: 'no',
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
key: 'choice_single_notitle',
|
|
139
|
+
type: 'choice:single',
|
|
140
|
+
extra: {
|
|
141
|
+
options: [
|
|
142
|
+
{
|
|
143
|
+
label: { primary: 'One' },
|
|
144
|
+
value: '1',
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
label: { primary: 'Two' },
|
|
148
|
+
value: '2',
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
key: 'choice_multiple',
|
|
155
|
+
label: 'Select from below',
|
|
156
|
+
type: 'choice:multiple',
|
|
157
|
+
extra: {
|
|
158
|
+
options: [
|
|
159
|
+
{
|
|
160
|
+
label: { primary: 'Choice 1', secondary: 'Choice 1 explanation' },
|
|
161
|
+
value: 'choice_1',
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
label: { primary: 'Choice 2' },
|
|
165
|
+
value: 'choice_2',
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
key: 'choice_single_select',
|
|
172
|
+
label: 'Select from below',
|
|
173
|
+
type: 'choice:single',
|
|
174
|
+
extra: {
|
|
175
|
+
options: [
|
|
176
|
+
{
|
|
177
|
+
label: { primary: 'Choice 1', secondary: 'Choice 1 explanation' },
|
|
178
|
+
value: 'choice_1',
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
label: { primary: 'Choice 2 lenghty explanation without reason' },
|
|
182
|
+
value: 'choice_2',
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
component: 'Select',
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
key: 'number',
|
|
190
|
+
label: { primary: 'A positive number' },
|
|
191
|
+
required: true,
|
|
192
|
+
width: '100px',
|
|
193
|
+
type: 'int',
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
key: 'string',
|
|
197
|
+
label: null,
|
|
198
|
+
width: '100px',
|
|
199
|
+
required: true,
|
|
200
|
+
type: 'string',
|
|
201
|
+
},
|
|
202
|
+
];
|
|
203
|
+
const initialValues = {
|
|
204
|
+
phone: '6988888888',
|
|
205
|
+
choice_multiple: ['choice_2'],
|
|
206
|
+
string: 'default content',
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const useStyles = makeStyles(
|
|
210
|
+
{
|
|
211
|
+
top: { minHeight: '75px' },
|
|
212
|
+
main: {},
|
|
213
|
+
side: {},
|
|
214
|
+
},
|
|
215
|
+
{ name: 'MuiSite' }
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
export default function Index() {
|
|
219
|
+
const styles = useStyles();
|
|
220
|
+
const [data, setData] = useState(null);
|
|
221
|
+
const { t } = useTranslation();
|
|
222
|
+
return (
|
|
223
|
+
<BasicLayout>
|
|
224
|
+
<Top className={styles.top}>
|
|
225
|
+
<Header>
|
|
226
|
+
<GovGRLogo />
|
|
227
|
+
<HeaderTitle>Service name</HeaderTitle>
|
|
228
|
+
</Header>
|
|
229
|
+
</Top>
|
|
230
|
+
<Content>
|
|
231
|
+
<Main className={styles.main}>
|
|
232
|
+
<BackButton>{t('button.back')}</BackButton>
|
|
233
|
+
<PageTitle>
|
|
234
|
+
<PageTitleSection>
|
|
235
|
+
<PageTitleCaption>Before we start</PageTitleCaption>
|
|
236
|
+
</PageTitleSection>
|
|
237
|
+
<PageTitleHeading>Submit a form</PageTitleHeading>
|
|
238
|
+
</PageTitle>
|
|
239
|
+
<FormBuilder
|
|
240
|
+
fields={FIELDS}
|
|
241
|
+
onSubmit={setData}
|
|
242
|
+
initial={initialValues}
|
|
243
|
+
>
|
|
244
|
+
<Fieldset>
|
|
245
|
+
<FieldsetLabel>This is a fieldset</FieldsetLabel>
|
|
246
|
+
{FIELDS.map((field) => (
|
|
247
|
+
<Field key={field.key} name={field.key} />
|
|
248
|
+
))}
|
|
249
|
+
</Fieldset>
|
|
250
|
+
<Button type="submit">{t('button.continue')}</Button>
|
|
251
|
+
</FormBuilder>
|
|
252
|
+
{data && <pre>{JSON.stringify(data, null, 2)}</pre>}
|
|
253
|
+
</Main>
|
|
254
|
+
</Content>
|
|
255
|
+
<Bottom>
|
|
256
|
+
<GovGRFooter />
|
|
257
|
+
</Bottom>
|
|
258
|
+
</BasicLayout>
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "4",
|
|
3
|
+
"stories": {
|
|
4
|
+
"digigov-form-formbuilder--default": {
|
|
5
|
+
"actionSets": [
|
|
6
|
+
{
|
|
7
|
+
"actions": [
|
|
8
|
+
{
|
|
9
|
+
"name": "type",
|
|
10
|
+
"args": {
|
|
11
|
+
"selector": "html>body>div:nth-child(5)>form>div>label>input",
|
|
12
|
+
"text": "hello"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"name": "click",
|
|
17
|
+
"args": {
|
|
18
|
+
"selector": "html>body>div:nth-child(5)>form>button"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
"id": "96MZZJqDNLCc",
|
|
23
|
+
"title": "type and submit"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"actions": [
|
|
27
|
+
{
|
|
28
|
+
"name": "keyboard.press",
|
|
29
|
+
"args": {
|
|
30
|
+
"key": "Tab"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "keyboard.press",
|
|
35
|
+
"args": {
|
|
36
|
+
"key": "Tab"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"name": "keyboard.press",
|
|
41
|
+
"args": {
|
|
42
|
+
"key": "Enter"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
"id": "El5R6WiI7IMr",
|
|
47
|
+
"title": "focus and show validation error"
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|