@ultraviolet/form 2.14.7 → 3.0.0

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 (86) hide show
  1. package/dist/components/CheckboxField/index.cjs +5 -3
  2. package/dist/components/CheckboxField/index.d.ts +2 -2
  3. package/dist/components/CheckboxField/index.js +5 -3
  4. package/dist/components/CheckboxGroupField/index.cjs +12 -8
  5. package/dist/components/CheckboxGroupField/index.d.ts +2 -2
  6. package/dist/components/CheckboxGroupField/index.js +8 -4
  7. package/dist/components/DateField/index.cjs +7 -6
  8. package/dist/components/DateField/index.d.ts +2 -2
  9. package/dist/components/DateField/index.js +7 -6
  10. package/dist/components/Form/index.cjs +7 -47
  11. package/dist/components/Form/index.d.ts +8 -64
  12. package/dist/components/Form/index.js +9 -47
  13. package/dist/components/NumberInputField/index.cjs +5 -3
  14. package/dist/components/NumberInputField/index.d.ts +2 -2
  15. package/dist/components/NumberInputField/index.js +5 -3
  16. package/dist/components/NumberInputFieldV2/index.cjs +5 -3
  17. package/dist/components/NumberInputFieldV2/index.d.ts +2 -4
  18. package/dist/components/NumberInputFieldV2/index.js +5 -3
  19. package/dist/components/RadioField/index.cjs +5 -3
  20. package/dist/components/RadioField/index.d.ts +2 -2
  21. package/dist/components/RadioField/index.js +5 -3
  22. package/dist/components/RadioGroupField/index.cjs +5 -3
  23. package/dist/components/RadioGroupField/index.d.ts +2 -2
  24. package/dist/components/RadioGroupField/index.js +5 -3
  25. package/dist/components/SelectInputField/index.cjs +7 -7
  26. package/dist/components/SelectInputField/index.d.ts +2 -2
  27. package/dist/components/SelectInputField/index.js +3 -3
  28. package/dist/components/SelectInputFieldV2/index.cjs +7 -5
  29. package/dist/components/SelectInputFieldV2/index.d.ts +2 -2
  30. package/dist/components/SelectInputFieldV2/index.js +5 -3
  31. package/dist/components/SelectableCardField/index.cjs +4 -2
  32. package/dist/components/SelectableCardField/index.d.ts +2 -2
  33. package/dist/components/SelectableCardField/index.js +4 -2
  34. package/dist/components/SelectableCardGroupField/index.cjs +5 -3
  35. package/dist/components/SelectableCardGroupField/index.d.ts +2 -2
  36. package/dist/components/SelectableCardGroupField/index.js +5 -3
  37. package/dist/components/TagInputField/index.cjs +5 -3
  38. package/dist/components/TagInputField/index.d.ts +2 -2
  39. package/dist/components/TagInputField/index.js +5 -3
  40. package/dist/components/TextAreaField/index.cjs +6 -3
  41. package/dist/components/TextAreaField/index.d.ts +2 -2
  42. package/dist/components/TextAreaField/index.js +6 -3
  43. package/dist/components/TextInputField/index.cjs +15 -14
  44. package/dist/components/TextInputField/index.d.ts +2 -2
  45. package/dist/components/TextInputField/index.js +15 -14
  46. package/dist/components/TextInputFieldV2/index.cjs +6 -3
  47. package/dist/components/TextInputFieldV2/index.d.ts +2 -2
  48. package/dist/components/TextInputFieldV2/index.js +6 -3
  49. package/dist/components/TimeField/index.cjs +5 -3
  50. package/dist/components/TimeField/index.d.ts +2 -4
  51. package/dist/components/TimeField/index.js +5 -3
  52. package/dist/components/ToggleField/index.cjs +5 -3
  53. package/dist/components/ToggleField/index.d.ts +2 -2
  54. package/dist/components/ToggleField/index.js +5 -3
  55. package/dist/components/ToggleGroupField/index.d.ts +2 -2
  56. package/dist/components/UnitInputField/index.cjs +4 -4
  57. package/dist/components/UnitInputField/index.d.ts +2 -2
  58. package/dist/components/UnitInputField/index.js +4 -4
  59. package/dist/components/index.d.ts +0 -1
  60. package/dist/hooks/index.d.ts +0 -4
  61. package/dist/hooks/useOnFieldChange.cjs +3 -3
  62. package/dist/hooks/useOnFieldChange.d.ts +3 -0
  63. package/dist/index.cjs +8 -18
  64. package/dist/index.d.ts +3 -3
  65. package/dist/index.js +1 -11
  66. package/dist/providers/ErrorContext/index.cjs +5 -5
  67. package/dist/types.d.ts +7 -10
  68. package/dist/utils/validateRegex.cjs +4 -0
  69. package/dist/utils/validateRegex.d.ts +2 -0
  70. package/dist/utils/validateRegex.js +4 -0
  71. package/package.json +2 -2
  72. package/dist/constants.cjs +0 -4
  73. package/dist/constants.d.ts +0 -1
  74. package/dist/constants.js +0 -4
  75. package/dist/hooks/useField.cjs +0 -30
  76. package/dist/hooks/useField.d.ts +0 -34
  77. package/dist/hooks/useField.js +0 -30
  78. package/dist/hooks/useFieldArray.cjs +0 -32
  79. package/dist/hooks/useFieldArray.d.ts +0 -21
  80. package/dist/hooks/useFieldArray.js +0 -32
  81. package/dist/hooks/useForm.cjs +0 -24
  82. package/dist/hooks/useForm.d.ts +0 -22
  83. package/dist/hooks/useForm.js +0 -24
  84. package/dist/hooks/useFormState.cjs +0 -22
  85. package/dist/hooks/useFormState.d.ts +0 -39
  86. package/dist/hooks/useFormState.js +0 -22
