@digigov/form 2.0.0-a402a664 → 2.0.0-abd768eb

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.
Files changed (88) hide show
  1. package/Field/FieldBase/index.js +1 -1
  2. package/Field/FieldBase.js.map +2 -2
  3. package/Field/FieldBaseContainer/index.js +36 -19
  4. package/Field/FieldBaseContainer.d.ts +1 -1
  5. package/Field/FieldBaseContainer.js.map +2 -2
  6. package/FieldArray/index.js +20 -1
  7. package/FieldArray/index.js.map +2 -2
  8. package/FormBuilder/index.js +0 -1
  9. package/FormBuilder/index.js.map +2 -2
  10. package/cjs/Field/FieldBase/index.js +1 -1
  11. package/cjs/Field/FieldBase.js.map +2 -2
  12. package/cjs/Field/FieldBaseContainer/index.js +36 -19
  13. package/cjs/Field/FieldBaseContainer.js.map +2 -2
  14. package/cjs/FieldArray/index.js +19 -0
  15. package/cjs/FieldArray/index.js.map +2 -2
  16. package/cjs/FormBuilder/index.js +0 -1
  17. package/cjs/FormBuilder/index.js.map +2 -2
  18. package/cjs/inputs/Checkboxes/index.js +1 -1
  19. package/cjs/inputs/Checkboxes/index.js.map +2 -2
  20. package/cjs/inputs/DateInput/index.js +26 -11
  21. package/cjs/inputs/DateInput/index.js.map +2 -2
  22. package/cjs/inputs/DateTimeInput/index.js +54 -33
  23. package/cjs/inputs/DateTimeInput/index.js.map +2 -2
  24. package/cjs/inputs/FileInput/index.js +42 -24
  25. package/cjs/inputs/FileInput/index.js.map +2 -2
  26. package/cjs/registry/index.js +4 -4
  27. package/cjs/registry.js.map +2 -2
  28. package/cjs/validators/index.js +1 -1
  29. package/cjs/validators/index.js.map +2 -2
  30. package/cjs/validators/utils/date/index.js +26 -3
  31. package/cjs/validators/utils/date.js.map +2 -2
  32. package/cjs/validators/utils/datetime/index.js +118 -24
  33. package/cjs/validators/utils/datetime.js.map +2 -2
  34. package/index.js +1 -1
  35. package/inputs/Checkboxes/index.js +1 -1
  36. package/inputs/Checkboxes/index.js.map +2 -2
  37. package/inputs/DateInput/DateInput.stories.d.ts +4 -0
  38. package/inputs/DateInput/__stories__/WithDefaultValue.d.ts +3 -0
  39. package/inputs/DateInput/__stories__/WithInitialValue.d.ts +3 -0
  40. package/inputs/DateInput/__stories__/WithWrongDefaultValue.d.ts +3 -0
  41. package/inputs/DateInput/__stories__/WithWrongInitialValue.d.ts +3 -0
  42. package/inputs/DateInput/index.js +27 -12
  43. package/inputs/DateInput/index.js.map +2 -2
  44. package/inputs/DateTimeInput/DateTimeInput.stories.d.ts +8 -3
  45. package/inputs/DateTimeInput/__stories__/WithDefaultValue.d.ts +3 -0
  46. package/inputs/DateTimeInput/__stories__/WithInitialValue.d.ts +3 -0
  47. package/inputs/DateTimeInput/__stories__/WithWrongDefaultValue.d.ts +3 -0
  48. package/inputs/DateTimeInput/__stories__/WithWrongInitialValue.d.ts +3 -0
  49. package/inputs/DateTimeInput/index.js +55 -34
  50. package/inputs/DateTimeInput/index.js.map +2 -2
  51. package/inputs/FileInput/index.d.ts +5 -1
  52. package/inputs/FileInput/index.js +43 -25
  53. package/inputs/FileInput/index.js.map +2 -2
  54. package/package.json +4 -4
  55. package/registry/index.js +4 -4
  56. package/src/Field/FieldBase.tsx +1 -1
  57. package/src/Field/FieldBaseContainer.tsx +67 -51
  58. package/src/FieldArray/index.tsx +28 -1
  59. package/src/FormBuilder/index.tsx +0 -1
  60. package/src/FormBuilder/scenarios.test.tsx +759 -1
  61. package/src/inputs/Checkboxes/index.tsx +1 -5
  62. package/src/inputs/DateInput/DateInput.stories.js +4 -0
  63. package/src/inputs/DateInput/__stories__/WithDefaultValue.tsx +26 -0
  64. package/src/inputs/DateInput/__stories__/WithInitialValue.tsx +28 -0
  65. package/src/inputs/DateInput/__stories__/WithWrongDefaultValue.tsx +26 -0
  66. package/src/inputs/DateInput/__stories__/WithWrongInitialValue.tsx +28 -0
  67. package/src/inputs/DateInput/index.test.tsx +16 -0
  68. package/src/inputs/DateInput/index.tsx +30 -12
  69. package/src/inputs/DateTimeInput/DateTimeInput.stories.js +9 -6
  70. package/src/inputs/DateTimeInput/__stories__/WithDefaultValue.tsx +26 -0
  71. package/src/inputs/DateTimeInput/__stories__/WithInitialValue.tsx +28 -0
  72. package/src/inputs/DateTimeInput/__stories__/WithWrongDefaultValue.tsx +26 -0
  73. package/src/inputs/DateTimeInput/__stories__/WithWrongInitialValue.tsx +28 -0
  74. package/src/inputs/DateTimeInput/index.test.tsx +16 -0
  75. package/src/inputs/DateTimeInput/index.tsx +58 -34
  76. package/src/inputs/FileInput/index.tsx +70 -39
  77. package/src/registry.js +4 -4
  78. package/src/validators/index.ts +1 -1
  79. package/src/validators/utils/date.ts +28 -4
  80. package/src/validators/utils/datetime.ts +121 -29
  81. package/validators/index.js +1 -1
  82. package/validators/index.js.map +2 -2
  83. package/validators/utils/date/index.js +26 -3
  84. package/validators/utils/date.d.ts +1 -1
  85. package/validators/utils/date.js.map +2 -2
  86. package/validators/utils/datetime/index.js +118 -24
  87. package/validators/utils/datetime.d.ts +1 -1
  88. package/validators/utils/datetime.js.map +2 -2
