@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
|
@@ -1,65 +1,59 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
2
|
import { ControlledInput, ControlledInputProps } from './Input';
|
|
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, InputPropsCategorized } from './Types';
|
|
8
|
+
import { FormProvider, useForm } from '..';
|
|
9
9
|
|
|
10
10
|
const meta: Meta<typeof ControlledInput> = {
|
|
11
11
|
title: 'Form Components/Controlled Form/ControlledInput',
|
|
12
12
|
component: ControlledInput,
|
|
13
13
|
tags: ['autodocs'],
|
|
14
|
+
argTypes: {...AllControllerPropertiesCategorized, ...InputPropsCategorized},
|
|
15
|
+
parameters: {
|
|
16
|
+
controls: {
|
|
17
|
+
exclude: [
|
|
18
|
+
'className',
|
|
19
|
+
'defaultChecked',
|
|
20
|
+
'onError',
|
|
21
|
+
'ref',
|
|
22
|
+
'style',
|
|
23
|
+
'tabIndex'
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
14
27
|
};
|
|
15
28
|
|
|
16
29
|
export default meta;
|
|
17
30
|
|
|
18
31
|
export const _ControlledInput: StoryObj<typeof ControlledInput> = {
|
|
19
32
|
render: (args: ControlledInputProps) => {
|
|
20
|
-
const
|
|
21
|
-
const {
|
|
22
|
-
getValues,
|
|
23
|
-
formState: { isSubmitSuccessful },
|
|
24
|
-
} = useFormContext();
|
|
33
|
+
const methods = useForm();
|
|
25
34
|
|
|
26
|
-
return isSubmitSuccessful ? (
|
|
27
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
28
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
29
|
-
<pre>{JSON.stringify(getValues(), null, 2)}</pre>
|
|
30
|
-
</Paper>
|
|
31
|
-
) : null;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const Actions = () => {
|
|
35
|
-
const {
|
|
36
|
-
reset,
|
|
37
|
-
formState: { isSubmitSuccessful },
|
|
38
|
-
} = useFormContext();
|
|
39
|
-
return (
|
|
40
|
-
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
41
|
-
<Button
|
|
42
|
-
disabled={!isSubmitSuccessful}
|
|
43
|
-
children="Reset"
|
|
44
|
-
color="secondary"
|
|
45
|
-
onClick={() => reset({ [args.name]: '' })}
|
|
46
|
-
/>
|
|
47
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
48
|
-
</Grid>
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
35
|
return (
|
|
52
|
-
<
|
|
53
|
-
<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
36
|
+
<FormProvider {...methods}>
|
|
37
|
+
<form onSubmit={methods.handleSubmit((data) => data)}>
|
|
38
|
+
<ControlledInput {...args} />
|
|
39
|
+
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
40
|
+
<Button disabled={!methods?.formState?.isSubmitSuccessful} children="Reset" color="secondary" onClick={() => methods.reset()} />
|
|
41
|
+
<Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
|
|
42
|
+
</Grid>
|
|
43
|
+
{ methods?.formState?.isSubmitSuccessful ? (
|
|
44
|
+
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
45
|
+
<Typography variant="h2">Submitted Values</Typography>
|
|
46
|
+
<pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
|
|
47
|
+
</Paper>
|
|
48
|
+
) : null }
|
|
49
|
+
</form>
|
|
50
|
+
</FormProvider>
|
|
57
51
|
);
|
|
58
52
|
},
|
|
59
53
|
args: {
|
|
60
54
|
name: 'controlledInput',
|
|
61
55
|
required: true,
|
|
62
|
-
maxLength: { value: 10, message: 'Too long' },
|
|
56
|
+
rules: { required:'This is required.', maxLength: { value: 10, message: 'Too long' }},
|
|
63
57
|
inputProps: { 'aria-label': 'Input Label' },
|
|
64
58
|
},
|
|
65
59
|
};
|
package/src/lib/Input.test.tsx
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { render, fireEvent, waitFor } from '@testing-library/react';
|
|
2
|
-
import { useFormContext } from 'react-hook-form';
|
|
3
2
|
import { Paper } from '@availity/mui-paper';
|
|
4
3
|
import { Typography } from '@availity/mui-typography';
|
|
5
4
|
import { Grid } from '@availity/mui-layout';
|
|
6
5
|
import { Button } from '@availity/mui-button';
|
|
7
|
-
import { ControlledForm } from './ControlledForm';
|
|
8
6
|
import { ControlledInput } from './Input';
|
|
7
|
+
import { useFormContext } from '..';
|
|
8
|
+
import { ControlledForm } from './ControlledForm';
|
|
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('ControlledInput', () => {
|
|
38
|
-
test('should render the error styling if an error is returned', async () => {
|
|
40
|
+
test('Deprecation Check: should render the error styling if an error is returned', async () => {
|
|
39
41
|
const screen = render(
|
|
40
|
-
<ControlledForm values={{ controlledInput: undefined }} onSubmit={
|
|
42
|
+
<ControlledForm values={{ controlledInput: undefined }} onSubmit={onSubmitDeprecated}>
|
|
41
43
|
<ControlledInput
|
|
42
44
|
data-testid="controlledInputWrapper"
|
|
43
45
|
name="controlledInput"
|
|
@@ -58,16 +60,16 @@ describe('ControlledInput', () => {
|
|
|
58
60
|
|
|
59
61
|
fireEvent.click(screen.getByText('Submit'));
|
|
60
62
|
|
|
61
|
-
await waitFor(() => expect(
|
|
63
|
+
await waitFor(() => expect(onSubmitDeprecated).toHaveBeenCalledTimes(0));
|
|
62
64
|
|
|
63
65
|
const muiInputBase = screen.getByTestId('controlledInputWrapper');
|
|
64
66
|
|
|
65
67
|
await waitFor(() => expect(muiInputBase.classList).toContain('Mui-error'));
|
|
66
68
|
});
|
|
67
69
|
|
|
68
|
-
test('should render the error styling if
|
|
70
|
+
test('DeprecatedCheck: should not render the error styling if no error is returned', async () => {
|
|
69
71
|
const screen = render(
|
|
70
|
-
<ControlledForm values={{ controlledInput: undefined }} onSubmit={
|
|
72
|
+
<ControlledForm values={{ controlledInput: undefined }} onSubmit={onSubmitDeprecated}>
|
|
71
73
|
<ControlledInput
|
|
72
74
|
name="controlledInput"
|
|
73
75
|
required="This field is required."
|
|
@@ -87,6 +89,68 @@ describe('ControlledInput', () => {
|
|
|
87
89
|
|
|
88
90
|
fireEvent.click(screen.getByText('Submit'));
|
|
89
91
|
|
|
92
|
+
await waitFor(() => expect(onSubmitDeprecated).toHaveBeenCalledTimes(1));
|
|
93
|
+
|
|
94
|
+
const result = screen.getByTestId('result');
|
|
95
|
+
await waitFor(() => {
|
|
96
|
+
const formValues = JSON.parse(result.innerHTML).controlledInput;
|
|
97
|
+
expect(formValues).toBe('Input Text');
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('should render the error styling if an error is returned', async () => {
|
|
102
|
+
const screen = render(
|
|
103
|
+
<TestForm UseFormOptions={{values: { controlledInput: undefined }}} onSubmit={onSubmit}>
|
|
104
|
+
<ControlledInput
|
|
105
|
+
data-testid="controlledInputWrapper"
|
|
106
|
+
name="controlledInput"
|
|
107
|
+
rules={{
|
|
108
|
+
required: "This field is required.",
|
|
109
|
+
maxLength:{ value: 10, message: 'Too long' }
|
|
110
|
+
}}
|
|
111
|
+
inputProps={{
|
|
112
|
+
'data-testid': 'testInput',
|
|
113
|
+
}}
|
|
114
|
+
/>
|
|
115
|
+
</TestForm>
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const input = screen.getByTestId('testInput');
|
|
119
|
+
|
|
120
|
+
fireEvent.change(input, { target: { value: 'This is way too much text' } });
|
|
121
|
+
|
|
122
|
+
fireEvent.click(screen.getByText('Submit'));
|
|
123
|
+
|
|
124
|
+
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(0));
|
|
125
|
+
|
|
126
|
+
const muiInputBase = screen.getByTestId('controlledInputWrapper');
|
|
127
|
+
|
|
128
|
+
await waitFor(() => expect(muiInputBase.classList).toContain('Mui-error'));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('should not render the error styling if no error is returned', async () => {
|
|
132
|
+
const screen = render(
|
|
133
|
+
<TestForm UseFormOptions={{values: { controlledInput: undefined }}} onSubmit={onSubmit}>
|
|
134
|
+
<ControlledInput
|
|
135
|
+
data-testid="controlledInputWrapper"
|
|
136
|
+
name="controlledInput"
|
|
137
|
+
rules={{
|
|
138
|
+
required: "This field is required.",
|
|
139
|
+
maxLength:{ value: 10, message: 'Too long' }
|
|
140
|
+
}}
|
|
141
|
+
inputProps={{
|
|
142
|
+
'data-testid': 'testInput',
|
|
143
|
+
}}
|
|
144
|
+
/>
|
|
145
|
+
</TestForm>
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const input = screen.getByTestId('testInput');
|
|
149
|
+
|
|
150
|
+
fireEvent.change(input, { target: { value: 'Input Text' } });
|
|
151
|
+
|
|
152
|
+
fireEvent.click(screen.getByText('Submit'));
|
|
153
|
+
|
|
90
154
|
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
|
|
91
155
|
|
|
92
156
|
const result = screen.getByTestId('result');
|
package/src/lib/Input.tsx
CHANGED
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
import { Input, InputProps } 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 ControlledInputProps = Omit<InputProps,
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
export type ControlledInputProps = Omit<InputProps,
|
|
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 ControlledInput = ({
|
|
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
|
-
defaultValue,
|
|
18
|
-
disabled,
|
|
19
|
-
onChange,
|
|
20
|
-
onBlur,
|
|
21
37
|
value,
|
|
22
|
-
shouldUnregister,
|
|
23
|
-
deps,
|
|
24
38
|
...rest
|
|
25
39
|
}: ControlledInputProps) => {
|
|
26
40
|
return (
|
|
@@ -29,7 +43,7 @@ export const ControlledInput = ({
|
|
|
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 ControlledInput = ({
|
|
|
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 ControlledInput = ({
|
|
|
48
63
|
{...rest}
|
|
49
64
|
{...field}
|
|
50
65
|
error={!!error}
|
|
51
|
-
required={
|
|
66
|
+
required={!!required}
|
|
52
67
|
/>
|
|
53
68
|
)}
|
|
54
69
|
/>
|
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
2
|
import { ControlledOrganizationAutocomplete } from './OrganizationAutocomplete';
|
|
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';
|
|
9
7
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
10
|
-
import {
|
|
8
|
+
import { AllControllerPropertiesCategorized, OrganizationAutocompletePropsCategorized } from './Types';
|
|
9
|
+
import { FormProvider, useForm } from '..';
|
|
11
10
|
|
|
12
11
|
const meta: Meta<typeof ControlledOrganizationAutocomplete> = {
|
|
13
12
|
title: 'Form Components/Controlled Form/Autocomplete/ControlledOrganizationAutocomplete',
|
|
14
13
|
component: ControlledOrganizationAutocomplete,
|
|
15
14
|
tags: ['autodocs'],
|
|
16
|
-
argTypes:
|
|
15
|
+
argTypes: {...AllControllerPropertiesCategorized, ...OrganizationAutocompletePropsCategorized},
|
|
16
|
+
parameters: {
|
|
17
|
+
controls: {
|
|
18
|
+
exclude: [
|
|
19
|
+
'max',
|
|
20
|
+
'maxLength',
|
|
21
|
+
'min',
|
|
22
|
+
'minLength'
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
17
26
|
};
|
|
18
27
|
|
|
19
28
|
export default meta;
|
|
@@ -28,45 +37,31 @@ const client = new QueryClient({
|
|
|
28
37
|
|
|
29
38
|
export const _ControlledOrganizationAutoComplete: StoryObj<typeof ControlledOrganizationAutocomplete> = {
|
|
30
39
|
render: (args) => {
|
|
31
|
-
const
|
|
32
|
-
const {
|
|
33
|
-
getValues,
|
|
34
|
-
formState: { isSubmitSuccessful },
|
|
35
|
-
} = useFormContext();
|
|
40
|
+
const methods = useForm();
|
|
36
41
|
|
|
37
|
-
return isSubmitSuccessful ? (
|
|
38
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
39
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
40
|
-
<pre>{JSON.stringify(getValues(), null, 2)}</pre>
|
|
41
|
-
</Paper>
|
|
42
|
-
) : null;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const Actions = () => {
|
|
46
|
-
const {
|
|
47
|
-
reset,
|
|
48
|
-
formState: { isSubmitSuccessful },
|
|
49
|
-
} = useFormContext();
|
|
50
|
-
return (
|
|
51
|
-
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
52
|
-
<Button disabled={!isSubmitSuccessful} children="Reset" color="secondary" onClick={() => reset()} />
|
|
53
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
54
|
-
</Grid>
|
|
55
|
-
);
|
|
56
|
-
};
|
|
57
42
|
return (
|
|
58
43
|
<QueryClientProvider client={client}>
|
|
59
|
-
<
|
|
60
|
-
<
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
44
|
+
<FormProvider {...methods}>
|
|
45
|
+
<form onSubmit={methods.handleSubmit((data) => data)}>
|
|
46
|
+
<ControlledOrganizationAutocomplete {...args} />
|
|
47
|
+
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
48
|
+
<Button disabled={!methods?.formState?.isSubmitSuccessful} children="Reset" color="secondary" onClick={() => methods.reset()} />
|
|
49
|
+
<Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
|
|
50
|
+
</Grid>
|
|
51
|
+
{ methods?.formState?.isSubmitSuccessful ? (
|
|
52
|
+
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
53
|
+
<Typography variant="h2">Submitted Values</Typography>
|
|
54
|
+
<pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
|
|
55
|
+
</Paper>
|
|
56
|
+
) : null }
|
|
57
|
+
</form>
|
|
58
|
+
</FormProvider>
|
|
64
59
|
</QueryClientProvider>
|
|
65
60
|
);
|
|
66
61
|
},
|
|
67
62
|
args: {
|
|
68
63
|
name: 'controlledOrganizationAutocomplete',
|
|
69
|
-
required:
|
|
64
|
+
rules: { required:'This is required.' },
|
|
70
65
|
FieldProps: {
|
|
71
66
|
label: 'Organization Select',
|
|
72
67
|
helperText: 'Select an Organization from the list',
|
|
@@ -1,39 +1,9 @@
|
|
|
1
1
|
import { fireEvent, render, waitFor } from '@testing-library/react';
|
|
2
2
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
3
|
-
import { Paper } from '@availity/mui-paper';
|
|
4
|
-
import { Typography } from '@availity/mui-typography';
|
|
5
|
-
import { useFormContext } from 'react-hook-form';
|
|
6
|
-
import { Grid } from '@availity/mui-layout';
|
|
7
|
-
import { Button } from '@availity/mui-button';
|
|
8
3
|
// eslint-disable-next-line @nx/enforce-module-boundaries
|
|
9
4
|
import { server } from '@availity/mock/src/lib/server';
|
|
10
|
-
import { ControlledForm } from './ControlledForm';
|
|
11
5
|
import { ControlledOrganizationAutocomplete } from './OrganizationAutocomplete';
|
|
12
|
-
|
|
13
|
-
const SubmittedValues = () => {
|
|
14
|
-
const {
|
|
15
|
-
getValues,
|
|
16
|
-
formState: { isSubmitSuccessful },
|
|
17
|
-
} = useFormContext();
|
|
18
|
-
|
|
19
|
-
return isSubmitSuccessful ? (
|
|
20
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
21
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
22
|
-
<pre data-testid="result">{JSON.stringify(getValues(), null, 2)}</pre>
|
|
23
|
-
</Paper>
|
|
24
|
-
) : null;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const Actions = () => {
|
|
28
|
-
const {
|
|
29
|
-
formState: { isSubmitSuccessful },
|
|
30
|
-
} = useFormContext();
|
|
31
|
-
return (
|
|
32
|
-
<Grid container direction="row" justifyContent="space-between">
|
|
33
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
34
|
-
</Grid>
|
|
35
|
-
);
|
|
36
|
-
};
|
|
6
|
+
import { TestForm } from './UtilComponents';
|
|
37
7
|
|
|
38
8
|
const onSubmit = jest.fn();
|
|
39
9
|
|
|
@@ -61,19 +31,17 @@ describe('ControlledOrganizationAutocomplete', () => {
|
|
|
61
31
|
test('should loadOptions successfully', async () => {
|
|
62
32
|
const screen = render(
|
|
63
33
|
<QueryClientProvider client={client}>
|
|
64
|
-
<
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
<SubmittedValues />
|
|
76
|
-
</ControlledForm>
|
|
34
|
+
<TestForm onSubmit={onSubmit}>
|
|
35
|
+
<ControlledOrganizationAutocomplete
|
|
36
|
+
name="controlledOrganizationAutocomplete"
|
|
37
|
+
FieldProps={{
|
|
38
|
+
label: 'Organization Select',
|
|
39
|
+
helperText: 'Select an Organization from the list',
|
|
40
|
+
placeholder: 'Select...',
|
|
41
|
+
fullWidth: false,
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
</TestForm>
|
|
77
45
|
</QueryClientProvider>
|
|
78
46
|
);
|
|
79
47
|
|
|
@@ -87,19 +55,17 @@ describe('ControlledOrganizationAutocomplete', () => {
|
|
|
87
55
|
test('should set the value and submit the form data', async () => {
|
|
88
56
|
const screen = render(
|
|
89
57
|
<QueryClientProvider client={client}>
|
|
90
|
-
<
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
<SubmittedValues />
|
|
102
|
-
</ControlledForm>
|
|
58
|
+
<TestForm onSubmit={onSubmit}>
|
|
59
|
+
<ControlledOrganizationAutocomplete
|
|
60
|
+
name="controlledOrganizationAutocomplete"
|
|
61
|
+
FieldProps={{
|
|
62
|
+
label: 'Organization Select',
|
|
63
|
+
helperText: 'Select an Organization from the list',
|
|
64
|
+
placeholder: 'Select...',
|
|
65
|
+
fullWidth: false,
|
|
66
|
+
}}
|
|
67
|
+
/>
|
|
68
|
+
</TestForm>
|
|
103
69
|
</QueryClientProvider>
|
|
104
70
|
);
|
|
105
71
|
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { OrganizationAutocomplete, OrgAutocompleteProps } from '@availity/mui-autocomplete';
|
|
2
|
-
import { Controller, RegisterOptions, FieldValues
|
|
2
|
+
import { Controller, RegisterOptions, FieldValues } from 'react-hook-form';
|
|
3
|
+
import { ControllerProps, DeprecatedRulesProps } from './Types';
|
|
3
4
|
|
|
4
|
-
export type ControlledOrgAutocompleteProps = Omit<OrgAutocompleteProps,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
export type ControlledOrgAutocompleteProps = Omit<OrgAutocompleteProps,
|
|
6
|
+
'onBlur' | 'onChange' | 'value' | 'name'
|
|
7
|
+
> & Pick<RegisterOptions<FieldValues, string>,
|
|
8
|
+
'onBlur' | 'onChange' | 'value'
|
|
9
|
+
> & ControllerProps
|
|
10
|
+
//TODO v1 - remove deprecated props
|
|
11
|
+
& Omit<DeprecatedRulesProps, 'max' | 'maxLength' | 'min' | 'minLength'
|
|
12
|
+
>;
|
|
10
13
|
|
|
11
14
|
export const ControlledOrganizationAutocomplete = ({
|
|
12
15
|
name,
|
|
@@ -16,6 +19,7 @@ export const ControlledOrganizationAutocomplete = ({
|
|
|
16
19
|
onChange,
|
|
17
20
|
pattern,
|
|
18
21
|
required,
|
|
22
|
+
rules = {},
|
|
19
23
|
shouldUnregister,
|
|
20
24
|
validate,
|
|
21
25
|
value,
|
|
@@ -35,14 +39,14 @@ export const ControlledOrganizationAutocomplete = ({
|
|
|
35
39
|
shouldUnregister,
|
|
36
40
|
validate,
|
|
37
41
|
value,
|
|
42
|
+
...rules,
|
|
38
43
|
}}
|
|
39
44
|
shouldUnregister={shouldUnregister}
|
|
40
|
-
render={({ field: { onChange, value, onBlur }, fieldState: { error } }) => (
|
|
45
|
+
render={({ field: { onChange, value, onBlur, ref }, fieldState: { error } }) => (
|
|
41
46
|
<OrganizationAutocomplete
|
|
42
47
|
{...rest}
|
|
43
48
|
FieldProps={{
|
|
44
49
|
...FieldProps,
|
|
45
|
-
required: typeof required === 'object' ? required.value : !!required,
|
|
46
50
|
error: !!error,
|
|
47
51
|
helperText: error?.message ? (
|
|
48
52
|
<>
|
|
@@ -53,6 +57,7 @@ export const ControlledOrganizationAutocomplete = ({
|
|
|
53
57
|
) : (
|
|
54
58
|
FieldProps?.helperText
|
|
55
59
|
),
|
|
60
|
+
inputRef:ref
|
|
56
61
|
}}
|
|
57
62
|
onChange={(event, value, reason) => {
|
|
58
63
|
if (reason === 'clear') {
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
2
|
import { ControlledProviderAutocomplete } from './ProviderAutocomplete';
|
|
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';
|
|
9
7
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
10
|
-
import {
|
|
8
|
+
import { AllControllerPropertiesCategorized, ProviderAutocompletePropsCategorized } from './Types';
|
|
9
|
+
import { FormProvider, useForm } from '..';
|
|
11
10
|
|
|
12
11
|
const meta: Meta<typeof ControlledProviderAutocomplete> = {
|
|
13
12
|
title: 'Form Components/Controlled Form/Autocomplete/ControlledProviderAutocomplete',
|
|
14
13
|
component: ControlledProviderAutocomplete,
|
|
15
14
|
tags: ['autodocs'],
|
|
16
|
-
argTypes:
|
|
15
|
+
argTypes: {...AllControllerPropertiesCategorized, ...ProviderAutocompletePropsCategorized}
|
|
17
16
|
};
|
|
18
17
|
|
|
19
18
|
export default meta;
|
|
@@ -28,39 +27,25 @@ const client = new QueryClient({
|
|
|
28
27
|
|
|
29
28
|
export const _ControlledProviderAutoComplete: StoryObj<typeof ControlledProviderAutocomplete> = {
|
|
30
29
|
render: (args) => {
|
|
31
|
-
const
|
|
32
|
-
const {
|
|
33
|
-
getValues,
|
|
34
|
-
formState: { isSubmitSuccessful },
|
|
35
|
-
} = useFormContext();
|
|
30
|
+
const methods = useForm();
|
|
36
31
|
|
|
37
|
-
return isSubmitSuccessful ? (
|
|
38
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
39
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
40
|
-
<pre>{JSON.stringify(getValues(), null, 2)}</pre>
|
|
41
|
-
</Paper>
|
|
42
|
-
) : null;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const Actions = () => {
|
|
46
|
-
const {
|
|
47
|
-
reset,
|
|
48
|
-
formState: { isSubmitSuccessful },
|
|
49
|
-
} = useFormContext();
|
|
50
|
-
return (
|
|
51
|
-
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
52
|
-
<Button disabled={!isSubmitSuccessful} children="Reset" color="secondary" onClick={() => reset()} />
|
|
53
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
54
|
-
</Grid>
|
|
55
|
-
);
|
|
56
|
-
};
|
|
57
32
|
return (
|
|
58
33
|
<QueryClientProvider client={client}>
|
|
59
|
-
<
|
|
60
|
-
<
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
34
|
+
<FormProvider {...methods}>
|
|
35
|
+
<form onSubmit={methods.handleSubmit((data) => data)}>
|
|
36
|
+
<ControlledProviderAutocomplete {...args} />
|
|
37
|
+
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
38
|
+
<Button disabled={!methods?.formState?.isSubmitSuccessful} children="Reset" color="secondary" onClick={() => methods.reset()} />
|
|
39
|
+
<Button type="submit" disabled={methods?.formState?.isSubmitSuccessful} children="Submit" />
|
|
40
|
+
</Grid>
|
|
41
|
+
{ methods?.formState?.isSubmitSuccessful ? (
|
|
42
|
+
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
43
|
+
<Typography variant="h2">Submitted Values</Typography>
|
|
44
|
+
<pre data-testid="result">{JSON.stringify(methods.getValues(), null, 2)}</pre>
|
|
45
|
+
</Paper>
|
|
46
|
+
) : null }
|
|
47
|
+
</form>
|
|
48
|
+
</FormProvider>
|
|
64
49
|
</QueryClientProvider>
|
|
65
50
|
);
|
|
66
51
|
},
|
|
@@ -74,6 +59,6 @@ export const _ControlledProviderAutoComplete: StoryObj<typeof ControlledProvider
|
|
|
74
59
|
},
|
|
75
60
|
limit: 10,
|
|
76
61
|
customerId: '1234',
|
|
77
|
-
required:
|
|
62
|
+
rules: { required:'This is required.' },
|
|
78
63
|
},
|
|
79
64
|
};
|