@availity/mui-controlled-form 0.2.6 → 0.3.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 +7 -0
- package/README.md +2 -2
- package/dist/index.d.mts +122 -26
- package/dist/index.d.ts +122 -26
- package/dist/index.js +174 -132
- package/dist/index.mjs +174 -131
- package/package.json +2 -2
- package/src/index.ts +47 -0
- package/src/lib/AsyncAutocomplete.stories.tsx +21 -36
- package/src/lib/AsyncAutocomplete.test.tsx +17 -53
- package/src/lib/AsyncAutocomplete.tsx +13 -6
- package/src/lib/Autocomplete.stories.tsx +20 -33
- package/src/lib/Autocomplete.test.tsx +7 -37
- package/src/lib/Autocomplete.tsx +21 -16
- package/src/lib/Checkbox.stories.tsx +48 -46
- package/src/lib/Checkbox.test.tsx +14 -46
- package/src/lib/Checkbox.tsx +19 -16
- package/src/lib/CodesAutocomplete.stories.tsx +21 -35
- package/src/lib/CodesAutocomplete.test.tsx +20 -54
- package/src/lib/CodesAutocomplete.tsx +13 -6
- package/src/lib/ControlledForm.stories.tsx +1 -0
- package/src/lib/ControlledForm.tsx +7 -3
- package/src/lib/Datepicker.stories.tsx +19 -32
- package/src/lib/Datepicker.test.tsx +3 -35
- package/src/lib/Datepicker.tsx +18 -8
- package/src/lib/Input.stories.tsx +32 -38
- package/src/lib/Input.test.tsx +71 -7
- package/src/lib/Input.tsx +30 -15
- package/src/lib/OrganizationAutocomplete.stories.tsx +30 -35
- package/src/lib/OrganizationAutocomplete.test.tsx +23 -57
- package/src/lib/OrganizationAutocomplete.tsx +14 -9
- package/src/lib/ProviderAutocomplete.stories.tsx +20 -35
- package/src/lib/ProviderAutocomplete.test.tsx +29 -63
- package/src/lib/ProviderAutocomplete.tsx +11 -5
- package/src/lib/RadioGroup.stories.tsx +41 -36
- package/src/lib/RadioGroup.test.tsx +3 -35
- package/src/lib/RadioGroup.tsx +31 -20
- package/src/lib/Select.stories.tsx +66 -93
- package/src/lib/Select.test.tsx +8 -36
- package/src/lib/Select.tsx +30 -15
- package/src/lib/TextField.stories.tsx +26 -39
- package/src/lib/TextField.test.tsx +71 -5
- package/src/lib/TextField.tsx +32 -17
- package/src/lib/Types.tsx +2489 -0
- package/src/lib/UtilComponents.tsx +52 -0
- package/docs/propDefinitions.tsx +0 -31
package/src/lib/Select.tsx
CHANGED
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
import { Select, SelectProps } from '@availity/mui-form-utils';
|
|
2
|
-
import { RegisterOptions, FieldValues, Controller
|
|
2
|
+
import { RegisterOptions, FieldValues, Controller } from 'react-hook-form';
|
|
3
|
+
import { ControllerProps, DeprecatedRulesProps } from './Types';
|
|
3
4
|
|
|
4
|
-
export type ControlledSelectProps = Omit<SelectProps,
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
export type ControlledSelectProps = Omit<SelectProps,
|
|
6
|
+
'onBlur' | 'onChange' | 'value' | 'name' | 'required'
|
|
7
|
+
> & Pick<RegisterOptions<FieldValues, string>,
|
|
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
|
+
};
|
|
7
20
|
|
|
8
21
|
export const ControlledSelect = ({
|
|
9
22
|
name,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
23
|
+
defaultValue,
|
|
24
|
+
deps,
|
|
25
|
+
disabled,
|
|
13
26
|
max,
|
|
27
|
+
maxLength,
|
|
14
28
|
min,
|
|
29
|
+
minLength,
|
|
30
|
+
onBlur,
|
|
31
|
+
onChange,
|
|
15
32
|
pattern,
|
|
33
|
+
required,
|
|
34
|
+
rules = {},
|
|
35
|
+
shouldUnregister,
|
|
16
36
|
validate,
|
|
17
|
-
disabled,
|
|
18
|
-
onChange,
|
|
19
|
-
onBlur,
|
|
20
37
|
value,
|
|
21
|
-
defaultValue,
|
|
22
|
-
shouldUnregister,
|
|
23
|
-
deps,
|
|
24
38
|
...rest
|
|
25
39
|
}: ControlledSelectProps) => {
|
|
26
40
|
return (
|
|
@@ -29,7 +43,7 @@ export const ControlledSelect = ({
|
|
|
29
43
|
defaultValue={defaultValue}
|
|
30
44
|
disabled={disabled}
|
|
31
45
|
rules={{
|
|
32
|
-
required,
|
|
46
|
+
required: typeof required === 'boolean' ? undefined : required,
|
|
33
47
|
maxLength,
|
|
34
48
|
minLength,
|
|
35
49
|
max,
|
|
@@ -41,6 +55,7 @@ export const ControlledSelect = ({
|
|
|
41
55
|
value,
|
|
42
56
|
shouldUnregister,
|
|
43
57
|
deps,
|
|
58
|
+
...rules,
|
|
44
59
|
}}
|
|
45
60
|
shouldUnregister={shouldUnregister}
|
|
46
61
|
render={({ field, fieldState: { error } }) => (
|
|
@@ -48,7 +63,7 @@ export const ControlledSelect = ({
|
|
|
48
63
|
{...rest}
|
|
49
64
|
{...field}
|
|
50
65
|
error={!!error}
|
|
51
|
-
required={
|
|
66
|
+
required={!!required}
|
|
52
67
|
/>
|
|
53
68
|
)}
|
|
54
69
|
/>
|
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
2
|
import { ControlledTextField, ControlledTextFieldProps } from './TextField';
|
|
3
|
-
import { ControlledForm } from './ControlledForm';
|
|
4
3
|
import { Button } from '@availity/mui-button';
|
|
5
|
-
import { useFormContext } from 'react-hook-form';
|
|
6
4
|
import { Paper } from '@availity/mui-paper';
|
|
7
5
|
import { Typography } from '@availity/mui-typography';
|
|
8
6
|
import { Grid } from '@availity/mui-layout';
|
|
7
|
+
import { AllControllerPropertiesCategorized, TextFieldPropsCategorized } from './Types';
|
|
8
|
+
import { FormProvider, useForm } from '..';
|
|
9
9
|
|
|
10
10
|
const meta: Meta<typeof ControlledTextField> = {
|
|
11
11
|
title: 'Form Components/Controlled Form/ControlledTextField',
|
|
12
12
|
component: ControlledTextField,
|
|
13
13
|
tags: ['autodocs'],
|
|
14
14
|
argTypes: {
|
|
15
|
+
...AllControllerPropertiesCategorized,
|
|
16
|
+
...TextFieldPropsCategorized,
|
|
15
17
|
helperText: {
|
|
16
18
|
type: 'string',
|
|
19
|
+
table: { category: 'Input Props' }
|
|
17
20
|
},
|
|
18
21
|
},
|
|
19
22
|
};
|
|
@@ -22,51 +25,35 @@ export default meta;
|
|
|
22
25
|
|
|
23
26
|
export const _ControlledTextField: StoryObj<typeof ControlledTextField> = {
|
|
24
27
|
render: (args: ControlledTextFieldProps) => {
|
|
25
|
-
const
|
|
26
|
-
const {
|
|
27
|
-
getValues,
|
|
28
|
-
formState: { isSubmitSuccessful },
|
|
29
|
-
} = useFormContext();
|
|
28
|
+
const methods = useForm({values:{ [args.name]: '' }});
|
|
30
29
|
|
|
31
|
-
return isSubmitSuccessful ? (
|
|
32
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
33
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
34
|
-
<pre>{JSON.stringify(getValues(), null, 2)}</pre>
|
|
35
|
-
</Paper>
|
|
36
|
-
) : null;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const Actions = () => {
|
|
40
|
-
const {
|
|
41
|
-
reset,
|
|
42
|
-
formState: { isSubmitSuccessful },
|
|
43
|
-
} = useFormContext();
|
|
44
|
-
return (
|
|
45
|
-
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
46
|
-
<Button
|
|
47
|
-
disabled={!isSubmitSuccessful}
|
|
48
|
-
children="Reset"
|
|
49
|
-
color="secondary"
|
|
50
|
-
onClick={() => reset({ [args.name]: '' })}
|
|
51
|
-
/>
|
|
52
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
53
|
-
</Grid>
|
|
54
|
-
);
|
|
55
|
-
};
|
|
56
30
|
return (
|
|
57
|
-
<
|
|
58
|
-
<
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
31
|
+
<FormProvider {...methods}>
|
|
32
|
+
<form onSubmit={methods.handleSubmit((data) => data)}>
|
|
33
|
+
<ControlledTextField {...args} />
|
|
34
|
+
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
35
|
+
<Button disabled={!methods?.formState?.isSubmitSuccessful} children="Reset" color="secondary" onClick={() => methods.reset()} />
|
|
36
|
+
<Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
|
|
37
|
+
</Grid>
|
|
38
|
+
{ methods?.formState?.isSubmitSuccessful ? (
|
|
39
|
+
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
40
|
+
<Typography variant="h2">Submitted Values</Typography>
|
|
41
|
+
<pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
|
|
42
|
+
</Paper>
|
|
43
|
+
) : null }
|
|
44
|
+
</form>
|
|
45
|
+
</FormProvider>
|
|
62
46
|
);
|
|
63
47
|
},
|
|
64
48
|
args: {
|
|
65
49
|
name: 'controlledTextField',
|
|
66
50
|
helperText: 'This is some helper text',
|
|
67
51
|
placeholder: 'Name',
|
|
68
|
-
required:
|
|
69
|
-
|
|
52
|
+
required: true,
|
|
53
|
+
rules: {
|
|
54
|
+
required: 'This field is required.',
|
|
55
|
+
maxLength: { value: 10, message: 'Too long' }
|
|
56
|
+
},
|
|
70
57
|
label: 'TextField Label',
|
|
71
58
|
},
|
|
72
59
|
};
|
|
@@ -6,6 +6,7 @@ import { Grid } from '@availity/mui-layout';
|
|
|
6
6
|
import { Button } from '@availity/mui-button';
|
|
7
7
|
import { ControlledForm } from './ControlledForm';
|
|
8
8
|
import { ControlledTextField } from './TextField';
|
|
9
|
+
import { TestForm } from './UtilComponents';
|
|
9
10
|
|
|
10
11
|
const SubmittedValues = () => {
|
|
11
12
|
const {
|
|
@@ -33,11 +34,12 @@ const Actions = () => {
|
|
|
33
34
|
};
|
|
34
35
|
|
|
35
36
|
const onSubmit = jest.fn();
|
|
37
|
+
const onSubmitDeprecated = jest.fn();
|
|
36
38
|
|
|
37
39
|
describe('ControlledTextField', () => {
|
|
38
|
-
test('should render the error styling if an error is returned', async () => {
|
|
40
|
+
test('Deprecated Check: should render the error styling if an error is returned', async () => {
|
|
39
41
|
const screen = render(
|
|
40
|
-
<ControlledForm values={{ controlledTextField: undefined }} onSubmit={
|
|
42
|
+
<ControlledForm values={{ controlledTextField: undefined }} onSubmit={onSubmitDeprecated}>
|
|
41
43
|
<ControlledTextField
|
|
42
44
|
name="controlledTextField"
|
|
43
45
|
helperText="This is some helper text"
|
|
@@ -59,14 +61,14 @@ describe('ControlledTextField', () => {
|
|
|
59
61
|
|
|
60
62
|
fireEvent.click(screen.getByText('Submit'));
|
|
61
63
|
|
|
62
|
-
await waitFor(() => expect(
|
|
64
|
+
await waitFor(() => expect(onSubmitDeprecated).toHaveBeenCalledTimes(0));
|
|
63
65
|
|
|
64
66
|
await waitFor(() => expect(screen.findByText('Too long')).toBeDefined());
|
|
65
67
|
});
|
|
66
68
|
|
|
67
|
-
test('should render the error styling if an error is returned', async () => {
|
|
69
|
+
test('Deprecated Check: should render the error styling if an error is returned', async () => {
|
|
68
70
|
const screen = render(
|
|
69
|
-
<ControlledForm values={{ controlledTextField: undefined }} onSubmit={
|
|
71
|
+
<ControlledForm values={{ controlledTextField: undefined }} onSubmit={onSubmitDeprecated}>
|
|
70
72
|
<ControlledTextField
|
|
71
73
|
name="controlledTextField"
|
|
72
74
|
helperText="This is some helper text"
|
|
@@ -88,6 +90,70 @@ describe('ControlledTextField', () => {
|
|
|
88
90
|
|
|
89
91
|
fireEvent.click(screen.getByText('Submit'));
|
|
90
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
|
+
test('should render the error styling if an error is returned', async () => {
|
|
103
|
+
const screen = render(
|
|
104
|
+
<TestForm UseFormOptions={{values: { controlledTextField: undefined }}} onSubmit={onSubmit}>
|
|
105
|
+
<ControlledTextField
|
|
106
|
+
name="controlledTextField"
|
|
107
|
+
helperText="This is some helper text"
|
|
108
|
+
placeholder="Name"
|
|
109
|
+
rules= {{
|
|
110
|
+
required:"This field is required.",
|
|
111
|
+
maxLength:{ value: 10, message: 'Too long' }
|
|
112
|
+
|
|
113
|
+
}}
|
|
114
|
+
inputProps={{
|
|
115
|
+
'data-testid': 'testTextField',
|
|
116
|
+
}}
|
|
117
|
+
/>
|
|
118
|
+
</TestForm>
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const input = screen.getByTestId('testTextField');
|
|
122
|
+
|
|
123
|
+
fireEvent.change(input, { target: { value: 'This is way too much text' } });
|
|
124
|
+
|
|
125
|
+
fireEvent.click(screen.getByText('Submit'));
|
|
126
|
+
|
|
127
|
+
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(0));
|
|
128
|
+
|
|
129
|
+
await waitFor(() => expect(screen.findByText('Too long')).toBeDefined());
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('should not render the error styling if no error is returned', async () => {
|
|
133
|
+
const screen = render(
|
|
134
|
+
<TestForm UseFormOptions={{values: { controlledTextField: undefined }}} onSubmit={onSubmit}>
|
|
135
|
+
<ControlledTextField
|
|
136
|
+
name="controlledTextField"
|
|
137
|
+
helperText="This is some helper text"
|
|
138
|
+
placeholder="Name"
|
|
139
|
+
rules= {{
|
|
140
|
+
required:"This field is required.",
|
|
141
|
+
maxLength:{ value: 10, message: 'Too long' }
|
|
142
|
+
|
|
143
|
+
}}
|
|
144
|
+
inputProps={{
|
|
145
|
+
'data-testid': 'testTextField',
|
|
146
|
+
}}
|
|
147
|
+
/>
|
|
148
|
+
</TestForm>
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const input = screen.getByTestId('testTextField');
|
|
152
|
+
|
|
153
|
+
fireEvent.change(input, { target: { value: 'Some Text' } });
|
|
154
|
+
|
|
155
|
+
fireEvent.click(screen.getByText('Submit'));
|
|
156
|
+
|
|
91
157
|
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
|
|
92
158
|
|
|
93
159
|
const result = screen.getByTestId('result');
|
package/src/lib/TextField.tsx
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
1
1
|
import { TextField, TextFieldProps } from '@availity/mui-textfield';
|
|
2
|
-
import { RegisterOptions, FieldValues, Controller
|
|
2
|
+
import { RegisterOptions, FieldValues, Controller } from 'react-hook-form';
|
|
3
|
+
import { ControllerProps, DeprecatedRulesProps } from './Types';
|
|
3
4
|
|
|
4
|
-
export type ControlledTextFieldProps = Omit<TextFieldProps,
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
export type ControlledTextFieldProps = Omit<TextFieldProps,
|
|
6
|
+
'onBlur' | 'onChange' | 'value' | 'name' | 'required'
|
|
7
|
+
> & Pick<RegisterOptions<FieldValues, string>,
|
|
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
|
+
};
|
|
7
20
|
|
|
8
21
|
export const ControlledTextField = ({
|
|
9
22
|
name,
|
|
23
|
+
defaultValue,
|
|
24
|
+
deps,
|
|
25
|
+
disabled,
|
|
10
26
|
helperText,
|
|
11
|
-
required,
|
|
12
|
-
maxLength,
|
|
13
|
-
minLength,
|
|
14
27
|
max,
|
|
28
|
+
maxLength,
|
|
15
29
|
min,
|
|
30
|
+
minLength,
|
|
31
|
+
onBlur,
|
|
32
|
+
onChange,
|
|
16
33
|
pattern,
|
|
34
|
+
required,
|
|
35
|
+
rules = {},
|
|
36
|
+
shouldUnregister,
|
|
17
37
|
validate,
|
|
18
|
-
disabled,
|
|
19
|
-
defaultValue,
|
|
20
|
-
onChange,
|
|
21
|
-
onBlur,
|
|
22
38
|
value,
|
|
23
|
-
shouldUnregister,
|
|
24
|
-
deps,
|
|
25
39
|
...rest
|
|
26
40
|
}: ControlledTextFieldProps) => {
|
|
27
41
|
return (
|
|
@@ -30,7 +44,7 @@ export const ControlledTextField = ({
|
|
|
30
44
|
defaultValue={defaultValue}
|
|
31
45
|
disabled={disabled}
|
|
32
46
|
rules={{
|
|
33
|
-
required,
|
|
47
|
+
required: typeof required === 'boolean' ? undefined : required,
|
|
34
48
|
maxLength,
|
|
35
49
|
minLength,
|
|
36
50
|
max,
|
|
@@ -42,13 +56,14 @@ export const ControlledTextField = ({
|
|
|
42
56
|
value,
|
|
43
57
|
shouldUnregister,
|
|
44
58
|
deps,
|
|
59
|
+
...rules,
|
|
45
60
|
}}
|
|
46
61
|
shouldUnregister={shouldUnregister}
|
|
47
|
-
render={({ field, fieldState: { error } }) => (
|
|
62
|
+
render={({ field : {ref, ...field}, fieldState: { error } }) => (
|
|
48
63
|
<TextField
|
|
49
|
-
{...rest}
|
|
50
64
|
{...field}
|
|
51
|
-
|
|
65
|
+
{...rest}
|
|
66
|
+
inputRef={ref}
|
|
52
67
|
error={!!error}
|
|
53
68
|
helperText={
|
|
54
69
|
error?.message ? (
|