@@ -32,14 +32,10 @@ export const Checkboxes: React.FC<CheckboxesProps> = ({
32
32
  const handleChange =
33
33
  (optionValue, idx, show) =>
34
34
  (evt): void => {
35
- let newValue;
35
+ let newValue: any = [];
36
36
  if (evt.currentTarget.checked) {
37
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
38
- // @ts-ignore
39
37
  newValue = value.concat([optionValue]);
40
38
  } else {
41
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
42
- // @ts-ignore
43
39
  newValue = value.filter((val) => val !== optionValue);
44
40
  }
45
41
  if (show && show.length > 0) {
@@ -8,3 +8,7 @@ export default {
8
8
  displayName: 'DateInput',
9
9
  };
10
10
  export { Default } from '@digigov/form/inputs/DateInput/__stories__/Default';
11
+ export { WithDefaultValue } from '@digigov/form/inputs/DateInput/__stories__/WithDefaultValue';
12
+ export { WithWrongDefaultValue } from '@digigov/form/inputs/DateInput/__stories__/WithWrongDefaultValue';
13
+ export { WithInitialValue } from '@digigov/form/inputs/DateInput/__stories__/WithInitialValue';
14
+ export { WithWrongInitialValue } from '@digigov/form/inputs/DateInput/__stories__/WithWrongInitialValue';
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'date_example',
8
+ type: 'date',
9
+ required: true,
10
+ defaultValue: '05/11/2020',
11
+ label: {
12
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
13
+ secondary: 'Για παράδειγμα, 05 11 2020',
14
+ },
15
+ } as const,
16
+ ];
17
+
18
+ export const WithDefaultValue = (_: any) => (
19
+ <FormBuilder onSubmit={(data) => console.log(data)}>
20
+ {fields.map(({ key, ...field }) => {
21
+ return <Field {...field} name={key} key={key} />;
22
+ })}
23
+ <Button type="submit">Συνέχεια</Button>
24
+ </FormBuilder>
25
+ );
26
+ export default WithDefaultValue;
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'date_example',
8
+ type: 'date',
9
+ required: true,
10
+ label: {
11
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
12
+ secondary: 'Για παράδειγμα, 05 11 2020',
13
+ },
14
+ } as const,
15
+ ];
16
+
17
+ export const WithInitialValue = (_: any) => (
18
+ <FormBuilder
19
+ initial={{ date_example: '05/01/2020' }}
20
+ onSubmit={(data) => console.log(data)}
21
+ >
22
+ {fields.map(({ key, ...field }) => {
23
+ return <Field {...field} name={key} key={key} />;
24
+ })}
25
+ <Button type="submit">Συνέχεια</Button>
26
+ </FormBuilder>
27
+ );
28
+ export default WithInitialValue;
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'date_example',
8
+ type: 'date',
9
+ required: true,
10
+ defaultValue: '5/1/2020',
11
+ label: {
12
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
13
+ secondary: 'Για παράδειγμα, 05 11 2020',
14
+ },
15
+ } as const,
16
+ ];
17
+
18
+ export const WithWrongDefaultValue = (_: any) => (
19
+ <FormBuilder onSubmit={(data) => console.log(data)}>
20
+ {fields.map(({ key, ...field }) => {
21
+ return <Field {...field} name={key} key={key} />;
22
+ })}
23
+ <Button type="submit">Συνέχεια</Button>
24
+ </FormBuilder>
25
+ );
26
+ export default WithWrongDefaultValue;
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'date_example',
8
+ type: 'date',
9
+ required: true,
10
+ label: {
11
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
12
+ secondary: 'Για παράδειγμα, 05 11 2020',
13
+ },
14
+ } as const,
15
+ ];
16
+
17
+ export const WithWrongInitialValue = (_: any) => (
18
+ <FormBuilder
19
+ initial={{ date_example: '5/1/2020' }}
20
+ onSubmit={(data) => console.log(data)}
21
+ >
22
+ {fields.map(({ key, ...field }) => {
23
+ return <Field {...field} name={key} key={key} />;
24
+ })}
25
+ <Button type="submit">Συνέχεια</Button>
26
+ </FormBuilder>
27
+ );
28
+ export default WithWrongInitialValue;
@@ -2,6 +2,10 @@ import React from 'react';
2
2
  import { test, expect } from '@playwright/experimental-ct-react';
