@availity/mui-controlled-form 0.3.2 → 1.0.0-alpha.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.
- package/CHANGELOG.md +19 -0
- package/dist/index.d.mts +30 -123
- package/dist/index.d.ts +30 -123
- package/dist/index.js +111 -301
- package/dist/index.mjs +90 -279
- package/package.json +17 -17
- package/src/index.ts +0 -1
- package/src/lib/AsyncAutocomplete.test.tsx +80 -16
- package/src/lib/AsyncAutocomplete.tsx +9 -25
- package/src/lib/Autocomplete.test.tsx +52 -6
- package/src/lib/Autocomplete.tsx +16 -33
- package/src/lib/Checkbox.tsx +11 -19
- package/src/lib/CodesAutocomplete.test.tsx +90 -17
- package/src/lib/CodesAutocomplete.tsx +6 -21
- package/src/lib/Datepicker.test.tsx +68 -1
- package/src/lib/Datepicker.tsx +6 -25
- package/src/lib/Input.test.tsx +0 -93
- package/src/lib/Input.tsx +5 -33
- package/src/lib/OrganizationAutocomplete.test.tsx +85 -18
- package/src/lib/OrganizationAutocomplete.tsx +6 -18
- package/src/lib/ProviderAutocomplete.test.tsx +99 -26
- package/src/lib/ProviderAutocomplete.tsx +8 -24
- package/src/lib/RadioGroup.tsx +13 -29
- package/src/lib/Select.tsx +5 -33
- package/src/lib/TextField.test.tsx +0 -94
- package/src/lib/TextField.tsx +6 -34
- package/src/lib/Types.tsx +811 -719
- package/src/lib/ControlledForm.stories.tsx +0 -76
- package/src/lib/ControlledForm.test.tsx +0 -77
- package/src/lib/ControlledForm.tsx +0 -39
- /package/{introduction.stories.mdx → introduction.mdx} +0 -0
|
@@ -1,30 +1,21 @@
|
|
|
1
1
|
import { ProviderAutocomplete, ProviderAutocompleteProps } from '@availity/mui-autocomplete';
|
|
2
2
|
import { Controller, RegisterOptions, FieldValues } from 'react-hook-form';
|
|
3
|
-
import { ControllerProps
|
|
3
|
+
import { ControllerProps } from './Types';
|
|
4
4
|
|
|
5
|
-
export type ControlledProviderAutocompleteProps = Omit<
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
> &
|
|
10
|
-
|
|
11
|
-
& DeprecatedRulesProps;
|
|
5
|
+
export type ControlledProviderAutocompleteProps = Omit<
|
|
6
|
+
ProviderAutocompleteProps,
|
|
7
|
+
'onBlur' | 'onChange' | 'value' | 'name'
|
|
8
|
+
> &
|
|
9
|
+
Pick<RegisterOptions<FieldValues, string>, 'onBlur' | 'onChange' | 'value'> &
|
|
10
|
+
ControllerProps;
|
|
12
11
|
|
|
13
12
|
export const ControlledProviderAutocomplete = ({
|
|
14
13
|
name,
|
|
15
14
|
defaultValue,
|
|
16
|
-
deps,
|
|
17
|
-
max,
|
|
18
|
-
maxLength,
|
|
19
|
-
min,
|
|
20
|
-
minLength,
|
|
21
15
|
onBlur,
|
|
22
16
|
onChange,
|
|
23
|
-
pattern,
|
|
24
|
-
required,
|
|
25
17
|
rules = {},
|
|
26
18
|
shouldUnregister,
|
|
27
|
-
validate,
|
|
28
19
|
value,
|
|
29
20
|
FieldProps,
|
|
30
21
|
...rest
|
|
@@ -34,17 +25,9 @@ export const ControlledProviderAutocomplete = ({
|
|
|
34
25
|
name={name}
|
|
35
26
|
defaultValue={defaultValue}
|
|
36
27
|
rules={{
|
|
37
|
-
deps,
|
|
38
|
-
max,
|
|
39
|
-
maxLength,
|
|
40
|
-
min,
|
|
41
|
-
minLength,
|
|
42
28
|
onBlur,
|
|
43
29
|
onChange,
|
|
44
|
-
pattern,
|
|
45
|
-
required,
|
|
46
30
|
shouldUnregister,
|
|
47
|
-
validate,
|
|
48
31
|
value,
|
|
49
32
|
...rules,
|
|
50
33
|
}}
|
|
@@ -53,6 +36,7 @@ export const ControlledProviderAutocomplete = ({
|
|
|
53
36
|
<ProviderAutocomplete
|
|
54
37
|
{...rest}
|
|
55
38
|
FieldProps={{
|
|
39
|
+
required: typeof rules.required === 'object' ? rules.required.value : !!rules.required,
|
|
56
40
|
...FieldProps,
|
|
57
41
|
error: !!error,
|
|
58
42
|
helperText: error?.message ? (
|
package/src/lib/RadioGroup.tsx
CHANGED
|
@@ -1,42 +1,22 @@
|
|
|
1
|
-
import { RadioGroup, RadioGroupProps } from '@availity/mui-form-utils';
|
|
1
|
+
import { FormControlProps, RadioGroup, RadioGroupProps } from '@availity/mui-form-utils';
|
|
2
2
|
import { Controller, RegisterOptions, FieldValues } from 'react-hook-form';
|
|
3
3
|
import { FormControl, FormLabel, FormHelperText } from '@availity/mui-form-utils';
|
|
4
|
-
import { ControllerProps
|
|
4
|
+
import { ControllerProps } from './Types';
|
|
5
5
|
|
|
6
6
|
export type ControlledRadioGroupProps = {
|
|
7
7
|
name: string;
|
|
8
8
|
label: string;
|
|
9
9
|
helperText?: string;
|
|
10
|
-
} & Omit
|
|
11
|
-
'onBlur' | 'onChange' | 'value'
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
> & ControllerProps
|
|
15
|
-
//TODO v1 - remove deprecated props
|
|
16
|
-
& Omit<DeprecatedRulesProps,
|
|
17
|
-
'max'
|
|
18
|
-
| 'maxLength'
|
|
19
|
-
| 'min'
|
|
20
|
-
| 'minLength'
|
|
21
|
-
| 'pattern'
|
|
22
|
-
| 'required'
|
|
23
|
-
| 'validate'
|
|
24
|
-
> & {
|
|
25
|
-
/** If `true`, will add `aria-required` to `input`.
|
|
26
|
-
*
|
|
27
|
-
* @deprecated There has been a collision of properties. The boolean value
|
|
28
|
-
* to mark the input as required will remain in future versions, but the
|
|
29
|
-
* required object for `react-hook-form` has been moved to the `rules` prop.
|
|
30
|
-
*/
|
|
31
|
-
required?: boolean | RegisterOptions['required'];
|
|
32
|
-
};
|
|
10
|
+
} & Omit<RadioGroupProps, 'onBlur' | 'onChange' | 'value' | 'name'> &
|
|
11
|
+
Pick<RegisterOptions<FieldValues, string>, 'onBlur' | 'onChange' | 'value'> &
|
|
12
|
+
ControllerProps &
|
|
13
|
+
Pick<FormControlProps, 'required'>;
|
|
33
14
|
|
|
34
15
|
export const ControlledRadioGroup = ({
|
|
35
16
|
name,
|
|
36
17
|
helperText,
|
|
37
18
|
label,
|
|
38
19
|
defaultValue,
|
|
39
|
-
deps,
|
|
40
20
|
onBlur,
|
|
41
21
|
onChange,
|
|
42
22
|
required,
|
|
@@ -49,10 +29,14 @@ export const ControlledRadioGroup = ({
|
|
|
49
29
|
<Controller
|
|
50
30
|
name={name}
|
|
51
31
|
defaultValue={defaultValue}
|
|
52
|
-
rules={{
|
|
32
|
+
rules={{ onBlur, onChange, shouldUnregister, value, ...rules }}
|
|
53
33
|
shouldUnregister={shouldUnregister}
|
|
54
|
-
render={({ field: {disabled, ...field}, fieldState: { error } }) => (
|
|
55
|
-
<FormControl
|
|
34
|
+
render={({ field: { disabled, ...field }, fieldState: { error } }) => (
|
|
35
|
+
<FormControl
|
|
36
|
+
error={!!error}
|
|
37
|
+
disabled={disabled}
|
|
38
|
+
required={!!required || (typeof rules.required === 'object' ? rules.required.value : !!rules.required)}
|
|
39
|
+
>
|
|
56
40
|
<FormLabel>{label}</FormLabel>
|
|
57
41
|
<RadioGroup {...field} {...rest} />
|
|
58
42
|
<FormHelperText>
|
package/src/lib/Select.tsx
CHANGED
|
@@ -1,39 +1,19 @@
|
|
|
1
1
|
import { Select, SelectProps } from '@availity/mui-form-utils';
|
|
2
2
|
import { RegisterOptions, FieldValues, Controller } from 'react-hook-form';
|
|
3
|
-
import { ControllerProps
|
|
3
|
+
import { ControllerProps } from './Types';
|
|
4
4
|
|
|
5
|
-
export type ControlledSelectProps = Omit<SelectProps,
|
|
6
|
-
'onBlur' | 'onChange' | 'value'
|
|
7
|
-
|
|
8
|
-
'onBlur' | 'onChange' | 'value'
|
|
9
|
-
> & ControllerProps
|
|
10
|
-
//TODO v1 - remove deprecated props
|
|
11
|
-
& Omit<DeprecatedRulesProps, 'required'> & {
|
|
12
|
-
/** If `true`, will add `aria-required` to `input`.
|
|
13
|
-
*
|
|
14
|
-
* @deprecated There has been a collision of properties. The boolean value
|
|
15
|
-
* to mark the input as required will remain in future versions, but the
|
|
16
|
-
* required object for `react-hook-form` has been moved to the `rules` prop.
|
|
17
|
-
*/
|
|
18
|
-
required?: boolean | RegisterOptions['required'];
|
|
19
|
-
};
|
|
5
|
+
export type ControlledSelectProps = Omit<SelectProps, 'onBlur' | 'onChange' | 'value' | 'name'> &
|
|
6
|
+
Pick<RegisterOptions<FieldValues, string>, 'onBlur' | 'onChange' | 'value'> &
|
|
7
|
+
ControllerProps;
|
|
20
8
|
|
|
21
9
|
export const ControlledSelect = ({
|
|
22
10
|
name,
|
|
23
11
|
defaultValue,
|
|
24
|
-
deps,
|
|
25
12
|
disabled,
|
|
26
|
-
max,
|
|
27
|
-
maxLength,
|
|
28
|
-
min,
|
|
29
|
-
minLength,
|
|
30
13
|
onBlur,
|
|
31
14
|
onChange,
|
|
32
|
-
pattern,
|
|
33
|
-
required,
|
|
34
15
|
rules = {},
|
|
35
16
|
shouldUnregister,
|
|
36
|
-
validate,
|
|
37
17
|
value,
|
|
38
18
|
...rest
|
|
39
19
|
}: ControlledSelectProps) => {
|
|
@@ -43,27 +23,19 @@ export const ControlledSelect = ({
|
|
|
43
23
|
defaultValue={defaultValue}
|
|
44
24
|
disabled={disabled}
|
|
45
25
|
rules={{
|
|
46
|
-
required: typeof required === 'boolean' ? undefined : required,
|
|
47
|
-
maxLength,
|
|
48
|
-
minLength,
|
|
49
|
-
max,
|
|
50
|
-
min,
|
|
51
|
-
pattern,
|
|
52
|
-
validate,
|
|
53
26
|
onChange,
|
|
54
27
|
onBlur,
|
|
55
28
|
value,
|
|
56
29
|
shouldUnregister,
|
|
57
|
-
deps,
|
|
58
30
|
...rules,
|
|
59
31
|
}}
|
|
60
32
|
shouldUnregister={shouldUnregister}
|
|
61
33
|
render={({ field, fieldState: { error } }) => (
|
|
62
34
|
<Select
|
|
35
|
+
required={typeof rules.required === 'object' ? rules.required.value : !!rules.required}
|
|
63
36
|
{...rest}
|
|
64
37
|
{...field}
|
|
65
38
|
error={!!error}
|
|
66
|
-
required={!!required}
|
|
67
39
|
/>
|
|
68
40
|
)}
|
|
69
41
|
/>
|
|
@@ -1,104 +1,10 @@
|
|
|
1
1
|
import { render, fireEvent, waitFor } from '@testing-library/react';
|
|
2
|
-
import { useFormContext } from 'react-hook-form';
|
|
3
|
-
import { Paper } from '@availity/mui-paper';
|
|
4
|
-
import { Typography } from '@availity/mui-typography';
|
|
5
|
-
import { Grid } from '@availity/mui-layout';
|
|
6
|
-
import { Button } from '@availity/mui-button';
|
|
7
|
-
import { ControlledForm } from './ControlledForm';
|
|
8
2
|
import { ControlledTextField } from './TextField';
|
|
9
3
|
import { TestForm } from './UtilComponents';
|
|
10
4
|
|
|
11
|
-
const SubmittedValues = () => {
|
|
12
|
-
const {
|
|
13
|
-
getValues,
|
|
14
|
-
formState: { isSubmitSuccessful },
|
|
15
|
-
} = useFormContext();
|
|
16
|
-
|
|
17
|
-
return isSubmitSuccessful ? (
|
|
18
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
19
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
20
|
-
<pre data-testid="result">{JSON.stringify(getValues(), null, 2)}</pre>
|
|
21
|
-
</Paper>
|
|
22
|
-
) : null;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const Actions = () => {
|
|
26
|
-
const {
|
|
27
|
-
formState: { isSubmitSuccessful },
|
|
28
|
-
} = useFormContext();
|
|
29
|
-
return (
|
|
30
|
-
<Grid container direction="row" justifyContent="space-between">
|
|
31
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
32
|
-
</Grid>
|
|
33
|
-
);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
5
|
const onSubmit = jest.fn();
|
|
37
|
-
const onSubmitDeprecated = jest.fn();
|
|
38
6
|
|
|
39
7
|
describe('ControlledTextField', () => {
|
|
40
|
-
test('Deprecated Check: should render the error styling if an error is returned', async () => {
|
|
41
|
-
const screen = render(
|
|
42
|
-
<ControlledForm values={{ controlledTextField: undefined }} onSubmit={onSubmitDeprecated}>
|
|
43
|
-
<ControlledTextField
|
|
44
|
-
name="controlledTextField"
|
|
45
|
-
helperText="This is some helper text"
|
|
46
|
-
placeholder="Name"
|
|
47
|
-
required="This field is required."
|
|
48
|
-
maxLength={{ value: 10, message: 'Too long' }}
|
|
49
|
-
inputProps={{
|
|
50
|
-
'data-testid': 'testTextField',
|
|
51
|
-
}}
|
|
52
|
-
/>
|
|
53
|
-
<Actions />
|
|
54
|
-
<SubmittedValues />
|
|
55
|
-
</ControlledForm>
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
const input = screen.getByTestId('testTextField');
|
|
59
|
-
|
|
60
|
-
fireEvent.change(input, { target: { value: 'This is way too much text' } });
|
|
61
|
-
|
|
62
|
-
fireEvent.click(screen.getByText('Submit'));
|
|
63
|
-
|
|
64
|
-
await waitFor(() => expect(onSubmitDeprecated).toHaveBeenCalledTimes(0));
|
|
65
|
-
|
|
66
|
-
await waitFor(() => expect(screen.findByText('Too long')).toBeDefined());
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('Deprecated Check: should render the error styling if an error is returned', async () => {
|
|
70
|
-
const screen = render(
|
|
71
|
-
<ControlledForm values={{ controlledTextField: undefined }} onSubmit={onSubmitDeprecated}>
|
|
72
|
-
<ControlledTextField
|
|
73
|
-
name="controlledTextField"
|
|
74
|
-
helperText="This is some helper text"
|
|
75
|
-
placeholder="Name"
|
|
76
|
-
required="This field is required."
|
|
77
|
-
maxLength={{ value: 10, message: 'Too long' }}
|
|
78
|
-
inputProps={{
|
|
79
|
-
'data-testid': 'testTextField',
|
|
80
|
-
}}
|
|
81
|
-
/>
|
|
82
|
-
<Actions />
|
|
83
|
-
<SubmittedValues />
|
|
84
|
-
</ControlledForm>
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
const input = screen.getByTestId('testTextField');
|
|
88
|
-
|
|
89
|
-
fireEvent.change(input, { target: { value: 'Some Text' } });
|
|
90
|
-
|
|
91
|
-
fireEvent.click(screen.getByText('Submit'));
|
|
92
|
-
|
|
93
|
-
await waitFor(() => expect(onSubmitDeprecated).toHaveBeenCalledTimes(1));
|
|
94
|
-
|
|
95
|
-
const result = screen.getByTestId('result');
|
|
96
|
-
await waitFor(() => {
|
|
97
|
-
const formValues = JSON.parse(result.innerHTML).controlledTextField;
|
|
98
|
-
expect(formValues).toBe('Some Text');
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
8
|
test('should render the error styling if an error is returned', async () => {
|
|
103
9
|
const screen = render(
|
|
104
10
|
<TestForm UseFormOptions={{values: { controlledTextField: undefined }}} onSubmit={onSubmit}>
|
package/src/lib/TextField.tsx
CHANGED
|
@@ -1,40 +1,20 @@
|
|
|
1
1
|
import { TextField, TextFieldProps } from '@availity/mui-textfield';
|
|
2
2
|
import { RegisterOptions, FieldValues, Controller } from 'react-hook-form';
|
|
3
|
-
import { ControllerProps
|
|
3
|
+
import { ControllerProps } from './Types';
|
|
4
4
|
|
|
5
|
-
export type ControlledTextFieldProps = Omit<TextFieldProps,
|
|
6
|
-
'onBlur' | 'onChange' | 'value'
|
|
7
|
-
|
|
8
|
-
'onBlur' | 'onChange' | 'value'
|
|
9
|
-
> & ControllerProps
|
|
10
|
-
//TODO v1 - remove deprecated props
|
|
11
|
-
& Omit<DeprecatedRulesProps, 'required'> & {
|
|
12
|
-
/** If `true`, will add required asterisk to `label` and `aria-required` to `input`.
|
|
13
|
-
*
|
|
14
|
-
* @deprecated There has been a collision of properties. The boolean value
|
|
15
|
-
* to mark the input as required will remain in future versions, but the
|
|
16
|
-
* required object for `react-hook-form` has been moved to the `rules` prop.
|
|
17
|
-
*/
|
|
18
|
-
required?: boolean | RegisterOptions['required'];
|
|
19
|
-
};
|
|
5
|
+
export type ControlledTextFieldProps = Omit<TextFieldProps, 'onBlur' | 'onChange' | 'value' | 'name'> &
|
|
6
|
+
Pick<RegisterOptions<FieldValues, string>, 'onBlur' | 'onChange' | 'value'> &
|
|
7
|
+
ControllerProps;
|
|
20
8
|
|
|
21
9
|
export const ControlledTextField = ({
|
|
22
10
|
name,
|
|
23
11
|
defaultValue,
|
|
24
|
-
deps,
|
|
25
12
|
disabled,
|
|
26
13
|
helperText,
|
|
27
|
-
max,
|
|
28
|
-
maxLength,
|
|
29
|
-
min,
|
|
30
|
-
minLength,
|
|
31
14
|
onBlur,
|
|
32
15
|
onChange,
|
|
33
|
-
pattern,
|
|
34
|
-
required,
|
|
35
16
|
rules = {},
|
|
36
17
|
shouldUnregister,
|
|
37
|
-
validate,
|
|
38
18
|
value,
|
|
39
19
|
...rest
|
|
40
20
|
}: ControlledTextFieldProps) => {
|
|
@@ -44,27 +24,19 @@ export const ControlledTextField = ({
|
|
|
44
24
|
defaultValue={defaultValue}
|
|
45
25
|
disabled={disabled}
|
|
46
26
|
rules={{
|
|
47
|
-
required: typeof required === 'boolean' ? undefined : required,
|
|
48
|
-
maxLength,
|
|
49
|
-
minLength,
|
|
50
|
-
max,
|
|
51
|
-
min,
|
|
52
|
-
pattern,
|
|
53
|
-
validate,
|
|
54
27
|
onChange,
|
|
55
28
|
onBlur,
|
|
56
29
|
value,
|
|
57
30
|
shouldUnregister,
|
|
58
|
-
deps,
|
|
59
31
|
...rules,
|
|
60
32
|
}}
|
|
61
33
|
shouldUnregister={shouldUnregister}
|
|
62
|
-
render={({ field
|
|
34
|
+
render={({ field: { ref, ...field }, fieldState: { error } }) => (
|
|
63
35
|
<TextField
|
|
36
|
+
required={typeof rules.required === 'object' ? rules.required.value : !!rules.required}
|
|
64
37
|
{...field}
|
|
65
38
|
{...rest}
|
|
66
39
|
inputRef={ref}
|
|
67
|
-
required={!!required}
|
|
68
40
|
error={!!error}
|
|
69
41
|
helperText={
|
|
70
42
|
error?.message ? (
|