@digigov/form 2.0.0-2445d5cb → 2.0.0-298cfc51
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/Field/FieldBase/index.js +1 -0
- package/Field/FieldBase.js.map +2 -2
- package/Field/FieldBaseContainer/index.js +2 -2
- package/Field/FieldBaseContainer.js.map +2 -2
- package/Field/FieldConditional/index.js +7 -3
- package/Field/FieldConditional.js.map +2 -2
- package/Field/index.js +5 -2
- package/Field/index.js.map +2 -2
- package/Field/types.d.ts +5 -1
- package/Field/utils/index.d.ts +1 -0
- package/Field/utils/index.js +18 -1
- package/Field/utils/index.js.map +2 -2
- package/FieldArray/index.d.ts +1 -0
- package/FieldArray/index.js +7 -5
- package/FieldArray/index.js.map +2 -2
- package/FieldObject/index.d.ts +2 -0
- package/FieldObject/index.js +9 -5
- package/FieldObject/index.js.map +2 -2
- package/Fieldset/index.d.ts +1 -1
- package/Fieldset/index.js +5 -5
- package/Fieldset/index.js.map +2 -2
- package/Fieldset/types.d.ts +2 -3
- package/FormBuilder/index.d.ts +8 -2
- package/FormBuilder/index.js +150 -6
- package/FormBuilder/index.js.map +3 -3
- package/MultiplicityField/add-objects/index.js +7 -5
- package/MultiplicityField/add-objects.js.map +2 -2
- package/MultiplicityField/index.js +16 -12
- package/MultiplicityField/index.js.map +2 -2
- package/Questions/QuestionsContext.d.ts +0 -1
- package/Questions/Step/StepContext.d.ts +0 -1
- package/Questions/Step/StepTitle/index.js +4 -3
- package/Questions/Step/StepTitle.d.ts +1 -1
- package/Questions/Step/StepTitle.js.map +2 -2
- package/Questions/Step/types.d.ts +0 -1
- package/Questions/types.d.ts +0 -1
- package/cjs/Field/FieldBase/index.js +1 -0
- package/cjs/Field/FieldBase.js.map +2 -2
- package/cjs/Field/FieldBaseContainer/index.js +4 -4
- package/cjs/Field/FieldBaseContainer.js.map +3 -3
- package/cjs/Field/FieldConditional/index.js +7 -3
- package/cjs/Field/FieldConditional.js.map +2 -2
- package/cjs/Field/index.js +5 -2
- package/cjs/Field/index.js.map +2 -2
- package/cjs/Field/types.js.map +1 -1
- package/cjs/Field/utils/index.js +18 -0
- package/cjs/Field/utils/index.js.map +2 -2
- package/cjs/FieldArray/index.js +9 -7
- package/cjs/FieldArray/index.js.map +3 -3
- package/cjs/FieldObject/index.js +10 -6
- package/cjs/FieldObject/index.js.map +3 -3
- package/cjs/Fieldset/index.js +10 -10
- package/cjs/Fieldset/index.js.map +3 -3
- package/cjs/Fieldset/types.js.map +1 -1
- package/cjs/FormBuilder/index.js +165 -5
- package/cjs/FormBuilder/index.js.map +3 -3
- package/cjs/MultiplicityField/add-objects/index.js +11 -9
- package/cjs/MultiplicityField/add-objects.js.map +3 -3
- package/cjs/MultiplicityField/index.js +15 -16
- package/cjs/MultiplicityField/index.js.map +3 -3
- package/cjs/Questions/Step/StepTitle/index.js +5 -5
- package/cjs/Questions/Step/StepTitle.js.map +3 -3
- package/cjs/index.js +11 -155
- package/cjs/index.js.map +4 -4
- package/cjs/inputs/AutoCompleteInput/__stories__/Multiple/index.js +0 -1
- package/cjs/inputs/AutoCompleteInput/__stories__/Multiple.js.map +2 -2
- package/cjs/inputs/AutoCompleteInput/index.js +8 -6
- package/cjs/inputs/AutoCompleteInput/index.js.map +3 -3
- package/cjs/inputs/Checkboxes/index.js +7 -9
- package/cjs/inputs/Checkboxes/index.js.map +3 -3
- package/cjs/inputs/DateInput/index.js +3 -4
- package/cjs/inputs/DateInput/index.js.map +3 -3
- package/cjs/inputs/FileInput/index.js +7 -8
- package/cjs/inputs/FileInput/index.js.map +3 -3
- package/cjs/inputs/ImageInput/ImageInput.stories/index.js +4 -4
- package/cjs/inputs/ImageInput/ImageInput.stories.js.map +2 -2
- package/cjs/inputs/ImageInput/__stories__/MaxSize/index.js +69 -0
- package/cjs/inputs/ImageInput/__stories__/MaxSize.js.map +7 -0
- package/cjs/inputs/ImageInput/__stories__/WithInvalidImageDimension/index.js +2 -1
- package/cjs/inputs/ImageInput/__stories__/WithInvalidImageDimension.js.map +2 -2
- package/cjs/inputs/ImageInput/index.js +8 -8
- package/cjs/inputs/ImageInput/index.js.map +3 -3
- package/cjs/inputs/Input/__stories__/LandlineNumber/index.js +1 -0
- package/cjs/inputs/Input/__stories__/LandlineNumber.js.map +2 -2
- package/cjs/inputs/Input/__stories__/MobilePhone/index.js +1 -0
- package/cjs/inputs/Input/__stories__/MobilePhone.js.map +2 -2
- package/cjs/inputs/Input/__stories__/PhoneNumber/index.js +1 -0
- package/cjs/inputs/Input/__stories__/PhoneNumber.js.map +2 -2
- package/cjs/inputs/Input/__stories__/PostalCode/index.js +1 -0
- package/cjs/inputs/Input/__stories__/PostalCode.js.map +2 -2
- package/cjs/inputs/Input/index.js +20 -7
- package/cjs/inputs/Input/index.js.map +3 -3
- package/cjs/inputs/Label/index.js +3 -3
- package/cjs/inputs/Label/index.js.map +3 -3
- package/cjs/inputs/Radio/__stories__/Conditional.js.map +2 -2
- package/cjs/inputs/Radio/index.js +56 -10
- package/cjs/inputs/Radio/index.js.map +3 -3
- package/cjs/inputs/Select/index.js +3 -4
- package/cjs/inputs/Select/index.js.map +3 -3
- package/cjs/inputs/inputsScenarios/index.js +0 -1
- package/cjs/inputs/inputsScenarios.js.map +2 -2
- package/cjs/lazy/index.js +7 -2
- package/cjs/lazy.js.map +2 -2
- package/cjs/registry/index.js +2 -0
- package/cjs/registry.js.map +2 -2
- package/cjs/types.js.map +1 -1
- package/cjs/validators/index.js +16 -66
- package/cjs/validators/index.js.map +3 -3
- package/cjs/validators/utils/date/index.js +138 -0
- package/cjs/validators/utils/date.js.map +7 -0
- package/cjs/validators/utils/file/index.js +1 -1
- package/cjs/validators/utils/file.js.map +2 -2
- package/cjs/validators/utils/index.js +3 -1
- package/cjs/validators/utils/index.js.map +2 -2
- package/index.d.ts +5 -8
- package/index.js +9 -152
- package/index.js.map +4 -4
- package/inputs/AutoCompleteInput/__stories__/Multiple/index.js +0 -1
- package/inputs/AutoCompleteInput/__stories__/Multiple.js.map +2 -2
- package/inputs/AutoCompleteInput/index.js +10 -6
- package/inputs/AutoCompleteInput/index.js.map +2 -2
- package/inputs/Checkboxes/index.js +8 -6
- package/inputs/Checkboxes/index.js.map +2 -2
- package/inputs/DateInput/index.js +5 -3
- package/inputs/DateInput/index.js.map +2 -2
- package/inputs/FileInput/index.js +5 -6
- package/inputs/FileInput/index.js.map +2 -2
- package/inputs/ImageInput/ImageInput.stories/index.js +2 -2
- package/inputs/ImageInput/ImageInput.stories.d.ts +1 -1
- package/inputs/ImageInput/ImageInput.stories.js.map +2 -2
- package/inputs/ImageInput/__stories__/MaxSize/index.js +36 -0
- package/inputs/ImageInput/__stories__/MaxSize/package.json +6 -0
- package/inputs/ImageInput/__stories__/MaxSize.d.ts +3 -0
- package/inputs/ImageInput/__stories__/MaxSize.js.map +7 -0
- package/inputs/ImageInput/__stories__/WithInvalidImageDimension/index.js +2 -1
- package/inputs/ImageInput/__stories__/WithInvalidImageDimension.js.map +2 -2
- package/inputs/ImageInput/index.js +7 -7
- package/inputs/ImageInput/index.js.map +2 -2
- package/inputs/Input/__stories__/LandlineNumber/index.js +1 -0
- package/inputs/Input/__stories__/LandlineNumber.js.map +2 -2
- package/inputs/Input/__stories__/MobilePhone/index.js +1 -0
- package/inputs/Input/__stories__/MobilePhone.js.map +2 -2
- package/inputs/Input/__stories__/PhoneNumber/index.js +1 -0
- package/inputs/Input/__stories__/PhoneNumber.js.map +2 -2
- package/inputs/Input/__stories__/PostalCode/index.js +1 -0
- package/inputs/Input/__stories__/PostalCode.js.map +2 -2
- package/inputs/Input/index.js +18 -5
- package/inputs/Input/index.js.map +3 -3
- package/inputs/Label/index.js +2 -2
- package/inputs/Label/index.js.map +2 -2
- package/inputs/Radio/__stories__/Conditional.js.map +2 -2
- package/inputs/Radio/index.d.ts +4 -0
- package/inputs/Radio/index.js +57 -8
- package/inputs/Radio/index.js.map +2 -2
- package/inputs/Select/index.js +1 -2
- package/inputs/Select/index.js.map +2 -2
- package/inputs/inputsScenarios/index.js +0 -1
- package/inputs/inputsScenarios.d.ts +0 -42
- package/inputs/inputsScenarios.js.map +2 -2
- package/lazy/index.js +7 -2
- package/lazy.d.ts +16 -8
- package/lazy.js.map +2 -2
- package/package.json +4 -4
- package/registry/index.js +2 -0
- package/registry.d.ts +1 -0
- package/registry.js.map +2 -2
- package/src/Field/FieldBase.tsx +1 -0
- package/src/Field/FieldBaseContainer.tsx +2 -2
- package/src/Field/FieldConditional.tsx +4 -0
- package/src/Field/index.tsx +4 -1
- package/src/Field/types.tsx +24 -20
- package/src/Field/utils/index.ts +18 -1
- package/src/FieldArray/index.tsx +9 -5
- package/src/FieldObject/index.tsx +8 -2
- package/src/Fieldset/index.tsx +5 -5
- package/src/Fieldset/types.tsx +2 -2
- package/src/FormBuilder/{FormBuilder.mdx → doc.mdx} +20 -33
- package/src/FormBuilder/index.tsx +179 -7
- package/src/FormBuilder/scenarios.test.tsx +2 -2
- package/src/MultiplicityField/add-objects.tsx +6 -5
- package/src/MultiplicityField/{MultiplicityField.mdx → doc.mdx} +12 -16
- package/src/MultiplicityField/index.tsx +15 -12
- package/src/Questions/Step/StepTitle.tsx +4 -3
- package/src/Questions/__snapshots__/index.spec.tsx.snap +8 -4
- package/src/Questions/{index.mdx → doc.mdx} +9 -12
- package/src/create-simple-form.mdx +2 -6
- package/src/{index.mdx → doc.mdx} +4 -8
- package/src/index.ts +6 -0
- package/src/inputs/AutoCompleteInput/__stories__/Multiple.tsx +0 -1
- package/src/inputs/AutoCompleteInput/{index.mdx → doc.mdx} +2 -13
- package/src/inputs/AutoCompleteInput/index.tsx +36 -29
- package/src/inputs/Checkboxes/{index.mdx → doc.mdx} +5 -15
- package/src/inputs/Checkboxes/index.test.tsx +16 -17
- package/src/inputs/Checkboxes/index.tsx +9 -7
- package/src/inputs/DateInput/{index.mdx → doc.mdx} +1 -8
- package/src/inputs/DateInput/index.tsx +5 -3
- package/src/inputs/FileInput/{index.mdx → doc.mdx} +1 -5
- package/src/inputs/FileInput/index.tsx +5 -6
- package/src/inputs/ImageInput/ImageInput.stories.js +2 -1
- package/src/inputs/ImageInput/__stories__/MaxSize.tsx +37 -0
- package/src/inputs/ImageInput/__stories__/WithInvalidImageDimension.tsx +2 -0
- package/src/inputs/ImageInput/doc.mdx +23 -0
- package/src/inputs/ImageInput/index.test.tsx +4 -0
- package/src/inputs/ImageInput/index.tsx +9 -11
- package/src/inputs/Input/__stories__/LandlineNumber.tsx +2 -1
- package/src/inputs/Input/__stories__/MobilePhone.tsx +1 -0
- package/src/inputs/Input/__stories__/PhoneNumber.tsx +1 -0
- package/src/inputs/Input/__stories__/PostalCode.tsx +1 -0
- package/src/inputs/Input/doc.mdx +56 -0
- package/src/inputs/Input/index.tsx +33 -30
- package/src/inputs/Label/doc.mdx +14 -0
- package/src/inputs/Label/index.tsx +2 -3
- package/src/inputs/OtpInput/{index.mdx → doc.mdx} +1 -8
- package/src/inputs/Radio/__stories__/Conditional.tsx +2 -1
- package/src/inputs/Radio/{index.mdx → doc.mdx} +5 -15
- package/src/inputs/Radio/index.tsx +75 -9
- package/src/inputs/Select/{index.mdx → doc.mdx} +1 -5
- package/src/inputs/Select/index.tsx +1 -2
- package/src/inputs/inputsScenarios.ts +147 -148
- package/src/installation.mdx +2 -5
- package/src/lazy.js +7 -2
- package/src/registry.js +2 -0
- package/src/types.tsx +1 -0
- package/src/validators/index.ts +33 -71
- package/src/validators/utils/date.ts +107 -0
- package/src/validators/utils/file.ts +1 -1
- package/src/validators/utils/index.ts +1 -0
- package/types.d.ts +1 -1
- package/types.js.map +1 -1
- package/validators/index.js +20 -67
- package/validators/index.js.map +2 -2
- package/validators/utils/date/index.js +103 -0
- package/validators/utils/date/package.json +6 -0
- package/validators/utils/date.d.ts +9 -0
- package/validators/utils/date.js.map +7 -0
- package/validators/utils/file/index.js +1 -1
- package/validators/utils/file.js.map +2 -2
- package/validators/utils/index.d.ts +1 -0
- package/validators/utils/index.js +1 -0
- package/validators/utils/index.js.map +2 -2
- package/src/index.tsx +0 -178
- package/src/inputs/ImageInput/index.mdx +0 -19
- package/src/inputs/Input/index.mdx +0 -95
- package/src/inputs/Label/index.mdx +0 -0
- /package/src/Field/{index.mdx → doc.mdx} +0 -0
package/types.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/types.tsx"],
|
|
4
|
-
"sourcesContent": ["import { UseFormReturn, UseFormProps } from 'react-hook-form';\nimport {\n FieldComponentRegistry,\n FieldLabelProps,\n FieldSpec,\n FormData,\n} from '@digigov/form/Field/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\nexport * from '@digigov/form/Field/types';\nexport type FieldError = {\n message: string;\n};\n\nexport interface FieldsetSpec {\n key: string;\n fields: string[];\n label?: FieldLabelProps;\n body?: React.ReactNode;\n}\n\nexport interface FormContextProps {\n fieldsetsMap?: Record<string, FieldsetSpec>;\n fieldsMap: Record<string, FieldSpec>;\n control: UseFormReturn['control'];\n register: UseFormReturn['register'];\n trigger: UseFormReturn['trigger'];\n clearErrors: UseFormReturn['clearErrors'];\n watch: UseFormReturn['watch'];\n getFieldState: UseFormReturn['getFieldState'];\n setValue: UseFormReturn['setValue'];\n getValues: UseFormReturn['getValues'];\n unregister: UseFormReturn['unregister'];\n formState: UseFormReturn['formState'];\n reset: UseFormReturn['reset'];\n resetField: UseFormReturn['resetField'];\n registerField: (field: FieldSpec) => void;\n errors: UseFormReturn['formState']['errors'];\n componentRegistry?: FieldComponentRegistry;\n submit: () => Promise<void>;\n}\n\nexport interface FormBuilderProps {\n fields?: FieldSpec[];\n fieldsets?: FieldsetSpec[];\n initial?: FormData;\n onSubmit?: (data: FormData) => void | null | FieldError[];\n mode?: UseFormProps['mode'];\n reValidateMode?: UseFormProps['reValidateMode'];\n criteriaMode?: UseFormProps['criteriaMode'];\n children?: React.ReactNode;\n shouldFocusError?: boolean;\n auto?: boolean;\n validatorRegistry?: Record<string, ValidatorSchema[]>;\n componentRegistry?: FieldComponentRegistry;\n grid?: boolean;\n}\n\nexport interface FormBaseProps\n extends Omit<FormBuilderProps, 'fields' | 'fieldsets' | 'auto'> {\n fieldsetsMap?: Record<string, FieldsetSpec>;\n fieldsMap: Record<string, FieldSpec>;\n registerField: (field: FieldSpec) => void;\n resolver: any;\n}\n"],
|
|
4
|
+
"sourcesContent": ["import { UseFormReturn, UseFormProps } from 'react-hook-form';\nimport {\n FieldComponentRegistry,\n FieldLabelProps,\n FieldSpec,\n FormData,\n} from '@digigov/form/Field/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\nexport * from '@digigov/form/Field/types';\nexport type FieldError = {\n message: string;\n};\n\nexport interface FieldsetSpec {\n key: string;\n fields: string[];\n label?: FieldLabelProps;\n body?: React.ReactNode;\n}\n\nexport interface FormContextProps {\n fieldsetsMap?: Record<string, FieldsetSpec>;\n fieldsMap: Record<string, FieldSpec>;\n control: UseFormReturn['control'];\n register: UseFormReturn['register'];\n trigger: UseFormReturn['trigger'];\n clearErrors: UseFormReturn['clearErrors'];\n watch: UseFormReturn['watch'];\n getFieldState: UseFormReturn['getFieldState'];\n setValue: UseFormReturn['setValue'];\n getValues: UseFormReturn['getValues'];\n unregister: UseFormReturn['unregister'];\n formState: UseFormReturn['formState'];\n reset: UseFormReturn['reset'];\n resetField: UseFormReturn['resetField'];\n registerField: (field: FieldSpec) => void;\n errors: UseFormReturn['formState']['errors'];\n componentRegistry?: FieldComponentRegistry;\n submit: () => Promise<void>;\n}\n\nexport interface FormBuilderProps {\n fields?: FieldSpec[];\n fieldsets?: FieldsetSpec[];\n initial?: FormData;\n onSubmit?: (data: FormData) => void | null | FieldError[];\n mode?: UseFormProps['mode'];\n reValidateMode?: UseFormProps['reValidateMode'];\n criteriaMode?: UseFormProps['criteriaMode'];\n children?: React.ReactNode;\n shouldFocusError?: boolean;\n auto?: boolean;\n validatorRegistry?: Record<string, ValidatorSchema[]>;\n componentRegistry?: FieldComponentRegistry;\n grid?: boolean;\n controlledFieldsOnly?: boolean;\n}\n\nexport interface FormBaseProps\n extends Omit<FormBuilderProps, 'fields' | 'fieldsets' | 'auto'> {\n fieldsetsMap?: Record<string, FieldsetSpec>;\n fieldsMap: Record<string, FieldSpec>;\n registerField: (field: FieldSpec) => void;\n resolver: any;\n}\n"],
|
|
5
5
|
"mappings": "AAQA,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/validators/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
|
-
import dayjs from "dayjs";
|
|
3
|
-
import customParseFormat from "dayjs/plugin/customParseFormat";
|
|
4
2
|
import * as yup from "yup";
|
|
5
3
|
import {
|
|
6
4
|
getYupObjectShape,
|
|
@@ -13,16 +11,13 @@ import {
|
|
|
13
11
|
TEXT_LIMIT_VALIDATOR,
|
|
14
12
|
POSTALCODE_VALIDATOR,
|
|
15
13
|
UUID4_VALIDATOR,
|
|
16
|
-
IMAGE_DIMENSION_VALIDATOR
|
|
14
|
+
IMAGE_DIMENSION_VALIDATOR,
|
|
15
|
+
DATE_VALIDATOR,
|
|
16
|
+
LATER_THAN_VALIDATOR,
|
|
17
|
+
EARLIER_THAN_VALIDATOR
|
|
17
18
|
} from "@digigov/form/validators/utils";
|
|
18
19
|
import { INT_VALIDATOR } from "@digigov/form/validators/utils/int";
|
|
19
20
|
import { NUMBER_VALIDATOR } from "@digigov/form/validators/utils/number";
|
|
20
|
-
dayjs.extend(customParseFormat);
|
|
21
|
-
const VALID_DATE_FORMATS = ["DD/MM/YYYY"];
|
|
22
|
-
const DATE_CACHE = {};
|
|
23
|
-
const getDate = (v) => {
|
|
24
|
-
return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);
|
|
25
|
-
};
|
|
26
21
|
const getYUPTypeMap = () => {
|
|
27
22
|
const yupTypeMap = {
|
|
28
23
|
file: (field) => {
|
|
@@ -34,7 +29,14 @@ const getYUPTypeMap = () => {
|
|
|
34
29
|
}).nullable().test(FILE_MAX_SIZE_VALIDATOR(field));
|
|
35
30
|
},
|
|
36
31
|
string: yup.string,
|
|
37
|
-
boolean:
|
|
32
|
+
boolean: () => {
|
|
33
|
+
return yup.boolean().nullable().transform((value) => {
|
|
34
|
+
if (value === "" || typeof value === "string") return null;
|
|
35
|
+
if (typeof value === "boolean") return value;
|
|
36
|
+
if (value === 0 || value === 1) return Boolean(value);
|
|
37
|
+
return value;
|
|
38
|
+
});
|
|
39
|
+
},
|
|
38
40
|
object: (field) => {
|
|
39
41
|
return getYupObjectShape(field.extra.fields, yupTypeMap);
|
|
40
42
|
},
|
|
@@ -97,65 +99,16 @@ const getYUPTypeMap = () => {
|
|
|
97
99
|
"choice:multiple": () => yup.array().of(yup.string()).nullable(),
|
|
98
100
|
"choice:single": () => yup.string().nullable(),
|
|
99
101
|
date: (field) => {
|
|
100
|
-
let
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return date.isValid();
|
|
104
|
-
});
|
|
105
|
-
const params = field.extra || {};
|
|
106
|
-
if (params.max) {
|
|
107
|
-
let maxDate;
|
|
108
|
-
if (params.max === "now") {
|
|
109
|
-
const today = /* @__PURE__ */ new Date();
|
|
110
|
-
maxDate = new Date(today);
|
|
111
|
-
} else {
|
|
112
|
-
maxDate = getDate(params.max).toDate();
|
|
113
|
-
}
|
|
114
|
-
const maxNextDate = new Date(maxDate);
|
|
115
|
-
maxNextDate.setDate(maxDate.getDate() + 1);
|
|
116
|
-
simpleDate = simpleDate.test({
|
|
117
|
-
name: "earlier-than",
|
|
118
|
-
message: {
|
|
119
|
-
key: "form.error.date.earlier_than",
|
|
120
|
-
context: {
|
|
121
|
-
maxDate: maxNextDate.toLocaleDateString()
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
test: function(value) {
|
|
125
|
-
if (!value) return true;
|
|
126
|
-
const date = getDate(value);
|
|
127
|
-
const isValid = +date.toDate() < +maxDate;
|
|
128
|
-
return isValid;
|
|
129
|
-
}
|
|
130
|
-
});
|
|
102
|
+
let schema = yup.string().nullable();
|
|
103
|
+
if (field?.extra?.min) {
|
|
104
|
+
schema = schema.test(LATER_THAN_VALIDATOR(field));
|
|
131
105
|
}
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
minDate = new Date(today);
|
|
137
|
-
} else {
|
|
138
|
-
minDate = getDate(params.min).toDate();
|
|
139
|
-
}
|
|
140
|
-
const minPreviousDate = new Date(minDate);
|
|
141
|
-
minPreviousDate.setDate(minDate.getDate() - 1);
|
|
142
|
-
simpleDate = simpleDate.test({
|
|
143
|
-
name: "later-than",
|
|
144
|
-
message: {
|
|
145
|
-
key: "form.error.date.later_than",
|
|
146
|
-
context: {
|
|
147
|
-
minDate: minPreviousDate.toLocaleDateString()
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
test: function(value) {
|
|
151
|
-
if (!value) return true;
|
|
152
|
-
const date = getDate(value);
|
|
153
|
-
const isValid = +date.toDate() > +minDate;
|
|
154
|
-
return isValid;
|
|
155
|
-
}
|
|
156
|
-
});
|
|
106
|
+
if (field?.extra?.max) {
|
|
107
|
+
schema = schema.test(EARLIER_THAN_VALIDATOR(field));
|
|
108
|
+
} else {
|
|
109
|
+
schema = schema.test(DATE_VALIDATOR);
|
|
157
110
|
}
|
|
158
|
-
return
|
|
111
|
+
return schema;
|
|
159
112
|
}
|
|
160
113
|
};
|
|
161
114
|
return yupTypeMap;
|
package/validators/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/validators/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { MutableRefObject, useMemo } from 'react';\nimport
|
|
5
|
-
"mappings": "AAAA,SAA2B,eAAe;AAC1C,
|
|
4
|
+
"sourcesContent": ["import { MutableRefObject, useMemo } from 'react';\nimport * as yup from 'yup';\nimport { AnyObjectSchema, StringSchema } from 'yup';\nimport { RequiredArraySchema } from 'yup/lib/array';\nimport Lazy from 'yup/lib/Lazy';\nimport { MixedSchema } from 'yup/lib/mixed';\nimport { TypeOfShape, AssertsShape } from 'yup/lib/object';\nimport { AnyObject } from 'yup/lib/types';\nimport { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\nimport {\n getYupObjectShape,\n AFM_VALIDATOR,\n FILE_MAX_SIZE_VALIDATOR,\n IBAN_VALIDATOR,\n OTP_VALIDATOR,\n MOBILE_PHONE_VALIDATOR,\n PHONE_NUMBER_VALIDATOR,\n TEXT_LIMIT_VALIDATOR,\n POSTALCODE_VALIDATOR,\n UUID4_VALIDATOR,\n IMAGE_DIMENSION_VALIDATOR,\n DATE_VALIDATOR,\n LATER_THAN_VALIDATOR,\n EARLIER_THAN_VALIDATOR,\n} from '@digigov/form/validators/utils';\nimport { INT_VALIDATOR } from '@digigov/form/validators/utils/int';\nimport { NUMBER_VALIDATOR } from '@digigov/form/validators/utils/number';\n\nexport const getYUPTypeMap = (): Record<string, any> => {\n const yupTypeMap = {\n file: (field): MixedSchema => {\n return yup\n .mixed()\n .transform((value) => {\n if (!value.length) {\n return null;\n }\n return value;\n })\n .nullable()\n .test(FILE_MAX_SIZE_VALIDATOR(field));\n },\n string: yup.string,\n boolean: (): yup.BooleanSchema<\n boolean | null | undefined,\n AnyObject,\n boolean | null | undefined\n > => {\n return yup\n .boolean()\n .nullable()\n .transform((value) => {\n if (value === '' || typeof value === 'string') return null;\n if (typeof value === 'boolean') return value;\n if (value === 0 || value === 1) return Boolean(value);\n return value;\n });\n },\n object: (\n field\n ):\n | yup.ObjectSchema<any, AnyObject, TypeOfShape<any>, AssertsShape<any>>\n | Lazy<any, unknown> => {\n return getYupObjectShape(field.extra.fields, yupTypeMap);\n },\n array: (\n field\n ):\n | yup.ArraySchema<any, AnyObject, any[] | undefined>\n | RequiredArraySchema<any, AnyObject, any[] | undefined> => {\n if (typeof field.extra.of === 'object') {\n // else if the field is an object then it is a scalar type\n // eg. number, string etc.\n let arrayValidator = yup\n .array()\n .of(yupTypeMap[field.extra.of.type](field.extra.of))\n .required(field.required);\n if (field.extra.length)\n arrayValidator = arrayValidator.length(field.extra.length, {\n key: 'form.error.array.length',\n context: {\n length: field.extra.length,\n },\n });\n if (field.extra.min)\n arrayValidator = arrayValidator.min(field.extra.min, {\n key: 'form.error.array.min',\n context: {\n min: field.extra.min,\n },\n });\n if (field.extra.max)\n arrayValidator = arrayValidator.max(field.extra.max, {\n key: 'form.error.array.max',\n context: {\n max: field.extra.max,\n },\n });\n return arrayValidator;\n } else if (typeof field.extra.of === 'string') {\n return yup.array().of(yupTypeMap[field.extra.of]());\n } else {\n // in any other case it will be string\n return yup.array().of(yup.string());\n }\n },\n number: (): MixedSchema =>\n yup\n .mixed()\n .transform((_, val) => (val !== '' ? Number(val) : null))\n .test(NUMBER_VALIDATOR()),\n int: (): MixedSchema =>\n yup\n .mixed()\n .transform((_, val) => (val !== '' ? Number(val) : null))\n .test(NUMBER_VALIDATOR())\n .test(INT_VALIDATOR()),\n email: (): yup.StringSchema => yup.string().email('form.error.email'),\n afm: (): yup.StringSchema => yup.string().test(AFM_VALIDATOR),\n uuid4: (): yup.StringSchema => yup.string().test(UUID4_VALIDATOR),\n iban: (field): yup.StringSchema => yup.string().test(IBAN_VALIDATOR(field)),\n otp: (field): yup.StringSchema => yup.string().test(OTP_VALIDATOR(field)),\n text: (field): yup.StringSchema => {\n if (field?.extra?.limit?.max || !!field?.extra?.limit?.min) {\n return yup.string().test(TEXT_LIMIT_VALIDATOR(field));\n } else {\n return yup.string();\n }\n },\n image: (field): MixedSchema => {\n return yup\n .mixed()\n .transform((value) => {\n if (!value.length || value.length === 0) {\n return null;\n }\n return value;\n })\n .nullable()\n .test(FILE_MAX_SIZE_VALIDATOR(field))\n .test(IMAGE_DIMENSION_VALIDATOR(field));\n },\n postal_code: (field): yup.StringSchema =>\n yup.string().test(POSTALCODE_VALIDATOR(field)),\n mobile_phone: (): yup.StringSchema =>\n yup.string().test(MOBILE_PHONE_VALIDATOR),\n phone_number: (field): yup.StringSchema =>\n yup.string().test(PHONE_NUMBER_VALIDATOR(field)),\n 'choice:multiple': (): yup.ArraySchema<\n yup.StringSchema<string | undefined, AnyObject, string | undefined>,\n AnyObject,\n (string | undefined)[] | null | undefined,\n (string | undefined)[] | null | undefined\n > => yup.array().of(yup.string()).nullable(),\n 'choice:single': (): StringSchema<\n string | null | undefined,\n AnyObject,\n string | null | undefined\n > => yup.string().nullable(),\n date: (\n field\n ): yup.StringSchema<\n string | null | undefined,\n AnyObject,\n string | null | undefined\n > => {\n let schema = yup.string().nullable();\n if (field?.extra?.min) {\n schema = schema.test(LATER_THAN_VALIDATOR(field));\n }\n if (field?.extra?.max) {\n schema = schema.test(EARLIER_THAN_VALIDATOR(field));\n } else {\n schema = schema.test(DATE_VALIDATOR);\n }\n return schema;\n },\n };\n return yupTypeMap;\n};\n\nexport interface MutableRefObjectProps {}\n// Create a yup validation schema from given fields input\nexport function useValidationSchema(\n fields: FieldSpec[] | MutableRefObject<MutableRefObjectProps>,\n validatorRegistry?: Record<string, ValidatorSchema[]>\n): Lazy<any, unknown> | AnyObjectSchema | void {\n return useMemo(() => {\n const yupTypeMap = getYUPTypeMap();\n return getYupObjectShape(fields, yupTypeMap, validatorRegistry);\n }, []);\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAA2B,eAAe;AAC1C,YAAY,SAAS;AASrB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AAE1B,MAAM,gBAAgB,MAA2B;AACtD,QAAM,aAAa;AAAA,IACjB,MAAM,CAAC,UAAuB;AAC5B,aAAO,IACJ,MAAM,EACN,UAAU,CAAC,UAAU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ACnC,KAAK,0BAA0B,KAAK,CAAC;AAAA,IAC1C;AAAA,IACA,aAAa,CAAC,UACZ,IAAI,OAAO,EAAE,KAAK,qBAAqB,KAAK,CAAC;AAAA,IAC/C,cAAc,MACZ,IAAI,OAAO,EAAE,KAAK,sBAAsB;AAAA,IAC1C,cAAc,CAAC,UACb,IAAI,OAAO,EAAE,KAAK,uBAAuB,KAAK,CAAC;AAAA,IACjD,mBAAmB,MAKd,IAAI,MAAM,EAAE,GAAG,IAAI,OAAO,CAAC,EAAE,SAAS;AAAA,IAC3C,iBAAiB,MAIZ,IAAI,OAAO,EAAE,SAAS;AAAA,IAC3B,MAAM,CACJ,UAKG;AACH,UAAI,SAAS,IAAI,OAAO,EAAE,SAAS;AACnC,UAAI,OAAO,OAAO,KAAK;AACrB,iBAAS,OAAO,KAAK,qBAAqB,KAAK,CAAC;AAAA,MAClD;AACA,UAAI,OAAO,OAAO,KAAK;AACrB,iBAAS,OAAO,KAAK,uBAAuB,KAAK,CAAC;AAAA,MACpD,OAAO;AACL,iBAAS,OAAO,KAAK,cAAc;AAAA,MACrC;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAIO,SAAS,oBACd,QACA,mBAC6C;AAC7C,SAAO,QAAQ,MAAM;AACnB,UAAM,aAAa,cAAc;AACjC,WAAO,kBAAkB,QAAQ,YAAY,iBAAiB;AAAA,EAChE,GAAG,CAAC,CAAC;AACP;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import customParseFormat from "dayjs/plugin/customParseFormat";
|
|
3
|
+
dayjs.extend(customParseFormat);
|
|
4
|
+
const VALID_DATE_FORMATS = ["DD/MM/YYYY"];
|
|
5
|
+
const DATE_CACHE = {};
|
|
6
|
+
const getDate = (v) => {
|
|
7
|
+
return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);
|
|
8
|
+
};
|
|
9
|
+
const checkPartialDate = (value) => {
|
|
10
|
+
console.log("value.split('/')", value.split("/"));
|
|
11
|
+
const [day, month, year] = value.split("/");
|
|
12
|
+
if (day && !month && !year) {
|
|
13
|
+
return "form.error.date.invalid_month_year";
|
|
14
|
+
}
|
|
15
|
+
if (!day && month && !year) {
|
|
16
|
+
return "form.error.date.invalid_day_year";
|
|
17
|
+
}
|
|
18
|
+
if (!day && !month && year) {
|
|
19
|
+
return "form.error.date.invalid_day_month";
|
|
20
|
+
}
|
|
21
|
+
if (day && month && !year) {
|
|
22
|
+
return "form.error.date.invalid_year";
|
|
23
|
+
}
|
|
24
|
+
if (day && !month && year) {
|
|
25
|
+
return "form.error.date.invalid_month";
|
|
26
|
+
}
|
|
27
|
+
if (!day && month && year) {
|
|
28
|
+
return "form.error.date.invalid_day";
|
|
29
|
+
} else {
|
|
30
|
+
return "form.error.date.invalid";
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const LATER_THAN_VALIDATOR = (field) => {
|
|
34
|
+
let minDate;
|
|
35
|
+
const params = field.extra || {};
|
|
36
|
+
if (params.min === "now") {
|
|
37
|
+
const today = /* @__PURE__ */ new Date();
|
|
38
|
+
minDate = new Date(today);
|
|
39
|
+
} else {
|
|
40
|
+
minDate = getDate(params.min).toDate();
|
|
41
|
+
}
|
|
42
|
+
const minPreviousDate = new Date(minDate);
|
|
43
|
+
minPreviousDate.setDate(minDate.getDate() - 1);
|
|
44
|
+
return {
|
|
45
|
+
name: "later-than",
|
|
46
|
+
message: {
|
|
47
|
+
key: "form.error.date.later_than",
|
|
48
|
+
context: {
|
|
49
|
+
minDate: minPreviousDate.toLocaleDateString()
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
test: function(value) {
|
|
53
|
+
if (!value) return true;
|
|
54
|
+
const date = getDate(value);
|
|
55
|
+
const isValid = +date.toDate() > +minDate;
|
|
56
|
+
return isValid;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
const EARLIER_THAN_VALIDATOR = (field) => {
|
|
61
|
+
let maxDate;
|
|
62
|
+
const params = field.extra || {};
|
|
63
|
+
if (params.max === "now") {
|
|
64
|
+
const today = /* @__PURE__ */ new Date();
|
|
65
|
+
maxDate = new Date(today);
|
|
66
|
+
} else {
|
|
67
|
+
maxDate = getDate(params.max).toDate();
|
|
68
|
+
}
|
|
69
|
+
const maxNextDate = new Date(maxDate);
|
|
70
|
+
maxNextDate.setDate(maxDate.getDate() + 1);
|
|
71
|
+
return {
|
|
72
|
+
name: "earlier-than",
|
|
73
|
+
message: {
|
|
74
|
+
key: "form.error.date.earlier_than",
|
|
75
|
+
context: {
|
|
76
|
+
maxDate: maxNextDate.toLocaleDateString()
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
test: function(value) {
|
|
80
|
+
if (!value) return true;
|
|
81
|
+
const date = getDate(value);
|
|
82
|
+
const isValid = +date.toDate() < +maxDate;
|
|
83
|
+
return isValid;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
const DATE_VALIDATOR = {
|
|
88
|
+
name: "date-validator",
|
|
89
|
+
message: (v) => {
|
|
90
|
+
return checkPartialDate(v.value);
|
|
91
|
+
},
|
|
92
|
+
test: function(value) {
|
|
93
|
+
if (!value) return true;
|
|
94
|
+
const date = getDate(value);
|
|
95
|
+
return date.isValid();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export {
|
|
99
|
+
DATE_VALIDATOR,
|
|
100
|
+
EARLIER_THAN_VALIDATOR,
|
|
101
|
+
LATER_THAN_VALIDATOR
|
|
102
|
+
};
|
|
103
|
+
//# sourceMappingURL=date.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FieldSpec } from '@digigov/form/types';
|
|
2
|
+
import { ValidatorSchema } from '@digigov/form/validators/types';
|
|
3
|
+
export declare const LATER_THAN_VALIDATOR: (field: FieldSpec) => ValidatorSchema;
|
|
4
|
+
export declare const EARLIER_THAN_VALIDATOR: (field: FieldSpec) => ValidatorSchema;
|
|
5
|
+
export declare const DATE_VALIDATOR: {
|
|
6
|
+
name: string;
|
|
7
|
+
message: (v: any) => string;
|
|
8
|
+
test: (value: string) => any;
|
|
9
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/validators/utils/date.ts"],
|
|
4
|
+
"sourcesContent": ["import dayjs from 'dayjs';\nimport customParseFormat from 'dayjs/plugin/customParseFormat';\nimport { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\n\ndayjs.extend(customParseFormat);\n\nconst VALID_DATE_FORMATS = ['DD/MM/YYYY'];\nconst DATE_CACHE = {};\n\nconst getDate = (v: string) => {\n return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);\n};\n\n// Function to check partial date completion\nconst checkPartialDate = (value: string) => {\n console.log(\"value.split('/')\", value.split('/'));\n const [day, month, year] = value.split('/');\n if (day && !month && !year) {\n return 'form.error.date.invalid_month_year';\n }\n if (!day && month && !year) {\n return 'form.error.date.invalid_day_year';\n }\n if (!day && !month && year) {\n return 'form.error.date.invalid_day_month';\n }\n if (day && month && !year) {\n return 'form.error.date.invalid_year';\n }\n if (day && !month && year) {\n return 'form.error.date.invalid_month';\n }\n if (!day && month && year) {\n return 'form.error.date.invalid_day';\n } else {\n return 'form.error.date.invalid';\n }\n};\n\nexport const LATER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {\n let minDate;\n const params = field.extra || {};\n if (params.min === 'now') {\n const today = new Date();\n minDate = new Date(today);\n } else {\n minDate = getDate(params.min).toDate();\n }\n const minPreviousDate = new Date(minDate);\n minPreviousDate.setDate(minDate.getDate() - 1);\n return {\n name: 'later-than',\n message: {\n key: 'form.error.date.later_than',\n context: {\n minDate: minPreviousDate.toLocaleDateString(),\n },\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n const isValid = +date.toDate() > +minDate;\n return isValid;\n },\n };\n};\n\nexport const EARLIER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {\n let maxDate;\n const params = field.extra || {};\n if (params.max === 'now') {\n const today = new Date();\n maxDate = new Date(today);\n } else {\n maxDate = getDate(params.max).toDate();\n }\n const maxNextDate = new Date(maxDate);\n maxNextDate.setDate(maxDate.getDate() + 1);\n return {\n name: 'earlier-than',\n message: {\n key: 'form.error.date.earlier_than',\n context: {\n maxDate: maxNextDate.toLocaleDateString(),\n },\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n const isValid = +date.toDate() < +maxDate;\n return isValid;\n },\n };\n};\n\nexport const DATE_VALIDATOR = {\n name: 'date-validator',\n message: (v): string => {\n return checkPartialDate(v.value);\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n return date.isValid();\n },\n};\n"],
|
|
5
|
+
"mappings": "AAAA,OAAO,WAAW;AAClB,OAAO,uBAAuB;AAI9B,MAAM,OAAO,iBAAiB;AAE9B,MAAM,qBAAqB,CAAC,YAAY;AACxC,MAAM,aAAa,CAAC;AAEpB,MAAM,UAAU,CAAC,MAAc;AAC7B,SAAO,WAAW,CAAC,IAAI,WAAW,CAAC,IAAI,MAAM,GAAG,oBAAoB,IAAI;AAC1E;AAGA,MAAM,mBAAmB,CAAC,UAAkB;AAC1C,UAAQ,IAAI,oBAAoB,MAAM,MAAM,GAAG,CAAC;AAChD,QAAM,CAAC,KAAK,OAAO,IAAI,IAAI,MAAM,MAAM,GAAG;AAC1C,MAAI,OAAO,CAAC,SAAS,CAAC,MAAM;AAC1B,WAAO;AAAA,EACT;AACA,MAAI,CAAC,OAAO,SAAS,CAAC,MAAM;AAC1B,WAAO;AAAA,EACT;AACA,MAAI,CAAC,OAAO,CAAC,SAAS,MAAM;AAC1B,WAAO;AAAA,EACT;AACA,MAAI,OAAO,SAAS,CAAC,MAAM;AACzB,WAAO;AAAA,EAC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YAAY,mBAAmB;AAAA,MAC1C;AAAA,IACF;AAAA,IACA,MAAM,SAAU,OAAe;AAC7B,UAAI,CAAC,MAAO,QAAO;AACnB,YAAM,OAAO,QAAQ,KAAK;AAC1B,YAAM,UAAU,CAAC,KAAK,OAAO,IAAI,CAAC;AAClC,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAEO,MAAM,iBAAiB;AAAA,EAC5B,MAAM;AAAA,EACN,SAAS,CAAC,MAAc;AACtB,WAAO,iBAAiB,EAAE,KAAK;AAAA,EACjC;AAAA,EACA,MAAM,SAAU,OAAe;AAC7B,QAAI,CAAC,MAAO,QAAO;AACnB,UAAM,OAAO,QAAQ,KAAK;AAC1B,WAAO,KAAK,QAAQ;AAAA,EACtB;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -2,7 +2,7 @@ const DEFAULT_FILE_MAX_SIZE = 1e7;
|
|
|
2
2
|
const FILE_MAX_SIZE_VALIDATOR = (field) => ({
|
|
3
3
|
name: "file-max-size-validator",
|
|
4
4
|
message: {
|
|
5
|
-
key: "form.error.file_size",
|
|
5
|
+
key: field.type === "file" ? "form.error.file_size" : "form.error.image_size",
|
|
6
6
|
context: {
|
|
7
7
|
maxSizeToMb: (field?.extra?.limit.maxSize || DEFAULT_FILE_MAX_SIZE) / 1e6
|
|
8
8
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/validators/utils/file.ts"],
|
|
4
|
-
"sourcesContent": ["import { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\n\nconst DEFAULT_FILE_MAX_SIZE = 10000000;\n\nexport const FILE_MAX_SIZE_VALIDATOR = (field: FieldSpec): ValidatorSchema => ({\n name: 'file-max-size-validator',\n message: {\n key: 'form.error.file_size',\n context: {\n maxSizeToMb: (field?.extra?.limit.maxSize || DEFAULT_FILE_MAX_SIZE) / 1000000,\n },\n },\n test: (value: File[]): boolean => {\n if (value) {\n for (const file of value) {\n const maxSize = field?.extra?.limit?.maxSize || DEFAULT_FILE_MAX_SIZE\n if (file.size > maxSize) {\n return false;\n }\n }\n }\n return true;\n },\n});\n"],
|
|
5
|
-
"mappings": "AAGA,MAAM,wBAAwB;AAEvB,MAAM,0BAA0B,CAAC,WAAuC;AAAA,EAC7E,MAAM;AAAA,EACN,SAAS;AAAA,IACP,KAAK;AAAA,
|
|
4
|
+
"sourcesContent": ["import { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\n\nconst DEFAULT_FILE_MAX_SIZE = 10000000;\n\nexport const FILE_MAX_SIZE_VALIDATOR = (field: FieldSpec): ValidatorSchema => ({\n name: 'file-max-size-validator',\n message: {\n key: field.type === 'file' ? 'form.error.file_size' : 'form.error.image_size',\n context: {\n maxSizeToMb: (field?.extra?.limit.maxSize || DEFAULT_FILE_MAX_SIZE) / 1000000,\n },\n },\n test: (value: File[]): boolean => {\n if (value) {\n for (const file of value) {\n const maxSize = field?.extra?.limit?.maxSize || DEFAULT_FILE_MAX_SIZE\n if (file.size > maxSize) {\n return false;\n }\n }\n }\n return true;\n },\n});\n"],
|
|
5
|
+
"mappings": "AAGA,MAAM,wBAAwB;AAEvB,MAAM,0BAA0B,CAAC,WAAuC;AAAA,EAC7E,MAAM;AAAA,EACN,SAAS;AAAA,IACP,KAAK,MAAM,SAAS,SAAS,yBAAyB;AAAA,IACtD,SAAS;AAAA,MACP,cAAc,OAAO,OAAO,MAAM,WAAW,yBAAyB;AAAA,IACxE;AAAA,EACF;AAAA,EACA,MAAM,CAAC,UAA2B;AAChC,QAAI,OAAO;AACT,iBAAW,QAAQ,OAAO;AACxB,cAAM,UAAU,OAAO,OAAO,OAAO,WAAW;AAChD,YAAI,KAAK,OAAO,SAAS;AACvB,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -10,6 +10,7 @@ export * from '@digigov/form/validators/utils/phone';
|
|
|
10
10
|
export * from '@digigov/form/validators/utils/postal_code';
|
|
11
11
|
export * from '@digigov/form/validators/utils/uuid4';
|
|
12
12
|
export * from '@digigov/form/validators/utils/text_limit';
|
|
13
|
+
export * from '@digigov/form/validators/utils/date';
|
|
13
14
|
export declare function getYupField(field: FieldSpec, yupTypeMap: Record<string, any>): any;
|
|
14
15
|
export declare function computeShape(fields: FieldSpec[], yupTypeMap: Record<string, ValidatorSchema>, validatorRegistry: Record<string, ValidatorSchema[]> | undefined): {};
|
|
15
16
|
export declare function getYupObjectShape(fields: FieldSpec[] | any, yupTypeMap: Record<string, any>, validatorRegistry?: Record<string, ValidatorSchema[]>): yup.ObjectSchema<{
|
|
@@ -8,6 +8,7 @@ export * from "@digigov/form/validators/utils/phone";
|
|
|
8
8
|
export * from "@digigov/form/validators/utils/postal_code";
|
|
9
9
|
export * from "@digigov/form/validators/utils/uuid4";
|
|
10
10
|
export * from "@digigov/form/validators/utils/text_limit";
|
|
11
|
+
export * from "@digigov/form/validators/utils/date";
|
|
11
12
|
function getYupField(field, yupTypeMap) {
|
|
12
13
|
const yupField = yupTypeMap[field?.type || "string"] || yupTypeMap["string"];
|
|
13
14
|
return yupField(field);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/validators/utils/index.ts"],
|
|
4
|
-
"sourcesContent": ["import * as yup from 'yup';\nimport { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\n\nexport * from '@digigov/form/validators/utils/afm';\nexport * from '@digigov/form/validators/utils/file';\nexport * from '@digigov/form/validators/utils/iban';\nexport * from '@digigov/form/validators/utils/otp';\nexport * from '@digigov/form/validators/utils/image';\nexport * from '@digigov/form/validators/utils/phone';\nexport * from '@digigov/form/validators/utils/postal_code';\nexport * from '@digigov/form/validators/utils/uuid4';\nexport * from '@digigov/form/validators/utils/text_limit';\n\nexport function getYupField(field: FieldSpec, yupTypeMap: Record<string, any>) {\n const yupField = yupTypeMap[field?.type || 'string'] || yupTypeMap['string'];\n return yupField(field);\n}\nexport function computeShape(\n fields: FieldSpec[],\n yupTypeMap: Record<string, ValidatorSchema>,\n validatorRegistry: Record<string, ValidatorSchema[]> | undefined\n) {\n const fieldSchemas = {};\n const objectFields: Record<string, FieldSpec[]> = {};\n fields.forEach((field) => {\n let yupField = getYupField(field, yupTypeMap);\n if (field.condition) {\n for (const key in field.condition) {\n let then: any, otherwise: any;\n if (field.condition[key].then) {\n if (field.condition[key].then?.required === false) {\n then = yupField.nullable();\n } else if (field.condition[key].then?.required === true) {\n then = yupField.nullable().required('form.error.required');\n }\n }\n if (field.condition[key].else) {\n if (field.condition[key].else?.required === false) {\n otherwise = yupField.nullable();\n } else if (field.condition[key].else?.required === true) {\n otherwise = yupField.nullable().required('form.error.required');\n }\n }\n if (then || otherwise) {\n yupField = yupField.when(key, {\n is: (val) => {\n //@ts-ignore\n const is = field.condition[key].is;\n if (is === val || (Array.isArray(val) && val.includes(is))) {\n return true;\n } else {\n return false;\n }\n },\n then,\n otherwise,\n });\n }\n }\n } else if (field.required) {\n yupField = yupField.nullable().required('form.error.required');\n }\n if (validatorRegistry && field.type && validatorRegistry[field.type]) {\n validatorRegistry[field.type].forEach((validator: any) => {\n yupField = yupField.test(validator);\n });\n }\n if (field.validators) {\n field.validators.forEach((validator) => {\n yupField = yupField.test(validator);\n });\n }\n fieldSchemas[field.key] = yupField;\n });\n for (const objectKey in objectFields) {\n fieldSchemas[objectKey] = getYupObjectShape(\n objectFields[objectKey],\n yupTypeMap,\n validatorRegistry\n );\n }\n return fieldSchemas;\n}\n\nexport function getYupObjectShape(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fields: FieldSpec[] | any,\n yupTypeMap: Record<string, any>,\n validatorRegistry?: Record<string, ValidatorSchema[]>\n) {\n if (!fields.current) {\n const fieldSchemas = computeShape(fields, yupTypeMap, validatorRegistry);\n return yup.object().shape(fieldSchemas);\n }\n\n return yup.lazy(function () {\n const fieldSchemas = computeShape(\n Object.values(fields.current),\n yupTypeMap,\n validatorRegistry\n );\n return yup.object().shape(fieldSchemas);\n });\n}\n"],
|
|
5
|
-
"mappings": "AAAA,YAAY,SAAS;AAIrB,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAEP,SAAS,YAAY,OAAkB,YAAiC;AAC7E,QAAM,WAAW,WAAW,OAAO,QAAQ,QAAQ,KAAK,WAAW,QAAQ;AAC3E,SAAO,SAAS,KAAK;AACvB;AACO,SAAS,aACd,QACA,YACA,mBACA;AACA,QAAM,eAAe,CAAC;AACtB,QAAM,eAA4C,CAAC;AACnD,SAAO,QAAQ,CAAC,UAAU;AACxB,QAAI,WAAW,YAAY,OAAO,UAAU;AAC5C,QAAI,MAAM,WAAW;AACnB,iBAAW,OAAO,MAAM,WAAW;AACjC,YAAI,MAAW;AACf,YAAI,MAAM,UAAU,GAAG,EAAE,MAAM;AAC7B,cAAI,MAAM,UAAU,GAAG,EAAE,MAAM,aAAa,OAAO;AACjD,mBAAO,SAAS,SAAS;AAAA,UAC3B,WAAW,MAAM,UAAU,GAAG,EAAE,MAAM,aAAa,MAAM;AACvD,mBAAO,SAAS,SAAS,EAAE,SAAS,qBAAqB;AAAA,UAC3D;AAAA,QACF;AACA,YAAI,MAAM,UAAU,GAAG,EAAE,MAAM;AAC7B,cAAI,MAAM,UAAU,GAAG,EAAE,MAAM,aAAa,OAAO;AACjD,wBAAY,SAAS,SAAS;AAAA,UAChC,WAAW,MAAM,UAAU,GAAG,EAAE,MAAM,aAAa,MAAM;AACvD,wBAAY,SAAS,SAAS,EAAE,SAAS,qBAAqB;AAAA,UAChE;AAAA,QACF;AACA,YAAI,QAAQ,WAAW;AACrB,qBAAW,SAAS,KAAK,KAAK;AAAA,YAC5B,IAA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form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\n\nexport * from '@digigov/form/validators/utils/afm';\nexport * from '@digigov/form/validators/utils/file';\nexport * from '@digigov/form/validators/utils/iban';\nexport * from '@digigov/form/validators/utils/otp';\nexport * from '@digigov/form/validators/utils/image';\nexport * from '@digigov/form/validators/utils/phone';\nexport * from '@digigov/form/validators/utils/postal_code';\nexport * from '@digigov/form/validators/utils/uuid4';\nexport * from '@digigov/form/validators/utils/text_limit';\nexport * from '@digigov/form/validators/utils/date';\n\nexport function getYupField(field: FieldSpec, yupTypeMap: Record<string, any>) {\n const yupField = yupTypeMap[field?.type || 'string'] || yupTypeMap['string'];\n return yupField(field);\n}\nexport function computeShape(\n fields: FieldSpec[],\n yupTypeMap: Record<string, ValidatorSchema>,\n validatorRegistry: Record<string, ValidatorSchema[]> | undefined\n) {\n const fieldSchemas = {};\n const objectFields: Record<string, FieldSpec[]> = {};\n fields.forEach((field) => {\n let yupField = getYupField(field, yupTypeMap);\n if (field.condition) {\n for (const key in field.condition) {\n let then: any, otherwise: any;\n if (field.condition[key].then) {\n if (field.condition[key].then?.required === false) {\n then = yupField.nullable();\n } else if (field.condition[key].then?.required === true) {\n then = yupField.nullable().required('form.error.required');\n }\n }\n if (field.condition[key].else) {\n if (field.condition[key].else?.required === false) {\n otherwise = yupField.nullable();\n } else if (field.condition[key].else?.required === true) {\n otherwise = yupField.nullable().required('form.error.required');\n }\n }\n if (then || otherwise) {\n yupField = yupField.when(key, {\n is: (val) => {\n //@ts-ignore\n const is = field.condition[key].is;\n if (is === val || (Array.isArray(val) && val.includes(is))) {\n return true;\n } else {\n return false;\n }\n },\n then,\n otherwise,\n });\n }\n }\n } else if (field.required) {\n yupField = yupField.nullable().required('form.error.required');\n }\n if (validatorRegistry && field.type && validatorRegistry[field.type]) {\n validatorRegistry[field.type].forEach((validator: any) => {\n yupField = yupField.test(validator);\n });\n }\n if (field.validators) {\n field.validators.forEach((validator) => {\n yupField = yupField.test(validator);\n });\n }\n fieldSchemas[field.key] = yupField;\n });\n for (const objectKey in objectFields) {\n fieldSchemas[objectKey] = getYupObjectShape(\n objectFields[objectKey],\n yupTypeMap,\n validatorRegistry\n );\n }\n return fieldSchemas;\n}\n\nexport function getYupObjectShape(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fields: FieldSpec[] | any,\n yupTypeMap: Record<string, any>,\n validatorRegistry?: Record<string, ValidatorSchema[]>\n) {\n if (!fields.current) {\n const fieldSchemas = computeShape(fields, yupTypeMap, validatorRegistry);\n return yup.object().shape(fieldSchemas);\n }\n\n return yup.lazy(function () {\n const fieldSchemas = computeShape(\n Object.values(fields.current),\n yupTypeMap,\n validatorRegistry\n );\n return yup.object().shape(fieldSchemas);\n });\n}\n"],
|
|
5
|
+
"mappings": "AAAA,YAAY,SAAS;AAIrB,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAEP,SAAS,YAAY,OAAkB,YAAiC;AAC7E,QAAM,WAAW,WAAW,OAAO,QAAQ,QAAQ,KAAK,WAAW,QAAQ;AAC3E,SAAO,SAAS,KAAK;AACvB;AACO,SAAS,aACd,QACA,YACA,mBACA;AACA,QAAM,eAAe,CAAC;AACtB,QAAM,eAA4C,CAAC;AACnD,SAAO,QAAQ,CAAC,UAAU;AACxB,QAAI,WAAW,YAAY,OAAO,UAAU;AAC5C,QAAI,MAAM,WAAW;AACnB,iBAAW,OAAO,MAAM,WAAW;AACjC,YAAI,MAAW;AACf,YAAI,MAAM,UAAU,GAAG,EAAE,MAAM;AAC7B,cAAI,MAAM,UAAU,GAAG,EAAE,MAAM,aAAa,OAAO;AACjD,mBAAO,SAAS,SAAS;AAAA,UAC3B,WAAW,MAAM,UAAU,GAAG,EAAE,MAAM,aAAa,MAAM;AACvD,mBAAO,SAAS,SAAS,EAAE,SAAS,qBAAqB;AAAA,UAC3D;AAAA,QACF;AACA,YAAI,MAAM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AAiB;AACvE,WAAO,IAAI,OAAO,EAAE,MAAM,YAAY;AAAA,EACxC;AAEA,SAAO,IAAI,KAAK,WAAY;AAC1B,UAAM,eAAe;AAAA,MACnB,OAAO,OAAO,OAAO,OAAO;AAAA,MAC5B;AAAA,MACA;AAAA,IACF;AACA,WAAO,IAAI,OAAO,EAAE,MAAM,YAAY;AAAA,EACxC,CAAC;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/src/index.tsx
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import React, { useCallback, useContext, useRef } from 'react';
|
|
3
|
-
import { useForm } from 'react-hook-form';
|
|
4
|
-
import Field from '@digigov/form/Field';
|
|
5
|
-
import Fieldset from '@digigov/form/Fieldset';
|
|
6
|
-
import { FormContext } from '@digigov/form/FormContext';
|
|
7
|
-
import { FormBaseProps, FormData, FormBuilderProps } from '@digigov/form/types';
|
|
8
|
-
import { yupResolver } from '@digigov/form/utils';
|
|
9
|
-
import { useValidationSchema } from '@digigov/form/validators';
|
|
10
|
-
import { Form } from '@digigov/react-core/Form';
|
|
11
|
-
import { Button } from '@digigov/ui/form/Button';
|
|
12
|
-
|
|
13
|
-
const FormBase = React.forwardRef(function FormBase(
|
|
14
|
-
{
|
|
15
|
-
onSubmit,
|
|
16
|
-
children,
|
|
17
|
-
registerField,
|
|
18
|
-
fieldsMap,
|
|
19
|
-
fieldsetsMap,
|
|
20
|
-
resolver,
|
|
21
|
-
mode,
|
|
22
|
-
initial,
|
|
23
|
-
reValidateMode,
|
|
24
|
-
shouldFocusError,
|
|
25
|
-
criteriaMode,
|
|
26
|
-
componentRegistry,
|
|
27
|
-
grid,
|
|
28
|
-
...props
|
|
29
|
-
}: FormBaseProps,
|
|
30
|
-
ref: React.Ref<HTMLFormElement>
|
|
31
|
-
) {
|
|
32
|
-
const form = useForm({
|
|
33
|
-
resolver,
|
|
34
|
-
mode,
|
|
35
|
-
defaultValues: initial,
|
|
36
|
-
reValidateMode,
|
|
37
|
-
shouldFocusError,
|
|
38
|
-
criteriaMode,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const handleSubmit = useCallback(
|
|
42
|
-
(data: FormData): void => {
|
|
43
|
-
onSubmit && onSubmit(data);
|
|
44
|
-
},
|
|
45
|
-
[onSubmit]
|
|
46
|
-
);
|
|
47
|
-
const submit = form.handleSubmit(handleSubmit);
|
|
48
|
-
|
|
49
|
-
const ctx = {
|
|
50
|
-
fieldsMap,
|
|
51
|
-
fieldsetsMap,
|
|
52
|
-
control: form.control,
|
|
53
|
-
register: form.register,
|
|
54
|
-
watch: form.watch,
|
|
55
|
-
resetField: form.resetField,
|
|
56
|
-
registerField: registerField,
|
|
57
|
-
errors: form.formState.errors,
|
|
58
|
-
formState: form.formState,
|
|
59
|
-
reset: form.reset,
|
|
60
|
-
trigger: form.trigger,
|
|
61
|
-
getFieldState: form.getFieldState,
|
|
62
|
-
setValue: form.setValue,
|
|
63
|
-
clearErrors: form.clearErrors,
|
|
64
|
-
getValues: form.getValues,
|
|
65
|
-
unregister: form.unregister,
|
|
66
|
-
componentRegistry,
|
|
67
|
-
setError: form.setError,
|
|
68
|
-
formRef: ref,
|
|
69
|
-
submit,
|
|
70
|
-
};
|
|
71
|
-
return (
|
|
72
|
-
<FormContext.Provider value={ctx}>
|
|
73
|
-
<Form grid={grid} onSubmit={submit} ref={ref} {...props}>
|
|
74
|
-
{children}
|
|
75
|
-
</Form>
|
|
76
|
-
</FormContext.Provider>
|
|
77
|
-
);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const useFormContext = () => {
|
|
81
|
-
return useContext(FormContext);
|
|
82
|
-
};
|
|
83
|
-
const useFormValues = () => {
|
|
84
|
-
const ctx = useFormContext();
|
|
85
|
-
return ctx.getValues();
|
|
86
|
-
};
|
|
87
|
-
const FormBuilder = React.forwardRef(function FormBuilder(
|
|
88
|
-
{
|
|
89
|
-
fields = [],
|
|
90
|
-
fieldsets,
|
|
91
|
-
initial = {},
|
|
92
|
-
onSubmit,
|
|
93
|
-
children,
|
|
94
|
-
reValidateMode = 'onSubmit',
|
|
95
|
-
mode = 'onSubmit',
|
|
96
|
-
shouldFocusError = true,
|
|
97
|
-
criteriaMode = 'all',
|
|
98
|
-
auto = false,
|
|
99
|
-
validatorRegistry,
|
|
100
|
-
componentRegistry,
|
|
101
|
-
grid = false,
|
|
102
|
-
...props
|
|
103
|
-
}: FormBuilderProps,
|
|
104
|
-
ref: React.Ref<HTMLFormElement>
|
|
105
|
-
): React.ReactElement {
|
|
106
|
-
const fieldsState = useRef(fields);
|
|
107
|
-
const setFieldsState = useCallback((newFields) => {
|
|
108
|
-
fieldsState.current = newFields;
|
|
109
|
-
}, []);
|
|
110
|
-
const schema = useValidationSchema(fieldsState, validatorRegistry);
|
|
111
|
-
const registerField = useCallback((field) => {
|
|
112
|
-
const fieldIndex = fieldsState.current.findIndex(
|
|
113
|
-
(f) => f.key === field.key
|
|
114
|
-
);
|
|
115
|
-
if (fieldIndex > -1) {
|
|
116
|
-
fieldsState.current[fieldIndex] = field;
|
|
117
|
-
} else {
|
|
118
|
-
fieldsState.current.push(field);
|
|
119
|
-
}
|
|
120
|
-
setFieldsState(fieldsState.current);
|
|
121
|
-
}, []);
|
|
122
|
-
let resolver;
|
|
123
|
-
let fieldsMap;
|
|
124
|
-
let fieldsetsMap;
|
|
125
|
-
let fieldChildren;
|
|
126
|
-
if (schema) {
|
|
127
|
-
resolver = yupResolver(schema);
|
|
128
|
-
fieldsMap = fields
|
|
129
|
-
? fields.reduce((map, field) => ({ ...map, [field.key]: field }), {})
|
|
130
|
-
: {};
|
|
131
|
-
fieldsetsMap =
|
|
132
|
-
fieldsets &&
|
|
133
|
-
fieldsets.reduce(
|
|
134
|
-
(map, fieldset) => ({ ...map, [fieldset.key]: fieldset }),
|
|
135
|
-
{}
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
if (auto) {
|
|
139
|
-
if (fieldsets) {
|
|
140
|
-
fieldChildren = fieldsets.map((fieldset) => (
|
|
141
|
-
<Fieldset key={fieldset.key} />
|
|
142
|
-
));
|
|
143
|
-
} else if (fields) {
|
|
144
|
-
fieldChildren = fields.map((field) => (
|
|
145
|
-
<Field key={field.key} name={field.key} />
|
|
146
|
-
));
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return (
|
|
151
|
-
<FormBase
|
|
152
|
-
resolver={resolver}
|
|
153
|
-
fieldsetsMap={fieldsetsMap}
|
|
154
|
-
fieldsMap={fieldsMap}
|
|
155
|
-
registerField={registerField}
|
|
156
|
-
initial={initial}
|
|
157
|
-
reValidateMode={reValidateMode}
|
|
158
|
-
mode={mode}
|
|
159
|
-
shouldFocusError={shouldFocusError}
|
|
160
|
-
criteriaMode={criteriaMode}
|
|
161
|
-
onSubmit={onSubmit}
|
|
162
|
-
componentRegistry={componentRegistry}
|
|
163
|
-
ref={ref}
|
|
164
|
-
grid={grid}
|
|
165
|
-
{...props}
|
|
166
|
-
>
|
|
167
|
-
{fieldChildren}
|
|
168
|
-
{auto && <Button type="submit">Συνέχεια</Button>}
|
|
169
|
-
{children}
|
|
170
|
-
</FormBase>
|
|
171
|
-
);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
export { FormBase, useFormContext, useFormValues, FormBuilder };
|
|
175
|
-
|
|
176
|
-
export * from '@digigov/form/internal';
|
|
177
|
-
|
|
178
|
-
export default FormBuilder;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Image Input
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# Image input
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Input element of type `image` defines an image as a submit button, i.e. submit buttons that take the form of an image rather than text.
|
|
10
|
-
|
|
11
|
-
## How to use
|
|
12
|
-
|
|
13
|
-
### Default
|
|
14
|
-
|
|
15
|
-
<Story
|
|
16
|
-
packageName="@digigov/form"
|
|
17
|
-
component="inputs/ImageInput"
|
|
18
|
-
story="Default.tsx"
|
|
19
|
-
/>
|