3
3
  import TestVariant from '@digigov/ui/utils/TestVariant'
4
4
  import { Default } from '@digigov/form/inputs/DateInput/__stories__/Default';
5
+ import { WithDefaultValue } from '@digigov/form/inputs/DateInput/__stories__/WithDefaultValue';
6
+ import { WithInitialValue } from '@digigov/form/inputs/DateInput/__stories__/WithInitialValue';
7
+ import { WithWrongDefaultValue } from '@digigov/form/inputs/DateInput/__stories__/WithWrongDefaultValue';
8
+ import { WithWrongInitialValue } from '@digigov/form/inputs/DateInput/__stories__/WithWrongInitialValue';
5
9
 
6
10
  test('renders the All DateInput variants', async ({ mount, page }) => {
7
11
  await mount(
@@ -10,6 +14,18 @@ test('renders the All DateInput variants', async ({ mount, page }) => {
10
14
  <TestVariant title="Default">
11
15
  <Default />
12
16
  </TestVariant>
17
+ <TestVariant title="WithDefaultValue">
18
+ <WithDefaultValue />
19
+ </TestVariant>
20
+ <TestVariant title="WithInitialValue">
21
+ <WithInitialValue />
22
+ </TestVariant>
23
+ <TestVariant title="WithWrongDefaultValue">
24
+ <WithWrongDefaultValue />
25
+ </TestVariant>
26
+ <TestVariant title="WithWrongInitialValue">
27
+ <WithWrongInitialValue />
28
+ </TestVariant>
13
29
  </div>
14
30
  )
15
31
  await page.evaluate(() => document.fonts.ready);
@@ -1,4 +1,4 @@
1
- import React, { useMemo, useState } from 'react';
1
+ import React, { useMemo, useEffect } from 'react';
2
2
  import dayjs from 'dayjs';
3
3
  import customParseFormat from 'dayjs/plugin/customParseFormat';
4
4
  import { UncontrolledFieldProps } from '@digigov/form/Field/types';
@@ -20,7 +20,6 @@ export interface DateInputProps extends Omit<UncontrolledFieldProps, 'extra'> {
20
20
  function useDate(name, value, form) {
21
21
  const [year, month, day] = useMemo(() => {
22
22
  if (!value || value.length === 0) {
23
- update('');
24
23
  return ['', '', ''];
25
24
  }
26
25
  const [day, month, year] = value.split('/');
@@ -94,23 +93,37 @@ const DatePart = ({ label, ...props }) => {
94
93
 
95
94
  export const DateInput = ({ name, ...props }) => {
96
95
  const { t } = useTranslation();
97
- const [initial] = useState(props.value);
98
- const value = useMemo(() => {
99
- if (initial === props.value) {
100
- return makeDate(props.value);
96
+ const form = useFormContext();
97
+
98
+ useEffect(() => {
99
+ const initialValue = form.getValues(name);
100
+ // checks if the initial value is undefined and if the default value is valid date
101
+ if (initialValue === undefined && props.defaultValue) {
102
+ form.setValue(name, makeDate(props.defaultValue), {
103
+ shouldValidate: false,
104
+ });
105
+ } else {
106
+ // checks if the initial value is a valid date
107
+ form.setValue(name, makeDate(initialValue), {
108
+ shouldValidate: false,
109
+ });
101
110
  }
102
- return props.value;
103
- }, [props.value, initial]);
111
+ }, []);
104
112
 
105
- const form = useFormContext();
106
- const date = useDate(name, value || makeDate(props.defaultValue), form);
113
+ const value = form.getValues(name);
114
+ const date = useDate(name, value, form);
107
115
 
108
116
  return (
109
117
  <DateInputContainer>
110
118
  <DatePart
111
119
  label={t('form.label.day')}
112
120
  onChange={date.setDay}
113
- onBlur={date.trigger}
121
+ onBlur={(e) => {
122
+ if (date.day && date.day.length === 1) {
123
+ date.setDay(e, true);
124
+ }
125
+ date.trigger();
126
+ }}
114
127
  value={date.day}
115
128
  width={2}
116
129
  name={`${name}-day`}
@@ -123,7 +136,12 @@ export const DateInput = ({ name, ...props }) => {
123
136
  <DatePart
124
137
  label={t('form.label.month')}
125
138
  onChange={date.setMonth}
126
- onBlur={date.trigger}
139
+ onBlur={(e) => {
140
+ if (date.month && date.month.length === 1) {
141
+ date.setMonth(e, true);
142
+ }
143
+ date.trigger();
144
+ }}
127
145
  value={date.month}
128
146
  width={2}
129
147
  name={`${name}-month`}
@@ -1,11 +1,14 @@
1
- import { Default } from './__stories__/Default';
1
+ import DateTimeInput from '@digigov/form/inputs/DateTimeInput';
2
2
  import doc from './doc.mdx?raw';
3
-
4
3
  export default {
5
- title: 'Form/Inputs/DateTimeInput',
6
- component: Default,
4
+ title: 'Digigov Form/inputs/DateTimeInput',
5
+ component: DateTimeInput,
7
6
  tags: ['autodocs'],
8
7
  markdown: doc,
8
+ displayName: 'DateTimeInput',
9
9
  };
10
-
11
- export { Default };
10
+ export { Default } from '@digigov/form/inputs/DateTimeInput/__stories__/Default';
11
+ export { WithDefaultValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithDefaultValue';
12
+ export { WithWrongDefaultValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithWrongDefaultValue';
13
+ export { WithInitialValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithInitialValue';
14
+ export { WithWrongInitialValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithWrongInitialValue';
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'datetime_example',
8
+ type: 'datetime',
9
+ defaultValue: '05/11/2020 13:30',
10
+ required: true,
11
+ label: {
12
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
13
+ secondary: 'Για παράδειγμα, 05 11 2020 13:30',
14
+ },
15
+ } as const,
16
+ ];
17
+
18
+ export const WithDefaultValue = (_: any) => (
19
+ <FormBuilder onSubmit={(data) => console.log(data)}>
20
+ {fields.map(({ key, ...field }) => {
21
+ return <Field {...field} name={key} key={key} />;
22
+ })}
23
+ <Button type="submit">Συνέχεια</Button>
24
+ </FormBuilder>
25
+ );
26
+ export default WithDefaultValue;
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'date_example',
8
+ type: 'datetime',
9
+ required: true,
10
+ label: {
11
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
12
+ secondary: 'Για παράδειγμα, 05 11 2020',
13
+ },
14
+ } as const,
15
+ ];
16
+
17
+ export const WithInitialValue = (_: any) => (
18
+ <FormBuilder
19
+ initial={{ date_example: '05/01/2020 13:30' }}
20
+ onSubmit={(data) => console.log(data)}
21
+ >
22
+ {fields.map(({ key, ...field }) => {
23
+ return <Field {...field} name={key} key={key} />;
24
+ })}
25
+ <Button type="submit">Συνέχεια</Button>
26
+ </FormBuilder>
27
+ );
28
+ export default WithInitialValue;
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'datetime_example',
8
+ type: 'datetime',
9
+ defaultValue: '5/1/2020 3:3',
10
+ required: true,
11
+ label: {
12
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
13
+ secondary: 'Για παράδειγμα, 05 11 2020 13:30',
14
+ },
15
+ } as const,
16
+ ];
17
+
18
+ export const WithWrongDefaultValue = (_: any) => (
19
+ <FormBuilder onSubmit={(data) => console.log(data)}>
20
+ {fields.map(({ key, ...field }) => {
21
+ return <Field {...field} name={key} key={key} />;
22
+ })}
23
+ <Button type="submit">Συνέχεια</Button>
24
+ </FormBuilder>
25
+ );
26
+ export default WithWrongDefaultValue;
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import FormBuilder, { Field } from '@digigov/form';
3
+ import { Button } from '@digigov/ui/form/Button';
4
+
5
+ const fields = [
6
+ {
7
+ key: 'date_example',
8
+ type: 'datetime',
9
+ required: true,
10
+ label: {
11
+ primary: 'Πότε εκδόθηκε το διαβατήριο σας;',
12
+ secondary: 'Για παράδειγμα, 05 11 2020',
13
+ },
14
+ } as const,
15
+ ];
16
+
17
+ export const WithWrongInitialValue = (_: any) => (
18
+ <FormBuilder
19
+ initial={{ date_example: '5/1/2020 3:3' }}
20
+ onSubmit={(data) => console.log(data)}
21
+ >
22
+ {fields.map(({ key, ...field }) => {
23
+ return <Field {...field} name={key} key={key} />;
24
+ })}
25
+ <Button type="submit">Συνέχεια</Button>
26
+ </FormBuilder>
27
+ );
28
+ export default WithWrongInitialValue;
@@ -2,6 +2,10 @@ import React from 'react';
2
2
  import { test, expect } from '@playwright/experimental-ct-react';
3
3
  import TestVariant from '@digigov/ui/utils/TestVariant'
4
4
  import { Default } from '@digigov/form/inputs/DateTimeInput/__stories__/Default';
5
+ import { WithDefaultValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithDefaultValue';
6
+ import { WithInitialValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithInitialValue';
7
+ import { WithWrongDefaultValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithWrongDefaultValue';
8
+ import { WithWrongInitialValue } from '@digigov/form/inputs/DateTimeInput/__stories__/WithWrongInitialValue';
5
9
 
6
10
  test('renders the All DateTimeInput variants', async ({ mount, page }) => {
7
11
  await mount(
@@ -10,6 +14,18 @@ test('renders the All DateTimeInput variants', async ({ mount, page }) => {
10
14
  <TestVariant title="Default">
11
15
  <Default />
12
16
  </TestVariant>
17
+ <TestVariant title="WithDefaultValue">
18
+ <WithDefaultValue />
19
+ </TestVariant>
20
+ <TestVariant title="WithInitialValue">
21
+ <WithInitialValue />
22
+ </TestVariant>
23
+ <TestVariant title="WithWrongDefaultValue">
24
+ <WithWrongDefaultValue />
25
+ </TestVariant>
26
+ <TestVariant title="WithWrongInitialValue">
27
+ <WithWrongInitialValue />
28
+ </TestVariant>
13
29
  </div>
14
30
  )
15
31
  await page.evaluate(() => document.fonts.ready);
@@ -1,4 +1,4 @@
1
- import React, { useMemo, useState } from 'react';
1
+ import React, { useMemo, useEffect } from 'react';
2
2
  import dayjs from 'dayjs';
3
3
  import customParseFormat from 'dayjs/plugin/customParseFormat';
4
4
  import { UncontrolledFieldProps } from '@digigov/form/Field/types';
@@ -21,7 +21,6 @@ export interface DateTimeInputProps
21
21
  function useDateTime(name, value, form) {
22
22
  const [year, month, day, hours, minutes] = useMemo(() => {
23
23
  if (!value || value.length === 0) {
24
- update('');
25
24
  return ['', '', '', '', ''];
26
25
  }
27
26
  const [datePart, timePart] = value.split(' ');
@@ -120,87 +119,112 @@ const DateTimePart = ({ label, ...props }) => {
120
119
 
121
120
  export const DateTimeInput = ({ name, ...props }) => {
122
121
  const { t } = useTranslation();
123
- const [initial] = useState(props.value);
124
- const value = useMemo(() => {
125
- if (initial === props.value) {
126
- return makeDateTime(props.value);
122
+ const form = useFormContext();
123
+
124
+ useEffect(() => {
125
+ const initialValue = form.getValues(name);
126
+ // checks if the initial value is undefined and if the default value is valid date
127
+ if (initialValue === undefined && props.defaultValue) {
128
+ form.setValue(name, makeDateTime(props.defaultValue), {
129
+ shouldValidate: false,
130
+ });
131
+ } else {
132
+ // checks if the initial value is a valid date
133
+ form.setValue(name, makeDateTime(initialValue), {
134
+ shouldValidate: false,
135
+ });
127
136
  }
128
- return props.value;
129
- }, [props.value, initial]);
137
+ }, []);
130
138
 
131
- const form = useFormContext();
132
- const datetime = useDateTime(
133
- name,
134
- value || makeDateTime(props.defaultValue),
135
- form
136
- );
139
+ const value = form.getValues(name);
140
+ const dateTime = useDateTime(name, value, form);
137
141
 
138
142
  return (
139
143
  <DateInputContainer>
140
144
  <DateTimePart
141
145
  label={t('form.label.day')}
142
- onChange={datetime.setDay}
143
- onBlur={datetime.trigger}
144
- value={datetime.day}
146
+ onChange={dateTime.setDay}
147
+ onBlur={(e) => {
148
+ if (dateTime.day && dateTime.day.length === 1) {
149
+ dateTime.setDay(e, true);
150
+ }
151
+ dateTime.trigger();
152
+ }}
153
+ value={dateTime.day}
145
154
  width={2}
146
155
  name={`${name}-day`}
147
156
  maxLength="2"
148
157
  disabled={props.disabled}
149
158
  aria-required={props['aria-required']}
150
- error={!datetime.day ? props.error : false}
159
+ error={!dateTime.day ? props.error : false}
151
160
  id={`${name}-day`}
152
161
  />
153
162
  <DateTimePart
154
163
  label={t('form.label.month')}
155
- onChange={datetime.setMonth}
156
- onBlur={datetime.trigger}
157
- value={datetime.month}
164
+ onChange={dateTime.setMonth}
165
+ onBlur={(e) => {
166
+ if (dateTime.month && dateTime.month.length === 1) {
167
+ dateTime.setMonth(e, true);
168
+ }
169
+ dateTime.trigger();
170
+ }}
171
+ value={dateTime.month}
158
172
  width={2}
159
173
  name={`${name}-month`}
160
174
  id={`${name}-month`}
161
175
  maxLength="2"
162
176
  disabled={props.disabled}
163
177
  aria-required={props['aria-required']}
164
- error={!datetime.month ? props.error : false}
178
+ error={!dateTime.month ? props.error : false}
165
179
  />
166
180
  <DateTimePart
167
181
  label={t('form.label.year')}
168
- onChange={datetime.setYear}
169
- onBlur={datetime.trigger}
170
- value={datetime.year}
182
+ onChange={dateTime.setYear}
183
+ onBlur={dateTime.trigger}
184
+ value={dateTime.year}
171
185
  width={4}
172
186
  name={`${name}-year`}
173
187
  id={`${name}-year`}
174
188
  maxLength="4"
175
189
  disabled={props.disabled}
176
190
  aria-required={props['aria-required']}
177
- error={!datetime.year ? props.error : false}
191
+ error={!dateTime.year ? props.error : false}
178
192
  />
179
193
  <DateTimePart
180
194
  label={t('form.label.hours')}
181
- onChange={datetime.setHours}
182
- onBlur={datetime.trigger}
183
- value={datetime.hours}
195
+ onChange={dateTime.setHours}
196
+ onBlur={(e) => {
197
+ if (dateTime.hours && dateTime.hours.length === 1) {
198
+ dateTime.setHours(e, true);
199
+ }
200
+ dateTime.trigger();
201
+ }}
202
+ value={dateTime.hours}
184
203
  width={2}
185
204
  name={`${name}-hours`}
186
205
  id={`${name}-hours`}
187
206
  maxLength="2"
188
207
  disabled={props.disabled}
189
208
  aria-required={props['aria-required']}
190
- error={!datetime.hours ? props.error : false}
209
+ error={!dateTime.hours ? props.error : false}
191
210
  />
192
211
  <DateTimePart
193
212
  label={t('form.label.minutes')}
194
- onChange={datetime.setMinutes}
195
- onBlur={datetime.trigger}
196
- value={datetime.minutes}
213
+ onChange={dateTime.setMinutes}
214
+ onBlur={(e) => {
215
+ if (dateTime.minutes && dateTime.minutes.length === 1) {
216
+ dateTime.setMinutes(e, true);
217
+ }
218
+ dateTime.trigger();
219
+ }}
220
+ value={dateTime.minutes}
197
221
  width={2}
198
222
  name={`${name}-minutes`}
199
223
  id={`${name}-minutes`}
200
224
  maxLength="2"
201
225
  disabled={props.disabled}
202
226
  aria-required={props['aria-required']}
203
- error={!datetime.minutes ? props.error : false}
227
+ error={!dateTime.minutes ? props.error : false}
204
228
  />
205
229
  </DateInputContainer>
206
230
  );