@ultraviolet/form 2.0.0-next.7 → 2.0.0-next.9

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,65 +1,61 @@
1
1
  import { Radio } from '@ultraviolet/ui';
2
+ import { useController } from 'react-hook-form';
2
3
  import { jsx } from '@emotion/react/jsx-runtime';
3
4
  import { useErrors } from '../../providers/ErrorContext/index.js';
4
- import { useFormField } from '../../hooks/useFormField.js';
5
5
 
6
- const RadioField = _ref => {
7
- let {
8
- className,
9
- 'data-testid': dataTestId,
10
- disabled,
11
- id,
12
- label = '',
13
- name,
14
- onBlur,
15
- onChange,
16
- onFocus,
17
- required,
18
- validate,
19
- value,
20
- tooltip
21
- } = _ref;
6
+ const RadioField = ({
7
+ className,
8
+ 'data-testid': dataTestId,
9
+ disabled,
10
+ id,
11
+ name,
12
+ onBlur,
13
+ label = '',
14
+ onChange,
15
+ onFocus,
16
+ required,
17
+ value,
18
+ rules,
19
+ tooltip,
20
+ shouldUnregister = false
21
+ }) => {
22
22
  const {
23
23
  getError
24
24
  } = useErrors();
25
25
  const {
26
- input,
27
- meta
28
- } = useFormField(name, {
29
- required,
30
- type: 'radio',
31
- validate,
32
- value
33
- });
34
- const error = getError({
35
- disabled,
36
- label: label,
37
- meta: meta,
26
+ field,
27
+ fieldState: {
28
+ error
29
+ }
30
+ } = useController({
38
31
  name,
39
- value: input.value
32
+ shouldUnregister,
33
+ rules: {
34
+ required,
35
+ ...rules
36
+ }
40
37
  });
41
38
  return jsx(Radio, {
42
- checked: input.checked,
39
+ name: field.name,
40
+ checked: field.value === value,
43
41
  className: className,
44
42
  "data-testid": dataTestId,
45
43
  disabled: disabled,
46
- error: error,
44
+ error: getError({
45
+ label: typeof label === 'string' ? label : ''
46
+ }, error),
47
47
  id: id,
48
- name: input.name,
49
- onChange: event => {
50
- input.onChange(event);
51
- onChange?.(event);
48
+ onChange: () => {
49
+ field.onChange(value);
50
+ onChange?.(value);
52
51
  },
53
52
  onBlur: event => {
54
- input.onBlur(event);
53
+ field.onBlur();
55
54
  onBlur?.(event);
56
55
  },
57
- onFocus: event => {
58
- input.onFocus(event);
59
- onFocus?.(event);
60
- },
56
+ onFocus: onFocus,
61
57
  required: required,
62
- value: input.value,
58
+ value: value ?? '',
63
59
  label: label,
64
60
  tooltip: tooltip
65
61
  });
@@ -1,50 +1,51 @@
1
1
  import { RadioGroup } from '@ultraviolet/ui';
2
+ import { useController } from 'react-hook-form';
2
3
  import { jsx } from '@emotion/react/jsx-runtime';
3
4
  import { useErrors } from '../../providers/ErrorContext/index.js';
4
- import { useFormField } from '../../hooks/useFormField.js';
5
5
 
6
- const RadioGroupField = _ref => {
7
- let {
8
- className,
9
- legend = '',
10
- name,
11
- onChange,
12
- required,
13
- validate,
14
- value,
15
- children,
16
- error: customError,
17
- helper,
18
- direction
19
- } = _ref;
6
+ const RadioGroupField = ({
7
+ className,
8
+ legend = '',
9
+ name,
10
+ onChange,
11
+ required,
12
+ rules,
13
+ children,
14
+ label = '',
15
+ error: customError,
16
+ helper,
17
+ direction,
18
+ shouldUnregister = false
19
+ }) => {
20
20
  const {
21
21
  getError
22
22
  } = useErrors();
23
23
  const {
24
- input,
25
- meta
26
- } = useFormField(name, {
27
- required,
28
- validate,
29
- value
30
- });
31
- const error = getError({
32
- label: legend,
33
- meta: meta,
24
+ field,
25
+ fieldState: {
26
+ error
27
+ }
28
+ } = useController({
34
29
  name,
35
- value: input.value
30
+ shouldUnregister,
31
+ rules: {
32
+ required,
33
+ ...rules
34
+ }
36
35
  });
37
36
  return jsx(RadioGroup, {
38
37
  className: className,
39
- name: input.name,
38
+ name: field.name,
40
39
  onChange: event => {
41
- input.onChange(event);
42
- onChange?.(event);
40
+ field.onChange(event);
41
+ onChange?.(event.target.value);
43
42
  },
44
43
  required: required,
45
- value: input.value,
44
+ value: field.value,
46
45
  legend: legend,
47
- error: error ?? customError,
46
+ error: getError({
47
+ label
48
+ }, error) ?? customError,
48
49
  helper: helper,
49
50
  direction: direction,
50
51
  children: children
@@ -1,62 +1,57 @@
1
1
  import { SelectInput } from '@ultraviolet/ui';
2
2
  import { useMemo, Children, useCallback } from 'react';
3
+ import { useController } 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 identity = x => x;
8
- const SelectInputField = _ref => {
9
- let {
10
- animation,
11
- animationDuration,
12
- animationOnChange,
13
- children,
14
- className,
15
- disabled,
16
- error: errorProp,
17
- format: formatProp = identity,
18
- formatOnBlur,
19
- id,
20
- inputId,
21
- isClearable,
22
- isLoading,
23
- isSearchable,
24
- label = '',
25
- maxLength,
26
- menuPortalTarget,
27
- minLength,
28
- multiple,
29
- name,
30
- onBlur,
31
- onChange,
32
- onFocus,
33
- options: optionsProp,
34
- parse: parseProp = identity,
35
- placeholder,
36
- readOnly,
37
- required,
38
- value,
39
- noTopLabel,
40
- noOptionsMessage,
41
- customStyle,
42
- validate,
43
- 'data-testid': dataTestId
44
- } = _ref;
45
- const {
46
- getError
47
- } = useErrors();
48
- const options = useMemo(() => optionsProp || Children.toArray(children).flat().filter(Boolean).map(_ref2 => {
49
- let {
50
- props: {
51
- children: labelChild,
52
- ...option
53
- }
54
- } = _ref2;
55
- return {
56
- ...option,
57
- label: labelChild
58
- };
59
- }), [optionsProp, children]);
8
+ // const identity = <T,>(x: T) => x
9
+
10
+ const SelectInputField = ({
11
+ animation,
12
+ animationDuration,
13
+ animationOnChange,
14
+ children,
15
+ className,
16
+ disabled,
17
+ // error: errorProp,
18
+ format: formatProp = identity,
19
+ // formatOnBlur,
20
+ id,
21
+ inputId,
22
+ isClearable,
23
+ isLoading,
24
+ isSearchable,
25
+ label = '',
26
+ maxLength,
27
+ menuPortalTarget,
28
+ minLength,
29
+ multiple,
30
+ name,
31
+ onBlur,
32
+ onChange,
33
+ onFocus,
34
+ options: optionsProp,
35
+ parse: parseProp = identity,
36
+ placeholder,
37
+ readOnly,
38
+ required,
39
+ rules,
40
+ noTopLabel,
41
+ noOptionsMessage,
42
+ customStyle,
43
+ shouldUnregister = false,
44
+ 'data-testid': dataTestId
45
+ }) => {
46
+ const options = useMemo(() => optionsProp || Children.toArray(children).flat().filter(Boolean).map(({
47
+ props: {
48
+ children: labelChild,
49
+ ...option
50
+ }
51
+ }) => ({
52
+ ...option,
53
+ label: labelChild
54
+ })), [optionsProp, children]);
60
55
  const parse = useMemo(() => multiple ? parseProp : option => parseProp(option?.value ?? null, name), [multiple, parseProp, name]);
61
56
  const format = useCallback(val => {
62
57
  if (multiple) return formatProp(val, name);
@@ -75,61 +70,58 @@ const SelectInputField = _ref => {
75
70
  return formatProp(selected, name);
76
71
  }, [formatProp, multiple, name, options]);
77
72
  const {
78
- input,
79
- meta
80
- } = useFormField(name, {
81
- disabled,
82
- format,
83
- formatOnBlur,
84
- maxLength,
85
- minLength: minLength || required ? 1 : undefined,
86
- multiple,
87
- parse,
88
- required,
89
- value,
90
- validate
91
- });
92
- const error = getError({
93
- errorProp,
94
- label,
95
- meta: meta,
73
+ getError
74
+ } = useErrors();
75
+ const {
76
+ field,
77
+ fieldState: {
78
+ error
79
+ }
80
+ } = useController({
96
81
  name,
97
- value: input.value
82
+ shouldUnregister,
83
+ rules: {
84
+ required,
85
+ minLength: minLength || required ? 1 : undefined,
86
+ maxLength,
87
+ ...rules
88
+ }
98
89
  });
99
90
  return jsx(SelectInput, {
91
+ name: field.name,
100
92
  animation: animation,
101
93
  animationDuration: animationDuration,
102
94
  animationOnChange: animationOnChange,
103
95
  className: className,
104
96
  disabled: disabled,
105
- error: error,
97
+ error: getError({
98
+ label,
99
+ minLength,
100
+ maxLength
101
+ }, error),
106
102
  id: id,
107
103
  inputId: inputId,
108
104
  isClearable: isClearable,
109
105
  isLoading: isLoading,
110
- isMulti: input.multiple,
106
+ isMulti: multiple,
111
107
  customStyle: customStyle,
112
108
  isSearchable: isSearchable,
113
109
  menuPortalTarget: menuPortalTarget,
114
- name: name,
115
110
  onBlur: event => {
116
- input.onBlur(event);
111
+ field.onBlur();
117
112
  onBlur?.(event);
118
113
  },
119
114
  onChange: (event, action) => {
120
- input.onChange(event);
115
+ field.onChange(parse(event));
121
116
  onChange?.(event, action);
122
117
  },
123
- onFocus: event => {
124
- input.onFocus(event);
125
- onFocus?.(event);
126
- },
118
+ onFocus: onFocus,
127
119
  options: options,
128
120
  placeholder: placeholder,
129
121
  readOnly: readOnly,
130
- value: input.value,
131
122
  noTopLabel: noTopLabel,
132
123
  required: required,
124
+ value: format(field.value),
133
125
  noOptionsMessage: noOptionsMessage,
134
126
  "data-testid": dataTestId,
135
127
  children: children
@@ -1,70 +1,62 @@
1
1
  import { SelectableCard } from '@ultraviolet/ui';
2
+ import { useController } from 'react-hook-form';
2
3
  import { jsx } from '@emotion/react/jsx-runtime';
3
- import { useErrors } from '../../providers/ErrorContext/index.js';
4
- import { useFormField } from '../../hooks/useFormField.js';
5
4
 
6
- const SelectableCardField = _ref => {
7
- let {
8
- name,
9
- value,
10
- onChange,
11
- showTick,
12
- type,
13
- disabled,
14
- children,
15
- className,
16
- onFocus,
17
- onBlur,
18
- required,
19
- validate,
20
- tooltip,
21
- id,
22
- label,
23
- 'data-testid': dataTestId
24
- } = _ref;
25
- const {
26
- getError
27
- } = useErrors();
5
+ const SelectableCardField = ({
6
+ name,
7
+ value,
8
+ onChange,
9
+ showTick,
10
+ type,
11
+ disabled,
12
+ children,
13
+ className,
14
+ onFocus,
15
+ onBlur,
16
+ required,
17
+ tooltip,
18
+ id,
19
+ label,
20
+ rules,
21
+ shouldUnregister = false,
22
+ 'data-testid': dataTestId
23
+ }) => {
28
24
  const {
29
- input,
30
- meta
31
- } = useFormField(name, {
32
- disabled,
33
- required,
34
- type: type ?? 'radio',
35
- validate,
36
- value
37
- });
38
- const error = getError({
39
- label: name,
40
- meta: meta,
25
+ field,
26
+ fieldState: {
27
+ error
28
+ }
29
+ } = useController({
41
30
  name,
42
- value: input.value
31
+ shouldUnregister,
32
+ rules: {
33
+ required,
34
+ ...rules
35
+ }
43
36
  });
44
37
  return jsx(SelectableCard, {
45
38
  isError: !!error,
46
39
  showTick: showTick,
47
- checked: input.checked,
40
+ checked: field.value === value,
48
41
  className: className,
49
42
  disabled: disabled,
50
- name: input.name,
51
43
  onChange: event => {
52
- input.onChange(event);
44
+ field.onChange(event);
53
45
  onChange?.(event);
54
46
  },
55
47
  onBlur: event => {
56
- input.onBlur(event);
48
+ field.onBlur();
57
49
  onBlur?.(event);
58
50
  },
59
51
  onFocus: event => {
60
- input.onFocus(event);
61
52
  onFocus?.(event);
62
53
  },
63
54
  type: type,
64
- value: input.value,
65
55
  id: id,
66
56
  tooltip: tooltip,
67
57
  label: label,
58
+ value: value ?? '',
59
+ name: field.name,
68
60
  "data-testid": dataTestId,
69
61
  children: children
70
62
  });
@@ -1,44 +1,31 @@
1
1
  import { Button } from '@ultraviolet/ui';
2
- import { useState, useEffect } from 'react';
3
- import { useFormState } from 'react-final-form';
2
+ import { useFormState } from 'react-hook-form';
4
3
  import { jsx } from '@emotion/react/jsx-runtime';
5
4
 
6
- const Submit = _ref => {
7
- let {
8
- children,
9
- className,
10
- disabled = false,
11
- icon,
12
- iconPosition,
13
- size,
14
- variant = 'filled',
15
- sentiment = 'primary',
16
- tooltip,
17
- fullWidth,
18
- onClick
19
- } = _ref;
5
+ const Submit = ({
6
+ children,
7
+ className,
8
+ disabled = false,
9
+ icon,
10
+ iconPosition,
11
+ size,
12
+ variant = 'filled',
13
+ sentiment = 'primary',
14
+ tooltip,
15
+ fullWidth,
16
+ onClick
17
+ }) => {
20
18
  const {
21
- invalid,
22
- submitting,
23
- hasValidationErrors,
24
- dirtySinceLastSubmit
25
- } = useFormState({
26
- subscription: {
27
- dirtySinceLastSubmit: true,
28
- hasValidationErrors: true,
29
- invalid: true,
30
- submitting: true
31
- }
32
- });
33
- const [isLoading, setIsLoading] = useState(true);
34
- const isDisabled = disabled || submitting || isLoading || invalid && hasValidationErrors && !dirtySinceLastSubmit;
35
- useEffect(() => setIsLoading(false), []);
19
+ isSubmitting,
20
+ isValid
21
+ } = useFormState();
22
+ const isDisabled = disabled || isSubmitting || !isValid;
36
23
  return jsx(Button, {
37
24
  className: className,
38
25
  disabled: isDisabled,
39
26
  icon: icon,
40
27
  iconPosition: iconPosition,
41
- isLoading: submitting,
28
+ isLoading: isSubmitting,
42
29
  size: size,
43
30
  type: "submit",
44
31
  variant: variant,
@@ -1,26 +1,18 @@
1
1
  import { Alert } from '@ultraviolet/ui';
2
- import { FormSpy } from 'react-final-form';
2
+ import { useFormState } from 'react-hook-form';
3
3
  import { jsx } from '@emotion/react/jsx-runtime';
4
4
 
5
- const SubmitErrorAlert = _ref => {
6
- let {
7
- className
8
- } = _ref;
9
- return jsx(FormSpy, {
10
- subscription: {
11
- submitError: true
12
- },
13
- children: _ref2 => {
14
- let {
15
- submitError
16
- } = _ref2;
17
- return submitError ? jsx(Alert, {
18
- className: className,
19
- sentiment: "danger",
20
- children: submitError
21
- }) : null;
22
- }
23
- });
5
+ const SubmitErrorAlert = ({
6
+ className
7
+ }) => {
8
+ const {
9
+ errors
10
+ } = useFormState();
11
+ return errors?.root?.['submit']?.message ? jsx(Alert, {
12
+ className: className,
13
+ sentiment: "danger",
14
+ children: errors.root['submit'].message
15
+ }) : null;
24
16
  };
25
17
 
26
18
  export { SubmitErrorAlert };
@@ -1,43 +1,42 @@
1
1
  import { TagInput } from '@ultraviolet/ui';
2
+ import { useController } from 'react-hook-form';
2
3
  import { jsx } from '@emotion/react/jsx-runtime';
3
- import { useFormField } from '../../hooks/useFormField.js';
4
4
 
5
- const TagInputField = _ref => {
6
- let {
7
- className,
8
- 'data-testid': dataTestId,
9
- disabled,
10
- id,
11
- name,
12
- onChange,
13
- placeholder,
14
- required,
15
- tags,
16
- validate,
17
- variant
18
- } = _ref;
5
+ const TagInputField = ({
6
+ className,
7
+ disabled,
8
+ id,
9
+ name,
10
+ onChange,
11
+ placeholder,
12
+ required,
13
+ rules,
14
+ variant,
15
+ shouldUnregister = false,
16
+ 'data-testid': dataTestId
17
+ }) => {
19
18
  const {
20
- input
21
- } = useFormField(name, {
22
- disabled,
23
- required,
24
- initialValue: tags,
25
- type: 'text',
26
- validate,
27
- value: tags
19
+ field
20
+ } = useController({
21
+ name,
22
+ rules: {
23
+ required,
24
+ shouldUnregister,
25
+ ...rules
26
+ }
28
27
  });
29
28
  return jsx(TagInput, {
29
+ name: field.name,
30
30
  className: className,
31
31
  disabled: disabled,
32
32
  id: id,
33
- name: name,
34
33
  onChange: event => {
34
+ field.onChange(event);
35
35
  onChange?.(event);
36
- input.onChange(event);
37
36
  },
38
37
  placeholder: placeholder,
39
38
  variant: variant,
40
- tags: input.value,
39
+ tags: field.value,
41
40
  "data-testid": dataTestId
42
41
  });
43
42
  };