@@ -6,6 +6,7 @@ const reactHookForm = require("react-hook-form");
6
6
  const index = require("../../providers/ErrorContext/index.cjs");
7
7
  const CheckboxField = ({
8
8
  id,
9
+ control,
9
10
  name,
10
11
  label,
11
12
  size,
@@ -17,11 +18,11 @@ const CheckboxField = ({
17
18
  onChange,
18
19
  onBlur,
19
20
  onFocus,
20
- rules,
21
21
  helper,
22
22
  tooltip,
23
23
  "data-testid": dataTestId,
24
- shouldUnregister = false
24
+ shouldUnregister = false,
25
+ validate
25
26
  }) => {
26
27
  const {
27
28
  getError
@@ -35,9 +36,10 @@ const CheckboxField = ({
35
36
  name,
36
37
  disabled,
37
38
  shouldUnregister,
39
+ control,
38
40
  rules: {
39
41
  required,
40
- ...rules
42
+ validate
41
43
  }
42
44
  });
43
45
  return /* @__PURE__ */ jsxRuntime.jsx(ui.Checkbox, { id, name: field.name, onChange: (event) => {
@@ -2,9 +2,9 @@ import { Checkbox } from '@ultraviolet/ui';
2
2
  import type { ComponentProps, ReactNode } from 'react';
3
3
  import type { FieldPath, FieldValues } from 'react-hook-form';
4
4
  import type { BaseFieldProps } from '../../types';
5
- type CheckboxFieldProps<TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues>> = Omit<BaseFieldProps<TFieldValues, TName>, 'value'> & Partial<Pick<ComponentProps<typeof Checkbox>, 'id' | 'disabled' | 'onBlur' | 'onFocus' | 'progress' | 'size' | 'data-testid' | 'helper' | 'tooltip'>> & {
5
+ type CheckboxFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = Omit<BaseFieldProps<TFieldValues, TFieldName>, 'value'> & Partial<Pick<ComponentProps<typeof Checkbox>, 'id' | 'disabled' | 'onBlur' | 'onFocus' | 'progress' | 'size' | 'data-testid' | 'helper' | 'tooltip'>> & {
6
6
  className?: string;
7
7
  children?: ReactNode;
8
8
  };
9
- export declare const CheckboxField: <TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ id, name, label, size, progress, disabled, required, className, children, onChange, onBlur, onFocus, rules, helper, tooltip, "data-testid": dataTestId, shouldUnregister, }: CheckboxFieldProps<TFieldValues, TName>) => import("@emotion/react/jsx-runtime").JSX.Element;
9
+ export declare const CheckboxField: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ id, control, name, label, size, progress, disabled, required, className, children, onChange, onBlur, onFocus, helper, tooltip, "data-testid": dataTestId, shouldUnregister, validate, }: CheckboxFieldProps<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
10
10
  export {};
@@ -4,6 +4,7 @@ import { useController } from "react-hook-form";
4
4
  import { useErrors } from "../../providers/ErrorContext/index.js";
5
5
  const CheckboxField = ({
6
6
  id,
7
+ control,
7
8
  name,
8
9
  label,
9
10
  size,
@@ -15,11 +16,11 @@ const CheckboxField = ({
15
16
  onChange,
16
17
  onBlur,
17
18
  onFocus,
18
- rules,
19
19
  helper,
20
20
  tooltip,
21
21
  "data-testid": dataTestId,
22
- shouldUnregister = false
22
+ shouldUnregister = false,
23
+ validate
23
24
  }) => {
24
25
  const {
25
26
  getError
@@ -33,9 +34,10 @@ const CheckboxField = ({
33
34
  name,
34
35
  disabled,
35
36
  shouldUnregister,
37
+ control,
36
38
  rules: {
37
39
  required,
38
- ...rules
40
+ validate
39
41
  }
40
42
  });
41
43
  return /* @__PURE__ */ jsx(Checkbox, { id, name: field.name, onChange: (event) => {
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const jsxRuntime = require("@emotion/react/jsx-runtime");
4
4
  const ui = require("@ultraviolet/ui");
5
- const React = require("react");
5
+ const react = require("react");
6
6
  const reactHookForm = require("react-hook-form");
7
7
  const index = require("../../providers/ErrorContext/index.cjs");
8
8
  const arraysContainSameValues = (array1, array2) => {
@@ -14,6 +14,7 @@ const arraysContainSameValues = (array1, array2) => {
14
14
  const CheckboxGroupField = ({
15
15
  legend,
16
16
  className,
17
+ control,
17
18
  helper,
18
19
  direction,
19
20
  children,
@@ -23,14 +24,14 @@ const CheckboxGroupField = ({
23
24
  name,
24
25
  required = false,
25
26
  shouldUnregister = false,
26
- rules
27
+ validate
27
28
  }) => {
28
29
  const {
29
30
  getError
30
31
  } = index.useErrors();
31
- const validate = React.useCallback((value) => {
32
- const requiredChildren = React.Children.map(children, (child) => {
33
- if (React.isValidElement(child)) {
32
+ const checkboxValid = react.useCallback((value) => {
33
+ const requiredChildren = react.Children.map(children, (child) => {
34
+ if (react.isValidElement(child)) {
34
35
  if (child.props.required) {
35
36
  return child.props.name;
36
37
  }
@@ -41,7 +42,7 @@ const CheckboxGroupField = ({
41
42
  if (!required && arraysContainSameValues(value, requiredChildren)) {
42
43
  return true;
43
44
  }
44
- if (value.length >= React.Children.count(children)) {
45
+ if (value.length >= react.Children.count(children)) {
45
46
  return true;
46
47
  }
47
48
  return false;
@@ -53,10 +54,13 @@ const CheckboxGroupField = ({
53
54
  }
54
55
  } = reactHookForm.useController({
55
56
  name,
57
+ control,
56
58
  shouldUnregister,
57
59
  rules: {
58
- validate,
59
- ...rules
60
+ validate: {
61
+ checkboxValid,
62
+ ...validate
63
+ }
60
64
  }
61
65
  });
62
66
  return /* @__PURE__ */ jsxRuntime.jsx(ui.CheckboxGroup, { legend, name, value: field.value, onChange: (event) => {
@@ -2,9 +2,9 @@ import { CheckboxGroup } from '@ultraviolet/ui';
2
2
  import type { ComponentProps } from 'react';
3
3
  import type { FieldPath, FieldValues } from 'react-hook-form';
4
4
  import type { BaseFieldProps } from '../../types';
5
- type CheckboxGroupFieldProps<TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TName> & Partial<Pick<ComponentProps<typeof CheckboxGroup>, 'className' | 'helper' | 'required' | 'direction' | 'children' | 'error' | 'legend'>> & Required<Pick<ComponentProps<typeof CheckboxGroup>, 'legend'>>;
5
+ type CheckboxGroupFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TFieldName> & Partial<Pick<ComponentProps<typeof CheckboxGroup>, 'className' | 'helper' | 'required' | 'direction' | 'children' | 'error' | 'legend'>> & Required<Pick<ComponentProps<typeof CheckboxGroup>, 'legend'>>;
6
6
  export declare const CheckboxGroupField: {
7
- <TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ legend, className, helper, direction, children, onChange, label, error: customError, name, required, shouldUnregister, rules, }: CheckboxGroupFieldProps<TFieldValues, TName>): import("@emotion/react/jsx-runtime").JSX.Element;
7
+ <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ legend, className, control, helper, direction, children, onChange, label, error: customError, name, required, shouldUnregister, validate, }: CheckboxGroupFieldProps<TFieldValues, TFieldName>): import("@emotion/react/jsx-runtime").JSX.Element;
8
8
  Checkbox: ({ onFocus, onBlur, disabled, error, name, value, children, helper, className, autoFocus, "data-testid": dataTestId, required, }: Omit<({
9
9
  error?: string | import("react").ReactNode;
10
10
  size?: number;
@@ -12,6 +12,7 @@ const arraysContainSameValues = (array1, array2) => {
12
12
  const CheckboxGroupField = ({
13
13
  legend,
14
14
  className,
15
+ control,
15
16
  helper,
16
17
  direction,
17
18
  children,
@@ -21,12 +22,12 @@ const CheckboxGroupField = ({
21
22
  name,
22
23
  required = false,
23
24
  shouldUnregister = false,
24
- rules
25
+ validate
25
26
  }) => {
26
27
  const {
27
28
  getError
28
29
  } = useErrors();
29
- const validate = useCallback((value) => {
30
+ const checkboxValid = useCallback((value) => {
30
31
  const requiredChildren = Children.map(children, (child) => {
31
32
  if (isValidElement(child)) {
32
33
  if (child.props.required) {
@@ -51,10 +52,13 @@ const CheckboxGroupField = ({
51
52
  }
52
53
  } = useController({
53
54
  name,
55
+ control,
54
56
  shouldUnregister,
55
57
  rules: {
56
- validate,
57
- ...rules
58
+ validate: {
59
+ checkboxValid,
60
+ ...validate
61
+ }
58
62
  }
59
63
  });
60
64
  return /* @__PURE__ */ jsx(CheckboxGroup, { legend, name, value: field.value, onChange: (event) => {
@@ -11,6 +11,7 @@ const isEmpty = (value) => typeof value === "string" ? value === "" : value ===
11
11
  const DateField = ({
12
12
  required,
13
13
  name,
14
+ control,
14
15
  label = "",
15
16
  format,
16
17
  locale,
@@ -20,7 +21,7 @@ const DateField = ({
20
21
  onChange,
21
22
  onBlur,
22
23
  onFocus,
23
- rules,
24
+ validate,
24
25
  autoFocus = false,
25
26
  excludeDates,
26
27
  selectsRange,
@@ -38,15 +39,15 @@ const DateField = ({
38
39
  }
39
40
  } = reactHookForm.useController({
40
41
  name,
42
+ control,
41
43
  shouldUnregister,
42
44
  rules: {
43
- ...rules,
45
+ required,
44
46
  validate: {
45
- ...rules?.validate,
46
47
  minDate: minDate.minDateValidator(minDate$1),
47
- maxDate: maxDate.maxDateValidator(maxDate$1)
48
- },
49
- required
48
+ maxDate: maxDate.maxDateValidator(maxDate$1),
49
+ ...validate
50
+ }
50
51
  }
51
52
  });
52
53
  return /* @__PURE__ */ jsxRuntime.jsx(ui.DateInput, { name: field.name, label, value: field.value, format: format || ((value) => value ? parseDate(value).toLocaleDateString() : ""), locale, required, onChange: (val) => {
@@ -2,7 +2,7 @@ import { DateInput } from '@ultraviolet/ui';
2
2
  import type { ComponentProps, FocusEvent } from 'react';
3
3
  import type { FieldPath, FieldValues } from 'react-hook-form';
4
4
  import type { BaseFieldProps } from '../../types';
5
- type DateFieldProps<TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TName> & Omit<ComponentProps<typeof DateInput>, 'maxDate' | 'minDate' | 'disabled' | 'required' | 'locale' | 'name' | 'onChange' | 'onFocus' | 'onBlur' | 'autoFocus' | 'stardDate' | 'endDate'> & {
5
+ type DateFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TFieldName> & Omit<ComponentProps<typeof DateInput>, 'maxDate' | 'minDate' | 'disabled' | 'required' | 'locale' | 'name' | 'onChange' | 'onFocus' | 'onBlur' | 'autoFocus' | 'stardDate' | 'endDate'> & {
6
6
  maxDate?: Date;
7
7
  minDate?: Date;
8
8
  disabled?: boolean;
@@ -12,5 +12,5 @@ type DateFieldProps<TFieldValues extends FieldValues, TName extends FieldPath<TF
12
12
  onFocus?: (value: FocusEvent<HTMLElement>) => void;
13
13
  autoFocus?: boolean;
14
14
  };
15
- export declare const DateField: <TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ required, name, label, format, locale, maxDate, minDate, disabled, onChange, onBlur, onFocus, rules, autoFocus, excludeDates, selectsRange, size, "data-testid": dataTestId, shouldUnregister, }: DateFieldProps<TFieldValues, TName>) => import("@emotion/react/jsx-runtime").JSX.Element;
15
+ export declare const DateField: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ required, name, control, label, format, locale, maxDate, minDate, disabled, onChange, onBlur, onFocus, validate, autoFocus, excludeDates, selectsRange, size, "data-testid": dataTestId, shouldUnregister, }: DateFieldProps<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
16
16
  export {};
@@ -9,6 +9,7 @@ const isEmpty = (value) => typeof value === "string" ? value === "" : value ===
9
9
  const DateField = ({
10
10
  required,
11
11
  name,
12
+ control,
12
13
  label = "",
13
14
  format,
14
15
  locale,
@@ -18,7 +19,7 @@ const DateField = ({
18
19
  onChange,
19
20
  onBlur,
20
21
  onFocus,
21
- rules,
22
+ validate,
22
23
  autoFocus = false,
23
24
  excludeDates,
24
25
  selectsRange,
@@ -36,15 +37,15 @@ const DateField = ({
36
37
  }
37
38
  } = useController({
38
39
  name,
40
+ control,
39
41
  shouldUnregister,
40
42
  rules: {
41
- ...rules,
43
+ required,
42
44
  validate: {
43
- ...rules?.validate,
44
45
  minDate: minDateValidator(minDate),
45
- maxDate: maxDateValidator(maxDate)
46
- },
47
- required
46
+ maxDate: maxDateValidator(maxDate),
47
+ ...validate
48
+ }
48
49
  }
49
50
  });
50
51
  return /* @__PURE__ */ jsx(DateInput, { name: field.name, label, value: field.value, format: format || ((value) => value ? parseDate(value).toLocaleDateString() : ""), locale, required, onChange: (val) => {
@@ -1,72 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const jsxRuntime = require("@emotion/react/jsx-runtime");
4
- const React = require("react");
5
4
  const reactHookForm = require("react-hook-form");
6
- const constants = require("../../constants.cjs");
7
5
  const defaultErrors = require("./defaultErrors.cjs");
8
6
  const index = require("../../providers/ErrorContext/index.cjs");
9
- const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
10
- const React__default = /* @__PURE__ */ _interopDefaultCompat(React);
11
- const FormSubmitContext = React__default.default.createContext({});
12
7
  const Form = ({
13
8
  children,
14
- methods: methodsProp,
15
- initialValues,
9
+ methods,
16
10
  errors,
17
- onRawSubmit,
11
+ onSubmit,
18
12
  name
19
13
  }) => {
20
- const methodsHook = reactHookForm.useForm({
21
- defaultValues: initialValues,
22
- mode: "onChange"
23
- });
24
- const methods = !methodsProp ? methodsHook : methodsProp;
25
14
  const handleSubmit = methods.handleSubmit(async (values) => {
26
- const result = await onRawSubmit(values, {
27
- reset: methods.reset,
28
- resetFieldState: methods.resetField,
29
- restart: () => methods.reset(initialValues),
30
- change: methods.setValue
31
- });
32
- if (result === null) {
33
- methods.setError("root.submit", {
34
- type: "custom"
35
- });
36
- return;
37
- }
38
- if (result && typeof result !== "boolean" && typeof result !== "number") {
15
+ const result = await onSubmit(values);
16
+ if (result) {
39
17
  methods.setError("root.submit", {
40
18
  type: "custom",
41
- message: typeof result === "object" ? result[constants.FORM_ERROR] : result
19
+ message: result
42
20
  });
43
21
  }
44
22
  });
45
- const formSubmitContextValue = React.useMemo(() => ({
46
- handleSubmit
47
- }), [handleSubmit]);
48
- return /* @__PURE__ */ jsxRuntime.jsx(reactHookForm.FormProvider, { ...methods, children: /* @__PURE__ */ jsxRuntime.jsx(FormSubmitContext.Provider, { value: formSubmitContextValue, children: /* @__PURE__ */ jsxRuntime.jsx(index.ErrorProvider, { errors: {
23
+ return /* @__PURE__ */ jsxRuntime.jsx(reactHookForm.FormProvider, { ...methods, children: /* @__PURE__ */ jsxRuntime.jsx(index.ErrorProvider, { errors: {
49
24
  ...defaultErrors.defaultErrors,
50
25
  ...errors
51
26
  }, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit: async (e) => {
52
27
  e.preventDefault();
53
28
  e.stopPropagation();
54
29
  await handleSubmit(e);
55
- }, name, noValidate: true, children: typeof children === "function" ? children({
56
- values: methods.watch(),
57
- hasValidationErrors: !methods.formState.isValid,
58
- errors: methods.formState.errors,
59
- submitting: methods.formState.isSubmitting,
60
- pristine: !methods.formState.isDirty,
61
- handleSubmit,
62
- submitError: !!methods.formState.errors?.root?.["submit"],
63
- valid: methods.formState.isValid,
64
- form: {
65
- change: methods.setValue,
66
- reset: methods.reset,
67
- submit: handleSubmit
68
- }
69
- }) : children }) }) }) });
30
+ }, name, noValidate: true, children }) }) });
70
31
  };
71
32
  exports.Form = Form;
72
- exports.FormSubmitContext = FormSubmitContext;
@@ -1,69 +1,13 @@
1
1
  import type { ReactNode } from 'react';
2
- import React from 'react';
3
- import type { DefaultValues, FieldErrors, FieldValues, UseFormHandleSubmit, UseFormReturn } from 'react-hook-form';
4
- import { FORM_ERROR } from '../../constants';
2
+ import type { FieldValues, UseFormReturn } from 'react-hook-form';
5
3
  import type { FormErrors } from '../../types';
6
- type Without<T, U> = {
7
- [P in Exclude<keyof T, keyof U>]?: never;
8
- };
9
- type SingleXOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
10
- export type XOR<T extends unknown[]> = T extends [infer Only] ? Only : T extends [infer A, infer B, ...infer Rest] ? XOR<[SingleXOR<A, B>, ...Rest]> : never;
11
- type FormStateReturn<TFormValues extends FieldValues> = {
12
- values: TFormValues;
13
- hasValidationErrors: boolean;
14
- errors: FieldErrors<TFormValues>;
15
- submitting: boolean;
16
- pristine: boolean;
17
- handleSubmit: () => Promise<void>;
18
- submitError: boolean;
19
- valid: boolean;
20
- form: {
21
- change: UseFormReturn<TFormValues>['setValue'];
22
- reset: UseFormReturn<TFormValues>['reset'];
23
- submit: () => Promise<void>;
24
- };
25
- };
26
- type OnRawSubmitReturn = {
27
- [FORM_ERROR]?: string;
28
- } | undefined | null | string | void | boolean | number;
29
- type FormSubmitContextValue<TFieldValues extends FieldValues = FieldValues> = {
30
- handleSubmit: ReturnType<UseFormHandleSubmit<TFieldValues>>;
31
- };
32
- export declare const FormSubmitContext: React.Context<FormSubmitContextValue<FieldValues>>;
33
- export type FormProps<TFormValues extends FieldValues = FieldValues> = {
34
- children?: ((props: FormStateReturn<TFormValues>) => ReactNode) | ReactNode;
4
+ type OnSubmitReturn = string | null | undefined | void;
5
+ type FormProps<TFieldValues extends FieldValues> = {
6
+ children?: ReactNode;
35
7
  errors: FormErrors;
36
8
  name?: string;
37
- onRawSubmit: (data: TFormValues, formState: {
38
- reset: UseFormReturn<TFormValues>['reset'];
39
- resetFieldState: UseFormReturn<TFormValues>['resetField'];
40
- restart: () => void;
41
- change: UseFormReturn<TFormValues>['setValue'];
42
- }) => Promise<OnRawSubmitReturn> | OnRawSubmitReturn;
43
- } & XOR<[
44
- {
45
- /**
46
- * @deprecated Use the `methods` prop with [useForm](https://www.react-hook-form.com/api/useform/) instead.
47
- *
48
- * @example
49
- * ```tsx
50
- * const methods = useForm({
51
- * defaultValues,
52
- * mode: 'onChange'
53
- * })
54
- *
55
- * return (
56
- * <Form methods={methods} onRawSubmit={handleSubmit} errors={formErrors}>
57
- * // ...
58
- * </Form>
59
- * )
60
- * ```
61
- */
62
- initialValues?: DefaultValues<TFormValues>;
63
- },
64
- {
65
- methods: UseFormReturn<TFormValues>;
66
- }
67
- ]>;
68
- export declare const Form: <TFormValues extends FieldValues>({ children, methods: methodsProp, initialValues, errors, onRawSubmit, name, }: FormProps<TFormValues>) => import("@emotion/react/jsx-runtime").JSX.Element;
9
+ onSubmit: (data: TFieldValues) => Promise<OnSubmitReturn> | OnSubmitReturn;
10
+ methods: UseFormReturn<TFieldValues>;
11
+ };
12
+ export declare const Form: <TFieldValues extends FieldValues>({ children, methods, errors, onSubmit, name, }: FormProps<TFieldValues>) => import("@emotion/react/jsx-runtime").JSX.Element;
69
13
  export {};
@@ -1,70 +1,32 @@
1
1
  import { jsx } from "@emotion/react/jsx-runtime";
2
- import React, { useMemo } from "react";
3
- import { useForm, FormProvider } from "react-hook-form";
4
- import { FORM_ERROR } from "../../constants.js";
2
+ import { FormProvider } from "react-hook-form";
5
3
  import { defaultErrors } from "./defaultErrors.js";
6
4
  import { ErrorProvider } from "../../providers/ErrorContext/index.js";
7
- const FormSubmitContext = React.createContext({});
8
5
  const Form = ({
9
6
  children,
10
- methods: methodsProp,
11
- initialValues,
7
+ methods,
12
8
  errors,
13
- onRawSubmit,
9
+ onSubmit,
14
10
  name
15
11
  }) => {
16
- const methodsHook = useForm({
17
- defaultValues: initialValues,
18
- mode: "onChange"
19
- });
20
- const methods = !methodsProp ? methodsHook : methodsProp;
21
12
  const handleSubmit = methods.handleSubmit(async (values) => {
22
- const result = await onRawSubmit(values, {
23
- reset: methods.reset,
24
- resetFieldState: methods.resetField,
25
- restart: () => methods.reset(initialValues),
26
- change: methods.setValue
27
- });
28
- if (result === null) {
29
- methods.setError("root.submit", {
30
- type: "custom"
31
- });
32
- return;
33
- }
34
- if (result && typeof result !== "boolean" && typeof result !== "number") {
13
+ const result = await onSubmit(values);
14
+ if (result) {
35
15
  methods.setError("root.submit", {
36
16
  type: "custom",
37
- message: typeof result === "object" ? result[FORM_ERROR] : result
17
+ message: result
38
18
  });
39
19
  }
40
20
  });
41
- const formSubmitContextValue = useMemo(() => ({
42
- handleSubmit
43
- }), [handleSubmit]);
44
- return /* @__PURE__ */ jsx(FormProvider, { ...methods, children: /* @__PURE__ */ jsx(FormSubmitContext.Provider, { value: formSubmitContextValue, children: /* @__PURE__ */ jsx(ErrorProvider, { errors: {
21
+ return /* @__PURE__ */ jsx(FormProvider, { ...methods, children: /* @__PURE__ */ jsx(ErrorProvider, { errors: {
45
22
  ...defaultErrors,
46
23
  ...errors
47
24
  }, children: /* @__PURE__ */ jsx("form", { onSubmit: async (e) => {
48
25
  e.preventDefault();
49
26
  e.stopPropagation();
50
27
  await handleSubmit(e);
51
- }, name, noValidate: true, children: typeof children === "function" ? children({
52
- values: methods.watch(),
53
- hasValidationErrors: !methods.formState.isValid,
54
- errors: methods.formState.errors,
55
- submitting: methods.formState.isSubmitting,
56
- pristine: !methods.formState.isDirty,
57
- handleSubmit,
58
- submitError: !!methods.formState.errors?.root?.["submit"],
59
- valid: methods.formState.isValid,
60
- form: {
61
- change: methods.setValue,
62
- reset: methods.reset,
63
- submit: handleSubmit
64
- }
65
- }) : children }) }) }) });
28
+ }, name, noValidate: true, children }) }) });
66
29
  };
67
30
  export {
68
- Form,
69
- FormSubmitContext
31
+ Form
70
32
  };
@@ -18,10 +18,11 @@ const NumberInputField = ({
18
18
  size,
19
19
  step,
20
20
  text,
21
- rules,
22
21
  className,
23
22
  label,
24
- shouldUnregister = false
23
+ shouldUnregister = false,
24
+ validate,
25
+ control
25
26
  }) => {
26
27
  const {
27
28
  getError
@@ -34,11 +35,12 @@ const NumberInputField = ({
34
35
  } = reactHookForm.useController({
35
36
  name,
36
37
  shouldUnregister,
38
+ control,
37
39
  rules: {
38
40
  max: maxValue,
39
41
  min: minValue,
40
42
  required,
41
- ...rules
43
+ validate
42
44
  }
43
45
  });
44
46
  return /* @__PURE__ */ jsxRuntime.jsx(ui.NumberInput, { name: field.name, value: field.value, disabled, onBlur: (event) => {
@@ -2,12 +2,12 @@ import { NumberInput } from '@ultraviolet/ui';
2
2
  import type { ComponentProps, FocusEventHandler } from 'react';
3
3
  import type { FieldPath, FieldValues } from 'react-hook-form';
4
4
  import type { BaseFieldProps } from '../../types';
5
- type NumberInputValueFieldProps<TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TName> & Partial<Pick<ComponentProps<typeof NumberInput>, 'disabled' | 'maxValue' | 'minValue' | 'onMaxCrossed' | 'onMinCrossed' | 'size' | 'step' | 'text' | 'className'>> & {
5
+ type NumberInputValueFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TFieldName> & Partial<Pick<ComponentProps<typeof NumberInput>, 'disabled' | 'maxValue' | 'minValue' | 'onMaxCrossed' | 'onMinCrossed' | 'size' | 'step' | 'text' | 'className'>> & {
6
6
  onBlur?: FocusEventHandler<HTMLInputElement>;
7
7
  onFocus?: FocusEventHandler<HTMLInputElement>;
8
8
  };
9
9
  /**
10
10
  * @deprecated This component is deprecated, use `NumberInputFieldV2` instead.
11
11
  */
12
- export declare const NumberInputField: <TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, maxValue, minValue, name, onChange, onBlur, onFocus, onMaxCrossed, onMinCrossed, required, size, step, text, rules, className, label, shouldUnregister, }: NumberInputValueFieldProps<TFieldValues, TName>) => import("@emotion/react/jsx-runtime").JSX.Element;
12
+ export declare const NumberInputField: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, maxValue, minValue, name, onChange, onBlur, onFocus, onMaxCrossed, onMinCrossed, required, size, step, text, className, label, shouldUnregister, validate, control, }: NumberInputValueFieldProps<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
13
13
  export {};
@@ -16,10 +16,11 @@ const NumberInputField = ({
16
16
  size,
17
17
  step,
18
18
  text,
19
- rules,
20
19
  className,
21
20
  label,
22
- shouldUnregister = false
21
+ shouldUnregister = false,
22
+ validate,
23
+ control
23
24
  }) => {
24
25
  const {
25
26
  getError
@@ -32,11 +33,12 @@ const NumberInputField = ({
32
33
  } = useController({
33
34
  name,
34
35
  shouldUnregister,
36
+ control,
35
37
  rules: {
36
38
  max: maxValue,
37
39
  min: minValue,
38
40
  required,
39
- ...rules
41
+ validate
40
42
  }
41
43
  });
42
44
  return /* @__PURE__ */ jsx(NumberInput, { name: field.name, value: field.value, disabled, onBlur: (event) => {
@@ -6,6 +6,7 @@ const reactHookForm = require("react-hook-form");
6
6
  const index = require("../../providers/ErrorContext/index.cjs");
7
7
  const NumberInputFieldV2 = ({
8
8
  disabled,
9
+ control,
9
10
  max = Number.MAX_SAFE_INTEGER,
10
11
  min = 0,
11
12
  name,
@@ -22,14 +23,14 @@ const NumberInputFieldV2 = ({
22
23
  placeholder,
23
24
  success,
24
25
  helper,
25
- rules,
26
26
  controls = true,
27
27
  "aria-label": ariaLabel,
28
28
  "data-testid": dataTestId,
29
29
  required,
30
30
  autoFocus,
31
31
  readOnly,
32
- shouldUnregister = false
32
+ shouldUnregister = false,
33
+ validate
33
34
  }) => {
34
35
  const {
35
36
  getError
@@ -41,12 +42,13 @@ const NumberInputFieldV2 = ({
41
42
  }
42
43
  } = reactHookForm.useController({
43
44
  name,
45
+ control,
44
46
  shouldUnregister,
45
47
  rules: {
46
48
  max,
47
49
  min,
48
50
  required,
49
- ...rules
51
+ validate
50
52
  }
51
53
  });
52
54
  return /* @__PURE__ */ jsxRuntime.jsx(ui.NumberInputV2, { name: field.name, value: field.value, disabled, onBlur: (event) => {
@@ -2,10 +2,8 @@ import { NumberInputV2 } from '@ultraviolet/ui';
2
2
  import type { ComponentProps } from 'react';
3
3
  import type { FieldPath, FieldValues } from 'react-hook-form';
4
4
  import type { BaseFieldProps } from '../../types';
5
- type NumberInputV2Props<TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TName> & Partial<Pick<ComponentProps<typeof NumberInputV2>, 'disabled' | 'id' | 'onBlur' | 'onChange' | 'onFocus' | 'value' | 'data-testid' | 'label' | 'tooltip' | 'unit' | 'size' | 'step' | 'className' | 'placeholder' | 'error' | 'success' | 'helper' | 'labelDescription' | 'aria-label' | 'autoFocus' | 'readOnly' | 'min' | 'max' | 'controls'>> & {
5
+ type NumberInputV2Props<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TFieldName> & Partial<Pick<ComponentProps<typeof NumberInputV2>, 'disabled' | 'id' | 'onBlur' | 'onChange' | 'onFocus' | 'value' | 'data-testid' | 'label' | 'tooltip' | 'unit' | 'size' | 'step' | 'className' | 'placeholder' | 'error' | 'success' | 'helper' | 'labelDescription' | 'aria-label' | 'autoFocus' | 'readOnly' | 'min' | 'max' | 'controls'>> & {
6
6
  className?: string;
7
- name: string;
8
- required?: boolean;
9
7
  };
10
- export declare const NumberInputFieldV2: <TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, max, min, name, onChange, onBlur, size, step, unit, tooltip, className, label, labelDescription, id, placeholder, success, helper, rules, controls, "aria-label": ariaLabel, "data-testid": dataTestId, required, autoFocus, readOnly, shouldUnregister, }: NumberInputV2Props<TFieldValues, TName>) => import("@emotion/react/jsx-runtime").JSX.Element;
8
+ export declare const NumberInputFieldV2: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, control, max, min, name, onChange, onBlur, size, step, unit, tooltip, className, label, labelDescription, id, placeholder, success, helper, controls, "aria-label": ariaLabel, "data-testid": dataTestId, required, autoFocus, readOnly, shouldUnregister, validate, }: NumberInputV2Props<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
11
9
  export {};