@ultraviolet/form 2.0.0-next.4 → 2.0.0-next.5

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.
@@ -1,153 +1,143 @@
1
1
  import { TextInput } from '@ultraviolet/ui';
2
2
  import { forwardRef } from 'react';
3
+ import { Controller } from 'react-hook-form';
3
4
  import { jsx } from '@emotion/react/jsx-runtime';
4
5
  import { useErrors } from '../../providers/ErrorContext/index.js';
5
- import { useFormField } from '../../hooks/useFormField.js';
6
6
 
7
7
  const TextInputField = /*#__PURE__*/forwardRef((_ref, ref) => {
8
8
  let {
9
- afterSubmit,
10
- allowNull,
11
9
  autoCapitalize,
12
10
  autoComplete,
13
11
  autoCorrect,
14
12
  autoFocus,
15
13
  autoSave,
16
- beforeSubmit,
17
14
  className,
18
15
  cols,
19
- 'data-testid': dataTestId,
20
- defaultValue,
21
16
  disabled,
22
17
  fillAvailable,
23
- format,
24
- formatOnBlur,
25
18
  generated,
26
19
  id,
27
- initialValue,
28
- isEqual,
29
20
  label = '',
30
- max,
31
- maxLength,
32
- min,
33
- minLength,
34
21
  multiline,
35
- multiple,
36
22
  name,
37
23
  noTopLabel,
38
24
  notice,
39
- onBlur,
40
25
  onChange,
41
26
  onFocus,
42
27
  onKeyDown,
43
28
  onKeyUp,
44
- parse,
29
+ onBlur,
45
30
  placeholder,
46
31
  random,
47
32
  readOnly,
48
- regex,
49
33
  required,
50
34
  resizable,
51
35
  rows,
52
- subscription,
53
36
  type,
54
37
  unit,
55
38
  size,
56
- validate,
57
- validateFields,
39
+ rules,
58
40
  valid,
59
- value
60
- } = _ref;
61
- const {
62
- getError
63
- } = useErrors();
64
- const {
65
- input,
66
- meta
67
- } = useFormField(name, {
68
- afterSubmit,
69
- allowNull,
70
- beforeSubmit,
71
- defaultValue,
72
- disabled,
41
+ parse,
73
42
  format,
74
- formatOnBlur,
75
- initialValue,
76
- isEqual,
77
- max,
78
- maxLength,
43
+ regex: regexes,
79
44
  min,
80
- minLength,
81
- multiple,
82
- parse,
83
- regex,
84
- required,
85
- subscription,
86
- type,
87
- validate,
88
- validateFields,
89
- value
90
- });
91
- const error = getError({
92
- label,
93
45
  max,
94
- maxLength,
95
- meta: meta,
96
- min,
97
46
  minLength,
98
- name,
99
- regex,
100
- value: input.value
101
- });
102
- return jsx(TextInput, {
103
- autoCapitalize: autoCapitalize,
104
- autoComplete: autoComplete,
105
- autoCorrect: autoCorrect,
106
- autoFocus: autoFocus,
107
- autoSave: autoSave,
108
- className: className,
109
- cols: cols,
110
- "data-testid": dataTestId,
111
- disabled: disabled,
112
- error: error,
113
- fillAvailable: fillAvailable,
114
- generated: generated,
115
- id: id,
116
- label: label,
117
- max: max,
118
- maxLength: maxLength,
119
- min: min,
120
- minLength: minLength,
121
- multiline: multiline,
122
- name: input.name,
123
- notice: notice,
124
- onBlur: event => {
125
- input.onBlur(event);
126
- onBlur?.(event);
127
- },
128
- onChange: event => {
129
- input.onChange(event);
130
- onChange?.(event);
131
- },
132
- onFocus: event => {
133
- input.onFocus(event);
134
- onFocus?.(event);
47
+ maxLength,
48
+ validate,
49
+ defaultValue,
50
+ customError
51
+ } = _ref;
52
+ const {
53
+ getError
54
+ } = useErrors();
55
+ return jsx(Controller, {
56
+ name: name,
57
+ rules: {
58
+ required,
59
+ pattern: regexes && new RegExp(regexes.map(regex => {
60
+ const newRegex = Array.isArray(regex) ? regex.map(reg => reg.source).join('|') : regex.source;
61
+ return `(?=${newRegex})`;
62
+ }).join('')),
63
+ validate,
64
+ minLength,
65
+ maxLength,
66
+ max,
67
+ min,
68
+ ...rules
135
69
  },
136
- onKeyUp: onKeyUp,
137
- onKeyDown: onKeyDown,
138
- placeholder: placeholder,
139
- random: random,
140
- readOnly: readOnly,
141
- ref: ref,
142
- required: required,
143
- resizable: resizable,
144
- rows: rows,
145
- type: input.type,
146
- value: input.value,
147
- noTopLabel: noTopLabel,
148
- unit: unit,
149
- valid: valid,
150
- size: size
70
+ defaultValue: defaultValue,
71
+ render: _ref2 => {
72
+ let {
73
+ field,
74
+ fieldState: {
75
+ error
76
+ }
77
+ } = _ref2;
78
+ const transformedValue = () => {
79
+ if (format) {
80
+ return format(field.value);
81
+ }
82
+ return field.value;
83
+ };
84
+ return jsx(TextInput, {
85
+ name: field.name,
86
+ autoCapitalize: autoCapitalize,
87
+ autoComplete: autoComplete,
88
+ autoCorrect: autoCorrect,
89
+ autoFocus: autoFocus,
90
+ autoSave: autoSave,
91
+ className: className,
92
+ cols: cols,
93
+ disabled: disabled,
94
+ error: customError ?? getError({
95
+ regex: regexes,
96
+ minLength,
97
+ maxLength,
98
+ label,
99
+ min,
100
+ max,
101
+ value: field.value
102
+ }, error),
103
+ fillAvailable: fillAvailable,
104
+ generated: generated,
105
+ id: id,
106
+ label: label,
107
+ multiline: multiline,
108
+ notice: notice,
109
+ required: required,
110
+ onBlur: event => {
111
+ field.onBlur();
112
+ onBlur?.(event);
113
+ },
114
+ onChange: event => {
115
+ if (parse) {
116
+ field.onChange(parse(event));
117
+ } else {
118
+ field.onChange(event);
119
+ }
120
+ onChange?.(event);
121
+ },
122
+ onFocus: event => {
123
+ onFocus?.(event);
124
+ },
125
+ onKeyUp: onKeyUp,
126
+ onKeyDown: onKeyDown,
127
+ placeholder: placeholder,
128
+ random: random,
129
+ readOnly: readOnly,
130
+ resizable: resizable,
131
+ rows: rows,
132
+ type: type,
133
+ noTopLabel: noTopLabel,
134
+ unit: unit,
135
+ valid: valid,
136
+ size: size,
137
+ value: transformedValue(),
138
+ ref: ref
139
+ });
140
+ }
151
141
  });
152
142
  });
153
143
 
@@ -1,7 +1,6 @@
1
1
  import { TimeInput } from '@ultraviolet/ui';
2
- import { useMemo } from 'react';
2
+ import { Controller } from 'react-hook-form';
3
3
  import { jsx } from '@emotion/react/jsx-runtime';
4
- import { useFormField } from '../../hooks/useFormField.js';
5
4
 
6
5
  const parseTime = date => {
7
6
  const timeStr = date && typeof date !== 'string' ? date.toLocaleTimeString().slice(0, -3) : '';
@@ -17,73 +16,73 @@ const TimeField = _ref => {
17
16
  schedule,
18
17
  placeholder,
19
18
  disabled,
20
- initialValue,
21
- validate,
22
19
  readOnly,
23
- value,
24
- onChange,
25
20
  onBlur,
26
21
  onFocus,
22
+ onChange,
27
23
  isLoading,
28
24
  isClearable,
29
25
  inputId,
30
26
  id,
31
- formatOnBlur,
32
27
  animation,
33
28
  animationDuration,
34
29
  animationOnChange,
35
30
  className,
36
31
  isSearchable,
32
+ rules,
37
33
  options,
38
34
  'data-testid': dataTestId
39
35
  } = _ref;
40
- const {
41
- input,
42
- meta
43
- } = useFormField(name, {
44
- disabled,
45
- formatOnBlur,
46
- initialValue,
47
- required,
48
- validate,
49
- value
50
- });
51
- const error = useMemo(() => input.value && meta.error ? meta.error : undefined, [input.value, meta.error]);
52
- return jsx(TimeInput, {
53
- placeholder: placeholder,
54
- schedule: schedule,
55
- required: required,
56
- value: parseTime(input.value),
57
- onChange: (val, action) => {
58
- if (!val) return;
59
- onChange?.(val, action);
60
- const [hours, minutes] = val.value.split(':');
61
- const date = input.value ? new Date(input.value) : new Date();
62
- date.setHours(Number(hours), Number(minutes), 0);
63
- input.onChange(date);
64
- },
65
- onBlur: event => {
66
- input.onBlur(event);
67
- onBlur?.(event);
68
- },
69
- onFocus: event => {
70
- input.onFocus(event);
71
- onFocus?.(event);
36
+ return jsx(Controller, {
37
+ name: name,
38
+ rules: {
39
+ required,
40
+ ...rules
72
41
  },
73
- error: error,
74
- disabled: disabled,
75
- readOnly: readOnly,
76
- animation: animation,
77
- animationDuration: animationDuration,
78
- animationOnChange: animationOnChange,
79
- className: className,
80
- isLoading: isLoading,
81
- isClearable: isClearable,
82
- isSearchable: isSearchable,
83
- inputId: inputId,
84
- id: id,
85
- options: options,
86
- "data-testid": dataTestId
42
+ render: _ref2 => {
43
+ let {
44
+ field,
45
+ fieldState: {
46
+ error
47
+ }
48
+ } = _ref2;
49
+ return jsx(TimeInput, {
50
+ name: field.name,
51
+ placeholder: placeholder,
52
+ schedule: schedule,
53
+ required: required,
54
+ value: parseTime(field.value),
55
+ onChange: (val, action) => {
56
+ if (!val) return;
57
+ onChange?.(val, action);
58
+ const [hours, minutes] = val.value.split(':');
59
+ const date = field.value ? new Date(field.value) : new Date();
60
+ date.setHours(Number(hours), Number(minutes), 0);
61
+ field.onChange(date);
62
+ },
63
+ onBlur: event => {
64
+ field.onBlur();
65
+ onBlur?.(event);
66
+ },
67
+ onFocus: event => {
68
+ onFocus?.(event);
69
+ },
70
+ error: error?.message,
71
+ disabled: disabled,
72
+ readOnly: readOnly,
73
+ animation: animation,
74
+ animationDuration: animationDuration,
75
+ animationOnChange: animationOnChange,
76
+ className: className,
77
+ isLoading: isLoading,
78
+ isClearable: isClearable,
79
+ isSearchable: isSearchable,
80
+ inputId: inputId,
81
+ id: id,
82
+ options: options,
83
+ "data-testid": dataTestId
84
+ });
85
+ }
87
86
  });
88
87
  };
89
88
 
@@ -1,72 +1,63 @@
1
1
  import { Toggle } from '@ultraviolet/ui';
2
+ import { Controller } from 'react-hook-form';
2
3
  import { jsx } from '@emotion/react/jsx-runtime';
3
- import { useFormField } from '../../hooks/useFormField.js';
4
4
 
5
5
  const ToggleField = _ref => {
6
6
  let {
7
- afterSubmit,
8
- allowNull,
9
- beforeSubmit,
10
7
  className,
11
- data,
12
- defaultValue,
13
8
  disabled,
14
- format,
15
- formatOnBlur,
16
- initialValue,
17
- isEqual,
18
9
  label,
19
- multiple,
20
10
  name,
21
11
  onChange,
22
- parse,
23
12
  required,
24
13
  size,
25
- subscription,
26
14
  tooltip,
27
- validate,
28
- validateFields,
29
- value,
15
+ rules,
30
16
  labelPosition,
17
+ parse,
18
+ format,
31
19
  'data-testid': dataTestId
32
20
  } = _ref;
33
- const {
34
- input
35
- } = useFormField(name, {
36
- afterSubmit,
37
- allowNull,
38
- beforeSubmit,
39
- data,
40
- defaultValue,
41
- disabled,
42
- format,
43
- formatOnBlur,
44
- initialValue,
45
- isEqual,
46
- multiple,
47
- parse,
48
- required,
49
- subscription,
50
- type: 'checkbox',
51
- validate,
52
- validateFields,
53
- value
54
- });
55
- return jsx(Toggle, {
56
- checked: input.checked,
57
- tooltip: tooltip,
58
- onChange: event => {
59
- input.onChange(event);
60
- onChange?.(event);
61
- },
62
- label: label,
63
- size: size,
21
+ return jsx(Controller, {
64
22
  name: name,
65
- disabled: disabled,
66
- labelPosition: labelPosition,
67
- className: className,
68
- required: required,
69
- "data-testid": dataTestId
23
+ rules: {
24
+ required,
25
+ ...rules
26
+ }
27
+ // defaultValue={value as any}
28
+ ,
29
+ render: _ref2 => {
30
+ let {
31
+ field
32
+ } = _ref2;
33
+ const transformedValue = () => {
34
+ if (format) {
35
+ return format(field.value);
36
+ }
37
+ return field.value;
38
+ };
39
+ return jsx(Toggle, {
40
+ name: field.name,
41
+ ref: field.ref,
42
+ checked: transformedValue(),
43
+ tooltip: tooltip,
44
+ onChange: event => {
45
+ if (parse) {
46
+ field.onChange(parse(event.target.checked));
47
+ } else {
48
+ field.onChange(event);
49
+ }
50
+ onChange?.(event);
51
+ },
52
+ label: label,
53
+ size: size,
54
+ disabled: disabled,
55
+ labelPosition: labelPosition,
56
+ className: className,
57
+ required: required,
58
+ "data-testid": dataTestId
59
+ });
60
+ }
70
61
  });
71
62
  };
72
63
 
@@ -1,7 +1,20 @@
1
1
  import { useFormContext, useController } from 'react-hook-form';
2
2
 
3
3
  /**
4
- * @deprecated
4
+ * @deprecated Use [useForm](https://www.react-hook-form.com/api/useform/), [useFormContext](https://www.react-hook-form.com/api/useformcontext/) or [useWatch](https://www.react-hook-form.com/api/usewatch/) to get values. Use [useFormState](https://www.react-hook-form.com/api/useformstate/) to get fields states.
5
+ * @example
6
+ * ```tsx
7
+ * const Input = () {
8
+ * const username = useWatch({
9
+ * name: 'username'
10
+ * })
11
+ *
12
+ * const { errors } = useFormState()
13
+ *
14
+ * console.log(errors.username)
15
+ * }
16
+ * ```
17
+
5
18
  */
6
19
  const useFieldDeprecated = (name, options) => {
7
20
  const {
@@ -1,7 +1,7 @@
1
1
  import { useFieldArray, useWatch } from 'react-hook-form';
2
2
 
3
3
  /**
4
- * @deprecated
4
+ * @deprecated Use [useFieldArray](https://www.react-hook-form.com/api/usefieldarray/)
5
5
  */
6
6
  const useFieldArrayDeprecated = (name, options) => {
7
7
  const {
@@ -3,7 +3,16 @@ import { useFormContext } from 'react-hook-form';
3
3
  import { FormSubmitContext } from '../components/Form/index.js';
4
4
 
5
5
  /**
6
- * @deprecated
6
+ * @deprecated Use [useFormContext](https://www.react-hook-form.com/api/useformcontext/)
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * const Input = () {
11
+ * const { setValue } = useFormContext()
12
+ *
13
+ * setValue('username', 'John Wick')
14
+ * }
15
+ * ```
7
16
  */
8
17
  const useFormDeprecated = () => {
9
18
  const {
@@ -1,7 +1,18 @@
1
1
  import { useFormContext, useWatch } from 'react-hook-form';
2
2
 
3
3
  /**
4
- * @deprecated
4
+ * @deprecated Use [useForm](https://www.react-hook-form.com/api/useform/), [useFormContext](https://www.react-hook-form.com/api/useformcontext/) or [useWatch](https://www.react-hook-form.com/api/usewatch/) to get values. Use [useFormState](https://www.react-hook-form.com/api/useformstate/) to get form states.
5
+ * @example
6
+ * ```tsx
7
+ * const Input = () {
8
+ * const username = useWatch({
9
+ * name: 'username'
10
+ * })
11
+ *
12
+ * const { isValid } = useFormState()
13
+ * }
14
+ * ```
15
+
5
16
  */
6
17
  const useFormStateDeprecated = _params => {
7
18
  const {
@@ -1,30 +1,27 @@
1
1
  import { useRef, useEffect } from 'react';
2
- import { useFormState, useField } from 'react-final-form';
2
+ import { useFormContext } from 'react-hook-form';
3
3
 
4
- const useOnFieldChange = function (name, callback, enabled) {
4
+ /**
5
+ * @deprecated
6
+ */
7
+ const useOnFieldChange = function (fieldName, callback, enabled) {
5
8
  if (enabled === void 0) {
6
9
  enabled = true;
7
10
  }
8
11
  const {
9
- values
10
- } = useFormState();
11
- const {
12
- input: {
13
- value
14
- }
15
- } = useField(name, {
16
- allowNull: true,
17
- subscription: {
18
- value: true
19
- }
20
- });
21
- const previousValues = useRef(value);
12
+ watch,
13
+ getValues
14
+ } = useFormContext();
15
+ const previousValues = useRef(getValues(fieldName));
22
16
  useEffect(() => {
23
- if (previousValues.current !== value && enabled) {
24
- previousValues.current = value;
25
- callback(value, values);
26
- }
27
- }, [value, values, callback, enabled]);
17
+ const subscription = watch(value => {
18
+ if (previousValues.current !== value[fieldName] && enabled) {
19
+ previousValues.current = value[fieldName];
20
+ callback(value[fieldName], value)?.catch(() => null);
21
+ }
22
+ });
23
+ return () => subscription.unsubscribe();
24
+ }, [callback, enabled, watch, getValues, fieldName]);
28
25
  };
29
26
 
30
27
  export { useOnFieldChange };