@digigov/form 2.0.0-rc.21 → 2.0.0-rc.22
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/FieldBaseContainer/index.js +1 -1
- package/Field/FieldBaseContainer.js.map +2 -2
- package/FieldArray/index.js +4 -4
- package/FieldArray/index.js.map +2 -2
- package/FieldObject/index.js +2 -2
- 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 -2
- package/FormBuilder/index.js +3 -3
- package/FormBuilder/index.js.map +2 -2
- package/MultiplicityField/add-objects/index.js +4 -4
- package/MultiplicityField/add-objects.js.map +2 -2
- package/MultiplicityField/index.js +12 -10
- package/MultiplicityField/index.js.map +2 -2
- 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/cjs/Field/FieldBaseContainer/index.js +2 -2
- package/cjs/Field/FieldBaseContainer.js.map +3 -3
- package/cjs/FieldArray/index.js +6 -6
- package/cjs/FieldArray/index.js.map +3 -3
- package/cjs/FieldObject/index.js +3 -3
- 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 +5 -5
- package/cjs/FormBuilder/index.js.map +3 -3
- package/cjs/MultiplicityField/add-objects/index.js +8 -8
- package/cjs/MultiplicityField/add-objects.js.map +3 -3
- package/cjs/MultiplicityField/index.js +11 -14
- 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/inputs/AutoCompleteInput/index.js +4 -4
- package/cjs/inputs/AutoCompleteInput/index.js.map +3 -3
- package/cjs/inputs/Checkboxes/index.js +6 -8
- 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/__stories__/WithInvalidImageDimension/index.js +2 -1
- package/cjs/inputs/ImageInput/__stories__/WithInvalidImageDimension.js.map +2 -2
- package/cjs/inputs/ImageInput/index.js +7 -8
- package/cjs/inputs/ImageInput/index.js.map +3 -3
- package/cjs/inputs/Input/index.js +4 -4
- package/cjs/inputs/Input/index.js.map +2 -2
- package/cjs/inputs/Label/index.js +3 -3
- package/cjs/inputs/Label/index.js.map +3 -3
- package/cjs/inputs/Radio/index.js +11 -13
- 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/lazy/index.js +3 -0
- package/cjs/lazy.js.map +2 -2
- package/cjs/registry/index.js +2 -0
- package/cjs/registry.js.map +2 -2
- package/cjs/validators/index.js +6 -65
- 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/index.js +3 -1
- package/cjs/validators/utils/index.js.map +2 -2
- package/index.js +1 -1
- package/inputs/AutoCompleteInput/index.js +6 -4
- package/inputs/AutoCompleteInput/index.js.map +2 -2
- package/inputs/Checkboxes/index.js +7 -5
- 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/__stories__/WithInvalidImageDimension/index.js +2 -1
- package/inputs/ImageInput/__stories__/WithInvalidImageDimension.js.map +2 -2
- package/inputs/ImageInput/index.js +6 -7
- package/inputs/ImageInput/index.js.map +2 -2
- package/inputs/Input/index.js +4 -4
- package/inputs/Input/index.js.map +2 -2
- package/inputs/Label/index.js +2 -2
- package/inputs/Label/index.js.map +2 -2
- package/inputs/Radio/index.js +7 -5
- package/inputs/Radio/index.js.map +2 -2
- package/inputs/Select/index.js +1 -2
- package/inputs/Select/index.js.map +2 -2
- package/lazy/index.js +3 -0
- package/lazy.d.ts +3 -0
- 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/FieldBaseContainer.tsx +1 -1
- package/src/FieldArray/index.tsx +9 -8
- package/src/FieldObject/index.tsx +2 -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 +3 -3
- package/src/MultiplicityField/add-objects.tsx +5 -6
- package/src/MultiplicityField/{MultiplicityField.mdx → doc.mdx} +12 -16
- package/src/MultiplicityField/index.tsx +13 -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/inputs/AutoCompleteInput/{index.mdx → doc.mdx} +2 -13
- package/src/inputs/AutoCompleteInput/index.tsx +31 -26
- package/src/inputs/Checkboxes/{index.mdx → doc.mdx} +5 -15
- package/src/inputs/Checkboxes/index.tsx +29 -27
- 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/__stories__/WithInvalidImageDimension.tsx +2 -0
- package/src/inputs/ImageInput/doc.mdx +23 -0
- package/src/inputs/ImageInput/index.tsx +9 -11
- package/src/inputs/Input/doc.mdx +56 -0
- package/src/inputs/Input/index.tsx +19 -11
- 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/{index.mdx → doc.mdx} +5 -15
- package/src/inputs/Radio/index.tsx +57 -55
- package/src/inputs/Select/{index.mdx → doc.mdx} +1 -5
- package/src/inputs/Select/index.tsx +1 -2
- package/src/installation.mdx +2 -5
- package/src/lazy.js +3 -0
- package/src/registry.js +2 -0
- package/src/validators/index.ts +10 -70
- package/src/validators/utils/date.ts +107 -0
- package/src/validators/utils/index.ts +1 -0
- package/validators/index.js +10 -66
- 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/index.d.ts +1 -0
- package/validators/utils/index.js +1 -0
- package/validators/utils/index.js.map +2 -2
- 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
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { useWatch } from 'react-hook-form';
|
|
3
3
|
import { UncontrolledFieldProps } from '@digigov/form/Field/types';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { TextInput } from '@digigov/react-core/TextInput';
|
|
4
|
+
import { TextArea } from '@digigov/ui/form/TextArea';
|
|
5
|
+
import { TextInput } from '@digigov/ui/form/TextInput';
|
|
7
6
|
import { useTranslation } from '@digigov/ui/i18n';
|
|
7
|
+
import { Hint } from '@digigov/ui/typography/Hint';
|
|
8
|
+
|
|
8
9
|
const TYPES_MAP = {
|
|
9
10
|
string: 'text',
|
|
10
11
|
int: 'text',
|
|
@@ -27,7 +28,7 @@ export interface InputProps extends Omit<UncontrolledFieldProps, 'extra'> {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export const Input: React.ExoticComponent<InputProps> = React.forwardRef(
|
|
30
|
-
function WrappedInput({ name, control, type, extra, ...props }, ref: never) {
|
|
31
|
+
function WrappedInput({ name, control, type, extra, Field, ...props }, ref: never) {
|
|
31
32
|
// eslint-disable-next-line prefer-const
|
|
32
33
|
let { multiline = false, rows, limit } = extra || {};
|
|
33
34
|
const { className } = extra || {};
|
|
@@ -42,17 +43,23 @@ export const Input: React.ExoticComponent<InputProps> = React.forwardRef(
|
|
|
42
43
|
|
|
43
44
|
const getRemainingChars = (currentValue, limit) => {
|
|
44
45
|
if (currentValue) {
|
|
45
|
-
return limit?.max - currentValue.length
|
|
46
|
+
return limit?.max - currentValue.length;
|
|
46
47
|
}
|
|
47
48
|
return limit?.max;
|
|
48
|
-
}
|
|
49
|
+
};
|
|
49
50
|
|
|
50
51
|
const constructRemainingText = (currentValue, limit) => {
|
|
51
52
|
const remainingChars = getRemainingChars(currentValue, limit);
|
|
52
|
-
let remainingText =
|
|
53
|
-
|
|
53
|
+
let remainingText =
|
|
54
|
+
Math.abs(remainingChars) === 1
|
|
55
|
+
? t('form.info.text.character')
|
|
56
|
+
: t('form.info.text.characters');
|
|
57
|
+
remainingText +=
|
|
58
|
+
remainingChars > 0
|
|
59
|
+
? ` ${t('form.info.text.remaining')}.`
|
|
60
|
+
: ` ${t('form.info.text.too_many')}.`;
|
|
54
61
|
return remainingText;
|
|
55
|
-
}
|
|
62
|
+
};
|
|
56
63
|
|
|
57
64
|
if (multiline === true) {
|
|
58
65
|
return (
|
|
@@ -72,8 +79,9 @@ export const Input: React.ExoticComponent<InputProps> = React.forwardRef(
|
|
|
72
79
|
{limit?.max && (
|
|
73
80
|
<Hint display={'flex'}>
|
|
74
81
|
{t('form.info.text.you_have')}
|
|
75
|
-
<Hint fontWeight=
|
|
76
|
-
{`${Math.abs(getRemainingChars(currentValue, limit))}`}
|
|
82
|
+
<Hint fontWeight="bold">
|
|
83
|
+
{`${Math.abs(getRemainingChars(currentValue, limit))}`}
|
|
84
|
+
|
|
77
85
|
</Hint>
|
|
78
86
|
{constructRemainingText(currentValue, limit)}
|
|
79
87
|
</Hint>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Label
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Label
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
The Label component is used to display descriptive text for form sections or fields. It supports both primary and secondary text, allowing for a title and optional subtitle.
|
|
9
|
+
|
|
10
|
+
## How to use
|
|
11
|
+
|
|
12
|
+
### Default
|
|
13
|
+
|
|
14
|
+
<code src="@digigov/form/inputs/Label/__stories__/Default.tsx" />
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { FieldLabelProps } from '@digigov/form/types';
|
|
3
|
-
import
|
|
4
|
-
import LabelTitle from '@digigov/react-core/LabelTitle';
|
|
3
|
+
import { LabelTitle } from '@digigov/ui/form/LabelContainer';
|
|
5
4
|
import { useTranslation } from '@digigov/ui/i18n';
|
|
5
|
+
import { Hint } from '@digigov/ui/typography/Hint';
|
|
6
6
|
|
|
7
7
|
export interface FieldOptionProps {
|
|
8
8
|
label?: FieldLabelProps;
|
|
@@ -39,5 +39,4 @@ export const Label: React.FC<LabelProps> = ({ className, label, value }) => {
|
|
|
39
39
|
);
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
|
|
43
42
|
export default Label;
|
|
@@ -10,24 +10,14 @@ Radios are used to let a user select one option of a limited number of choices.
|
|
|
10
10
|
|
|
11
11
|
### Default
|
|
12
12
|
|
|
13
|
-
<
|
|
14
|
-
|
|
15
|
-
component="inputs/Radio"
|
|
16
|
-
story="Default.tsx"
|
|
17
|
-
/>
|
|
13
|
+
<code src="@digigov/form/inputs/Radio/__stories__/Default.tsx" />
|
|
14
|
+
|
|
18
15
|
|
|
19
16
|
### Conditional
|
|
20
17
|
|
|
21
|
-
<
|
|
22
|
-
|
|
23
|
-
component="inputs/Radio"
|
|
24
|
-
story="Conditional.tsx"
|
|
25
|
-
/>
|
|
18
|
+
<code src="@digigov/form/inputs/Radio/__stories__/Conditional.tsx" />
|
|
19
|
+
|
|
26
20
|
|
|
27
21
|
### With divider
|
|
28
22
|
|
|
29
|
-
<
|
|
30
|
-
packageName="@digigov/form"
|
|
31
|
-
component="inputs/Radio"
|
|
32
|
-
story="WithDivider.tsx"
|
|
33
|
-
/>
|
|
23
|
+
<code src="@digigov/form/inputs/Radio/__stories__/WithDivider.tsx" />
|
|
@@ -2,12 +2,14 @@ import React from 'react';
|
|
|
2
2
|
import { useWatch } from 'react-hook-form';
|
|
3
3
|
import { UncontrolledFieldProps } from '@digigov/form/Field/types';
|
|
4
4
|
import { FieldOptionProps } from '@digigov/form/inputs/Label';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
import {
|
|
6
|
+
RadioContainer,
|
|
7
|
+
RadioItem,
|
|
8
|
+
RadioConditional,
|
|
9
|
+
} from '@digigov/ui/form/RadioContainer';
|
|
10
10
|
import { useTranslation } from '@digigov/ui/i18n';
|
|
11
|
+
import { Hint } from '@digigov/ui/typography/Hint';
|
|
12
|
+
import { Base } from '@digigov/ui/utils/Base';
|
|
11
13
|
|
|
12
14
|
export interface RadioButtonsGroupProps
|
|
13
15
|
extends Omit<UncontrolledFieldProps, 'extra'> {
|
|
@@ -87,55 +89,55 @@ export const ControlledRadioButtonsGroup: React.FC<
|
|
|
87
89
|
Field,
|
|
88
90
|
...props
|
|
89
91
|
}) => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
92
|
+
const currentValue: string | undefined = value;
|
|
93
|
+
const { t } = useTranslation();
|
|
94
|
+
return (
|
|
95
|
+
<RadioContainer className={className} id={name}>
|
|
96
|
+
{options.map(
|
|
97
|
+
({
|
|
98
|
+
label,
|
|
99
|
+
value: v,
|
|
100
|
+
show,
|
|
101
|
+
disabled: optionDisabled,
|
|
102
|
+
selected,
|
|
103
|
+
...option
|
|
104
|
+
}) => (
|
|
105
|
+
<React.Fragment key={`${name}.${v}`}>
|
|
106
|
+
<RadioItem
|
|
107
|
+
onChange={(e) => {
|
|
108
|
+
console.log(e);
|
|
109
|
+
onChange(e);
|
|
110
|
+
}}
|
|
111
|
+
id={`${name}.${v}`}
|
|
112
|
+
value={v}
|
|
113
|
+
checked={currentValue === v}
|
|
114
|
+
disabled={disabled || optionDisabled}
|
|
115
|
+
{...option}
|
|
116
|
+
{...{
|
|
117
|
+
...props,
|
|
118
|
+
reset: undefined,
|
|
119
|
+
required: undefined,
|
|
120
|
+
error: undefined,
|
|
121
|
+
'aria-describedby': undefined,
|
|
122
|
+
type: 'radio',
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
{(label && label.primary && t(label.primary)) || v}
|
|
126
|
+
{label && label.secondary && <Hint>{t(label.secondary)}</Hint>}
|
|
127
|
+
</RadioItem>
|
|
128
|
+
{selected && selected()}
|
|
129
|
+
{currentValue === v && show && (
|
|
130
|
+
<RadioConditional>
|
|
131
|
+
{show.map((fieldName) => (
|
|
132
|
+
<Field name={fieldName} key={fieldName} />
|
|
133
|
+
))}
|
|
134
|
+
</RadioConditional>
|
|
135
|
+
)}
|
|
136
|
+
</React.Fragment>
|
|
137
|
+
)
|
|
138
|
+
)}
|
|
139
|
+
</RadioContainer>
|
|
140
|
+
);
|
|
141
|
+
};
|
|
140
142
|
|
|
141
143
|
export default RadioButtonsGroup;
|
|
@@ -10,8 +10,4 @@ The select element represents a control that provides a menu of options.
|
|
|
10
10
|
|
|
11
11
|
### Default
|
|
12
12
|
|
|
13
|
-
<
|
|
14
|
-
packageName="@digigov/form"
|
|
15
|
-
component="inputs/Select"
|
|
16
|
-
story="Default.tsx"
|
|
17
|
-
/>
|
|
13
|
+
<code src="@digigov/form/inputs/Select/__stories__/Default.tsx" />
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { UncontrolledFieldProps } from '@digigov/form/Field/types';
|
|
3
3
|
import { FieldOptionProps } from '@digigov/form/inputs/Label';
|
|
4
|
-
import SelectContainer from '@digigov/
|
|
5
|
-
import SelectOption from '@digigov/react-core/SelectOption';
|
|
4
|
+
import { SelectContainer, SelectOption } from '@digigov/ui/form/SelectContainer';
|
|
6
5
|
|
|
7
6
|
export interface SelectProps extends Omit<UncontrolledFieldProps, 'extra'> {
|
|
8
7
|
extra: {
|
package/src/installation.mdx
CHANGED
|
@@ -7,12 +7,9 @@ sidebar_label: Installation
|
|
|
7
7
|
import Tabs from '@theme/Tabs';
|
|
8
8
|
import TabItem from '@theme/TabItem';
|
|
9
9
|
import CodeBlock from '@theme/CodeBlock';
|
|
10
|
-
import LeadText from '@site/src/components/LeadText';
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
you use and the application architecture.
|
|
15
|
-
</LeadText>
|
|
11
|
+
This page will help you install Digigov Form depending on the framework that
|
|
12
|
+
you use and the application architecture.
|
|
16
13
|
|
|
17
14
|
We assume that your stack uses some of the following tools.
|
|
18
15
|
|
package/src/lazy.js
CHANGED
|
@@ -50,6 +50,9 @@ export default {
|
|
|
50
50
|
'Questions': lazy(() => import('@digigov/form/Questions/Questions').then((module)=> ({default: module['Questions']}))),
|
|
51
51
|
'QuestionsContext': lazy(() => import('@digigov/form/Questions/QuestionsContext').then((module)=> ({default: module['QuestionsContext']}))),
|
|
52
52
|
'AFM_VALIDATOR': lazy(() => import('@digigov/form/validators/utils/afm').then((module)=> ({default: module['AFM_VALIDATOR']}))),
|
|
53
|
+
'DATE_VALIDATOR': lazy(() => import('@digigov/form/validators/utils/date').then((module)=> ({default: module['DATE_VALIDATOR']}))),
|
|
54
|
+
'EARLIER_THAN_VALIDATOR': lazy(() => import('@digigov/form/validators/utils/date').then((module)=> ({default: module['EARLIER_THAN_VALIDATOR']}))),
|
|
55
|
+
'LATER_THAN_VALIDATOR': lazy(() => import('@digigov/form/validators/utils/date').then((module)=> ({default: module['LATER_THAN_VALIDATOR']}))),
|
|
53
56
|
'FILE_MAX_SIZE_VALIDATOR': lazy(() => import('@digigov/form/validators/utils/file').then((module)=> ({default: module['FILE_MAX_SIZE_VALIDATOR']}))),
|
|
54
57
|
'IBAN_VALIDATOR': lazy(() => import('@digigov/form/validators/utils/iban').then((module)=> ({default: module['IBAN_VALIDATOR']}))),
|
|
55
58
|
'IMAGE_DIMENSION_VALIDATOR': lazy(() => import('@digigov/form/validators/utils/image').then((module)=> ({default: module['IMAGE_DIMENSION_VALIDATOR']}))),
|
package/src/registry.js
CHANGED
|
@@ -55,6 +55,7 @@ import * as _digigov_form_utils from '@digigov/form/utils';
|
|
|
55
55
|
import * as _digigov_form_validators from '@digigov/form/validators';
|
|
56
56
|
import * as _digigov_form_validators_types from '@digigov/form/validators/types';
|
|
57
57
|
import * as _digigov_form_validators_utils_afm from '@digigov/form/validators/utils/afm';
|
|
58
|
+
import * as _digigov_form_validators_utils_date from '@digigov/form/validators/utils/date';
|
|
58
59
|
import * as _digigov_form_validators_utils_file from '@digigov/form/validators/utils/file';
|
|
59
60
|
import * as _digigov_form_validators_utils_iban from '@digigov/form/validators/utils/iban';
|
|
60
61
|
import * as _digigov_form_validators_utils_image from '@digigov/form/validators/utils/image';
|
|
@@ -142,6 +143,7 @@ export default {
|
|
|
142
143
|
'@digigov/form/validators': lazyImport(_digigov_form_validators),
|
|
143
144
|
'@digigov/form/validators/types': lazyImport(_digigov_form_validators_types),
|
|
144
145
|
'@digigov/form/validators/utils/afm': lazyImport(_digigov_form_validators_utils_afm),
|
|
146
|
+
'@digigov/form/validators/utils/date': lazyImport(_digigov_form_validators_utils_date),
|
|
145
147
|
'@digigov/form/validators/utils/file': lazyImport(_digigov_form_validators_utils_file),
|
|
146
148
|
'@digigov/form/validators/utils/iban': lazyImport(_digigov_form_validators_utils_iban),
|
|
147
149
|
'@digigov/form/validators/utils/image': lazyImport(_digigov_form_validators_utils_image),
|
package/src/validators/index.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { MutableRefObject, 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 { AnyObjectSchema, StringSchema } from 'yup';
|
|
6
4
|
import { RequiredArraySchema } from 'yup/lib/array';
|
|
@@ -22,16 +20,12 @@ import {
|
|
|
22
20
|
POSTALCODE_VALIDATOR,
|
|
23
21
|
UUID4_VALIDATOR,
|
|
24
22
|
IMAGE_DIMENSION_VALIDATOR,
|
|
23
|
+
DATE_VALIDATOR,
|
|
24
|
+
LATER_THAN_VALIDATOR,
|
|
25
|
+
EARLIER_THAN_VALIDATOR,
|
|
25
26
|
} from '@digigov/form/validators/utils';
|
|
26
27
|
import { INT_VALIDATOR } from '@digigov/form/validators/utils/int';
|
|
27
28
|
import { NUMBER_VALIDATOR } from '@digigov/form/validators/utils/number';
|
|
28
|
-
dayjs.extend(customParseFormat);
|
|
29
|
-
|
|
30
|
-
const VALID_DATE_FORMATS = ['DD/MM/YYYY'];
|
|
31
|
-
const DATE_CACHE = {};
|
|
32
|
-
const getDate = (v) => {
|
|
33
|
-
return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);
|
|
34
|
-
};
|
|
35
29
|
|
|
36
30
|
export const getYUPTypeMap = (): Record<string, any> => {
|
|
37
31
|
const yupTypeMap = {
|
|
@@ -150,69 +144,15 @@ export const getYUPTypeMap = (): Record<string, any> => {
|
|
|
150
144
|
AnyObject,
|
|
151
145
|
string | null | undefined
|
|
152
146
|
> => yup.string().nullable(),
|
|
153
|
-
date: (field) => {
|
|
154
|
-
|
|
155
|
-
.string()
|
|
156
|
-
.nullable(true)
|
|
157
|
-
.test('date', 'form.error.date.invalid', function (value) {
|
|
158
|
-
if (!value) return true;
|
|
159
|
-
const date = getDate(value);
|
|
160
|
-
return date.isValid();
|
|
161
|
-
});
|
|
162
|
-
const params = field.extra || {};
|
|
163
|
-
if (params.max) {
|
|
164
|
-
let maxDate;
|
|
165
|
-
if (params.max === 'now') {
|
|
166
|
-
const today = new Date();
|
|
167
|
-
maxDate = new Date(today);
|
|
168
|
-
} else {
|
|
169
|
-
maxDate = getDate(params.max).toDate();
|
|
170
|
-
}
|
|
171
|
-
const maxNextDate = new Date(maxDate);
|
|
172
|
-
maxNextDate.setDate(maxDate.getDate() + 1);
|
|
173
|
-
simpleDate = simpleDate.test({
|
|
174
|
-
name: 'earlier-than',
|
|
175
|
-
message: {
|
|
176
|
-
key: 'form.error.date.earlier_than',
|
|
177
|
-
context: {
|
|
178
|
-
maxDate: maxNextDate.toLocaleDateString(),
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
test: function (value) {
|
|
182
|
-
if (!value) return true;
|
|
183
|
-
const date = getDate(value);
|
|
184
|
-
const isValid = +date.toDate() < +maxDate;
|
|
185
|
-
return isValid;
|
|
186
|
-
},
|
|
187
|
-
});
|
|
147
|
+
date: (field): yup.StringSchema => {
|
|
148
|
+
if (field?.extra?.min) {
|
|
149
|
+
return yup.string().test(LATER_THAN_VALIDATOR(field));
|
|
188
150
|
}
|
|
189
|
-
if (
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
minDate = new Date(today);
|
|
194
|
-
} else {
|
|
195
|
-
minDate = getDate(params.min).toDate();
|
|
196
|
-
}
|
|
197
|
-
const minPreviousDate = new Date(minDate);
|
|
198
|
-
minPreviousDate.setDate(minDate.getDate() - 1);
|
|
199
|
-
simpleDate = simpleDate.test({
|
|
200
|
-
name: 'later-than',
|
|
201
|
-
message: {
|
|
202
|
-
key: 'form.error.date.later_than',
|
|
203
|
-
context: {
|
|
204
|
-
minDate: minPreviousDate.toLocaleDateString(),
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
test: function (value) {
|
|
208
|
-
if (!value) return true;
|
|
209
|
-
const date = getDate(value);
|
|
210
|
-
const isValid = +date.toDate() > +minDate;
|
|
211
|
-
return isValid;
|
|
212
|
-
},
|
|
213
|
-
});
|
|
151
|
+
if (field?.extra?.max) {
|
|
152
|
+
return yup.string().test(EARLIER_THAN_VALIDATOR(field));
|
|
153
|
+
} else {
|
|
154
|
+
return yup.string().test(DATE_VALIDATOR);
|
|
214
155
|
}
|
|
215
|
-
return simpleDate;
|
|
216
156
|
},
|
|
217
157
|
};
|
|
218
158
|
return yupTypeMap;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
|
3
|
+
import { FieldSpec } from '@digigov/form/types';
|
|
4
|
+
import { ValidatorSchema } from '@digigov/form/validators/types';
|
|
5
|
+
|
|
6
|
+
dayjs.extend(customParseFormat);
|
|
7
|
+
|
|
8
|
+
const VALID_DATE_FORMATS = ['DD/MM/YYYY'];
|
|
9
|
+
const DATE_CACHE = {};
|
|
10
|
+
|
|
11
|
+
const getDate = (v: string) => {
|
|
12
|
+
return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Function to check partial date completion
|
|
16
|
+
const checkPartialDate = (value: string) => {
|
|
17
|
+
console.log("value.split('/')", value.split('/'));
|
|
18
|
+
const [day, month, year] = value.split('/');
|
|
19
|
+
if (day && !month && !year) {
|
|
20
|
+
return 'form.error.date.invalid_month_year';
|
|
21
|
+
}
|
|
22
|
+
if (!day && month && !year) {
|
|
23
|
+
return 'form.error.date.invalid_day_year';
|
|
24
|
+
}
|
|
25
|
+
if (!day && !month && year) {
|
|
26
|
+
return 'form.error.date.invalid_day_month';
|
|
27
|
+
}
|
|
28
|
+
if (day && month && !year) {
|
|
29
|
+
return 'form.error.date.invalid_year';
|
|
30
|
+
}
|
|
31
|
+
if (day && !month && year) {
|
|
32
|
+
return 'form.error.date.invalid_month';
|
|
33
|
+
}
|
|
34
|
+
if (!day && month && year) {
|
|
35
|
+
return 'form.error.date.invalid_day';
|
|
36
|
+
} else {
|
|
37
|
+
return 'form.error.date.invalid';
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const LATER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {
|
|
42
|
+
let minDate;
|
|
43
|
+
const params = field.extra || {};
|
|
44
|
+
if (params.min === 'now') {
|
|
45
|
+
const today = new Date();
|
|
46
|
+
minDate = new Date(today);
|
|
47
|
+
} else {
|
|
48
|
+
minDate = getDate(params.min).toDate();
|
|
49
|
+
}
|
|
50
|
+
const minPreviousDate = new Date(minDate);
|
|
51
|
+
minPreviousDate.setDate(minDate.getDate() - 1);
|
|
52
|
+
return {
|
|
53
|
+
name: 'later-than',
|
|
54
|
+
message: {
|
|
55
|
+
key: 'form.error.date.later_than',
|
|
56
|
+
context: {
|
|
57
|
+
minDate: minPreviousDate.toLocaleDateString(),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
test: function (value: string) {
|
|
61
|
+
if (!value) return true;
|
|
62
|
+
const date = getDate(value);
|
|
63
|
+
const isValid = +date.toDate() > +minDate;
|
|
64
|
+
return isValid;
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const EARLIER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {
|
|
70
|
+
let maxDate;
|
|
71
|
+
const params = field.extra || {};
|
|
72
|
+
if (params.max === 'now') {
|
|
73
|
+
const today = new Date();
|
|
74
|
+
maxDate = new Date(today);
|
|
75
|
+
} else {
|
|
76
|
+
maxDate = getDate(params.max).toDate();
|
|
77
|
+
}
|
|
78
|
+
const maxNextDate = new Date(maxDate);
|
|
79
|
+
maxNextDate.setDate(maxDate.getDate() + 1);
|
|
80
|
+
return {
|
|
81
|
+
name: 'earlier-than',
|
|
82
|
+
message: {
|
|
83
|
+
key: 'form.error.date.earlier_than',
|
|
84
|
+
context: {
|
|
85
|
+
maxDate: maxNextDate.toLocaleDateString(),
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
test: function (value: string) {
|
|
89
|
+
if (!value) return true;
|
|
90
|
+
const date = getDate(value);
|
|
91
|
+
const isValid = +date.toDate() < +maxDate;
|
|
92
|
+
return isValid;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const DATE_VALIDATOR = {
|
|
98
|
+
name: 'date-validator',
|
|
99
|
+
message: (v): string => {
|
|
100
|
+
return checkPartialDate(v.value);
|
|
101
|
+
},
|
|
102
|
+
test: function (value: string) {
|
|
103
|
+
if (!value) return true;
|
|
104
|
+
const date = getDate(value);
|
|
105
|
+
return date.isValid();
|
|
106
|
+
},
|
|
107
|
+
};
|
|
@@ -11,6 +11,7 @@ export * from '@digigov/form/validators/utils/phone';
|
|
|
11
11
|
export * from '@digigov/form/validators/utils/postal_code';
|
|
12
12
|
export * from '@digigov/form/validators/utils/uuid4';
|
|
13
13
|
export * from '@digigov/form/validators/utils/text_limit';
|
|
14
|
+
export * from '@digigov/form/validators/utils/date';
|
|
14
15
|
|
|
15
16
|
export function getYupField(field: FieldSpec, yupTypeMap: Record<string, any>) {
|
|
16
17
|
const yupField = yupTypeMap[field?.type || 'string'] || yupTypeMap['string'];
|
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) => {
|
|
@@ -97,65 +92,14 @@ const getYUPTypeMap = () => {
|
|
|
97
92
|
"choice:multiple": () => yup.array().of(yup.string()).nullable(),
|
|
98
93
|
"choice:single": () => yup.string().nullable(),
|
|
99
94
|
date: (field) => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const date = getDate(value);
|
|
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
|
-
});
|
|
95
|
+
if (field?.extra?.min) {
|
|
96
|
+
return yup.string().test(LATER_THAN_VALIDATOR(field));
|
|
131
97
|
}
|
|
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
|
-
});
|
|
98
|
+
if (field?.extra?.max) {
|
|
99
|
+
return yup.string().test(EARLIER_THAN_VALIDATOR(field));
|
|
100
|
+
} else {
|
|
101
|
+
return yup.string().test(DATE_VALIDATOR);
|
|
157
102
|
}
|
|
158
|
-
return simpleDate;
|
|
159
103
|
}
|
|
160
104
|
};
|
|
161
105
|
return yupTypeMap;
|