@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
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { ComponentRef } from 'react';
2
2
  import { FieldContainerProps } from '@digigov/form/Field/types';
3
3
  import Label from '@digigov/form/inputs/Label';
4
4
  import { ErrorMessage } from '@digigov/react-core/ErrorMessage';
@@ -9,56 +9,72 @@ import LabelContainer from '@digigov/react-core/LabelContainer';
9
9
  import { useTranslation } from '@digigov/ui/i18n';
10
10
  import { Hint } from '@digigov/ui/typography/Hint';
11
11
 
12
- export const FieldBaseContainer: React.FC<FieldContainerProps> = ({
13
- name,
14
- wrapper,
15
- label,
16
- children,
17
- error,
18
- hasError,
19
- layout,
20
- controlled,
21
- labelSize = 'sm',
22
- }) => {
23
- const { t } = useTranslation();
24
- const errorMessage = error?.message.key
25
- ? error.message.key
26
- : error?.message || '';
27
- const errorContext = error?.message.context || {};
28
- if (wrapper === 'fieldset') {
29
- return (
30
- <FieldContainer error={hasError} {...layout}>
31
- <Fieldset {...(controlled && { tabIndex: -1 })}>
32
- <FieldsetLegend size={labelSize}>
33
- {label && label.primary}
34
- </FieldsetLegend>
35
- {label && label.secondary && <Hint>{t(label.secondary)}</Hint>}
36
- {error && (
37
- <ErrorMessage id={`${name}-error`}>
38
- {t(errorMessage, errorContext)}
39
- </ErrorMessage>
40
- )}
41
- {children}
42
- {label && label.hint && <Hint size="sm">{label.hint}</Hint>}
43
- </Fieldset>
44
- </FieldContainer>
45
- );
46
- } else {
47
- return (
48
- <FieldContainer error={hasError} {...layout}>
49
- <LabelContainer>
50
- {label && <Label label={label} />}
51
- {error && (
52
- <ErrorMessage id={`${name}-error`}>
53
- {t(errorMessage, errorContext)}
54
- </ErrorMessage>
55
- )}
56
- {children}
57
- {label && label.hint && <Hint size="sm">{label.hint}</Hint>}
58
- </LabelContainer>
59
- </FieldContainer>
60
- );
12
+ export const FieldBaseContainer = React.forwardRef<
13
+ ComponentRef<typeof Fieldset>,
14
+ FieldContainerProps
15
+ >(
16
+ (
17
+ {
18
+ name,
19
+ wrapper,
20
+ label,
21
+ children,
22
+ error,
23
+ hasError,
24
+ layout,
25
+ controlled,
26
+ labelSize = 'sm',
27
+ },
28
+ ref
29
+ ) => {
30
+ const { t } = useTranslation();
31
+ const errorMessage = error?.message.key
32
+ ? error.message.key
33
+ : error?.message || '';
34
+ const errorContext = error?.message.context || {};
35
+ if (wrapper === 'fieldset') {
36
+ return (
37
+ <FieldContainer error={hasError} {...layout}>
38
+ <Fieldset
39
+ ref={ref}
40
+ className="outline-none"
41
+ {...(controlled && {
42
+ tabIndex: -1,
43
+ })}
44
+ >
45
+ <FieldsetLegend size={labelSize}>
46
+ {label && label.primary}
47
+ </FieldsetLegend>
48
+ {label && label.secondary && <Hint>{t(label.secondary)}</Hint>}
49
+ {error && (
50
+ <ErrorMessage id={`${name}-error`}>
51
+ {t(errorMessage, errorContext)}
52
+ </ErrorMessage>
53
+ )}
54
+ {children}
55
+ {label && label.hint && <Hint size="sm">{label.hint}</Hint>}
56
+ </Fieldset>
57
+ </FieldContainer>
58
+ );
59
+ } else {
60
+ return (
61
+ <FieldContainer error={hasError} {...layout}>
62
+ <LabelContainer>
63
+ {label && <Label label={label} />}
64
+ {error && (
65
+ <ErrorMessage id={`${name}-error`}>
66
+ {t(errorMessage, errorContext)}
67
+ </ErrorMessage>
68
+ )}
69
+ {children}
70
+ {label && label.hint && <Hint size="sm">{label.hint}</Hint>}
71
+ </LabelContainer>
72
+ </FieldContainer>
73
+ );
74
+ }
61
75
  }
62
- };
76
+ );
77
+
78
+ FieldBaseContainer.displayName = 'FieldBaseContainer';
63
79
 
64
80
  export default FieldBaseContainer;
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { useEffect, useRef } from 'react';
2
2
  import { ErrorOption, useFieldArray, UseFormReturn } from 'react-hook-form';
3
3
  import { FieldBaseContainer } from '@digigov/form/Field/FieldBaseContainer';
4
4
  import { FieldProps } from '@digigov/form/Field/types';
@@ -43,11 +43,38 @@ export const FieldArray: React.FC<FieldArrayProps> = ({
43
43
  name,
44
44
  });
45
45
  const fields = getValues(name) || [];
46
+
47
+ /** The ref of the FieldBaseContainer. */
48
+ const containerRef = useRef<HTMLFieldSetElement | null>(null);
49
+
50
+ const { errors } = formState;
51
+
52
+ // Focus the fieldset when there are errors related to this field array
53
+ useEffect(() => {
54
+ if (!containerRef.current) return;
55
+ const fieldErrors = Object.keys(errors);
56
+ // no errors
57
+ if (!fieldErrors.length) return;
58
+
59
+ if (
60
+ // Check if there are errors related to the current field array
61
+ fieldErrors.some((key) => {
62
+ const fieldArrayName = key.split('.')[0];
63
+ return fieldArrayName === name;
64
+ })
65
+ ) {
66
+ if (containerRef.current && errors[name]) {
67
+ containerRef.current.focus();
68
+ }
69
+ }
70
+ }, [errors, name]);
71
+
46
72
  return (
47
73
  <FieldBaseContainer
48
74
  label={label}
49
75
  layout={layout}
50
76
  register={register}
77
+ ref={containerRef}
51
78
  error={error?.message && error ? error : undefined}
52
79
  hasError={formState.isSubmitted && !formState.isSubmitting && !!error}
53
80
  wrapper="fieldset"
@@ -44,7 +44,6 @@ const FormBase = React.forwardRef(function FormBase(
44
44
  const handleSubmit = useCallback(
45
45
  (data: FormData): void => {
46
46
  if (onSubmit) {
47
- form.reset(data);
48
47
  onSubmit(data);
49
48
  }
50
49
  },