@availity/mui-controlled-form 0.3.2 → 1.0.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 +44 -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/{introduction.stories.mdx → introduction.mdx} +3 -0
- 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
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
// Each exported component in the package should have its own stories file
|
|
2
|
-
|
|
3
|
-
import type { Meta, StoryObj } from '@storybook/react';
|
|
4
|
-
import { useFormContext } from 'react-hook-form';
|
|
5
|
-
import { Paper } from '@availity/mui-paper';
|
|
6
|
-
import { Typography } from '@availity/mui-typography';
|
|
7
|
-
import { Grid } from '@availity/mui-layout';
|
|
8
|
-
import { Button } from '@availity/mui-button';
|
|
9
|
-
import { ControlledForm, ControlledFormProps } from './ControlledForm';
|
|
10
|
-
import { ControlledTextField } from './TextField';
|
|
11
|
-
import * as yup from 'yup';
|
|
12
|
-
import { yupResolver } from '@hookform/resolvers/yup';
|
|
13
|
-
|
|
14
|
-
/** Deprecated. Use `FormProvider` and `useForm` directly. */
|
|
15
|
-
const meta: Meta<typeof ControlledForm> = {
|
|
16
|
-
title: 'Form Components/Controlled Form/ControlledForm',
|
|
17
|
-
component: ControlledForm,
|
|
18
|
-
tags: ['autodocs'],
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export default meta;
|
|
22
|
-
|
|
23
|
-
export const _ControlledForm: StoryObj<typeof ControlledForm> = {
|
|
24
|
-
render: ({ values, onSubmit, ...args }: ControlledFormProps) => {
|
|
25
|
-
const SubmittedValues = () => {
|
|
26
|
-
const {
|
|
27
|
-
getValues,
|
|
28
|
-
formState: { isSubmitSuccessful },
|
|
29
|
-
} = useFormContext();
|
|
30
|
-
|
|
31
|
-
return isSubmitSuccessful ? (
|
|
32
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
33
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
34
|
-
<pre data-testid="result">{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">
|
|
46
|
-
<Button disabled={!isSubmitSuccessful} children="Reset" color="secondary" onClick={() => reset()} />
|
|
47
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
48
|
-
</Grid>
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
return (
|
|
52
|
-
<ControlledForm
|
|
53
|
-
values={values}
|
|
54
|
-
onSubmit={onSubmit}
|
|
55
|
-
schema={yup.object({ controlledTextField: yup.string().max(10) })}
|
|
56
|
-
validationResolver={yupResolver}
|
|
57
|
-
{...args}
|
|
58
|
-
>
|
|
59
|
-
<ControlledTextField
|
|
60
|
-
name="controlledTextField"
|
|
61
|
-
placeholder="Enter your name"
|
|
62
|
-
label="Name"
|
|
63
|
-
inputProps={{
|
|
64
|
-
'data-testid': 'testTextField',
|
|
65
|
-
}}
|
|
66
|
-
/>
|
|
67
|
-
<Actions />
|
|
68
|
-
<SubmittedValues />
|
|
69
|
-
</ControlledForm>
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
args: {
|
|
73
|
-
values: { controlledTextField: undefined },
|
|
74
|
-
onSubmit: (data) => data,
|
|
75
|
-
},
|
|
76
|
-
};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { render, fireEvent, waitFor } from '@testing-library/react';
|
|
2
|
-
import * as yup from 'yup';
|
|
3
|
-
import { ControlledForm } from './ControlledForm';
|
|
4
|
-
import { ControlledTextField } from './TextField';
|
|
5
|
-
import { useFormContext } from 'react-hook-form';
|
|
6
|
-
import { Paper } from '@availity/mui-paper';
|
|
7
|
-
import { Typography } from '@availity/mui-typography';
|
|
8
|
-
import { Grid } from '@availity/mui-layout';
|
|
9
|
-
import { Button } from '@availity/mui-button';
|
|
10
|
-
import { yupResolver } from '@hookform/resolvers/yup';
|
|
11
|
-
|
|
12
|
-
const SubmittedValues = () => {
|
|
13
|
-
const {
|
|
14
|
-
getValues,
|
|
15
|
-
formState: { isSubmitSuccessful },
|
|
16
|
-
} = useFormContext();
|
|
17
|
-
|
|
18
|
-
return isSubmitSuccessful ? (
|
|
19
|
-
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
20
|
-
<Typography variant="h2">Submitted Values</Typography>
|
|
21
|
-
<pre data-testid="result">{JSON.stringify(getValues(), null, 2)}</pre>
|
|
22
|
-
</Paper>
|
|
23
|
-
) : null;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const Actions = () => {
|
|
27
|
-
const {
|
|
28
|
-
formState: { isSubmitSuccessful },
|
|
29
|
-
} = useFormContext();
|
|
30
|
-
return (
|
|
31
|
-
<Grid container direction="row" justifyContent="space-between">
|
|
32
|
-
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
33
|
-
</Grid>
|
|
34
|
-
);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const onSubmit = jest.fn();
|
|
38
|
-
|
|
39
|
-
describe('ControlledForm', () => {
|
|
40
|
-
test('should render successfully', () => {
|
|
41
|
-
const { getByText } = render(
|
|
42
|
-
<ControlledForm onSubmit={(data) => data} values={{}}>
|
|
43
|
-
Test
|
|
44
|
-
</ControlledForm>
|
|
45
|
-
);
|
|
46
|
-
expect(getByText('Test')).toBeTruthy();
|
|
47
|
-
});
|
|
48
|
-
test('should handle yup schema resolver', async () => {
|
|
49
|
-
const screen = render(
|
|
50
|
-
<ControlledForm
|
|
51
|
-
values={{ controlledTextField: undefined }}
|
|
52
|
-
onSubmit={onSubmit}
|
|
53
|
-
schema={yup.object({ controlledTextField: yup.string().max(10) })}
|
|
54
|
-
validationResolver={yupResolver}
|
|
55
|
-
>
|
|
56
|
-
<ControlledTextField
|
|
57
|
-
name="controlledTextField"
|
|
58
|
-
placeholder="Name"
|
|
59
|
-
inputProps={{
|
|
60
|
-
'data-testid': 'testTextField',
|
|
61
|
-
}}
|
|
62
|
-
/>
|
|
63
|
-
<Actions />
|
|
64
|
-
<SubmittedValues />
|
|
65
|
-
</ControlledForm>
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
const input = screen.getByTestId('testTextField');
|
|
69
|
-
|
|
70
|
-
fireEvent.change(input, { target: { value: 'This is too much text' } });
|
|
71
|
-
|
|
72
|
-
fireEvent.click(screen.getByText('Submit'));
|
|
73
|
-
|
|
74
|
-
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(0));
|
|
75
|
-
await waitFor(() => expect(screen.findByText('controlledTextField must be at most 10 characters')).toBeDefined());
|
|
76
|
-
});
|
|
77
|
-
});
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { FormHTMLAttributes } from 'react';
|
|
2
|
-
import { useForm, SubmitHandler, FormProvider, Resolver, UseFormProps } from 'react-hook-form';
|
|
3
|
-
|
|
4
|
-
/** @deprecated Use `UseFormProps` directly with `useForm` and `FormProvider` */
|
|
5
|
-
export type ControlledFormProps = {
|
|
6
|
-
/** This function will receive the form data if form validation is successful. */
|
|
7
|
-
onSubmit: SubmitHandler<any>;
|
|
8
|
-
/** Reactive values to update the form values. */
|
|
9
|
-
values: Record<string, any>;
|
|
10
|
-
/** The schema used by the validationResolver. */
|
|
11
|
-
schema?: unknown;
|
|
12
|
-
/** Integrates with your preferred schema validation library.
|
|
13
|
-
* More information on react-hook-form's resolvers can be
|
|
14
|
-
* found here: https://github.com/react-hook-form/resolvers#quickstart
|
|
15
|
-
*/
|
|
16
|
-
validationResolver?: (schema: unknown) => Resolver;
|
|
17
|
-
/** Additional react-hook-form `useForm` options, like `defaultValues` and validation `mode`. For more information see the [react-hook-form useForm docs](https://react-hook-form.com/docs/useform) */
|
|
18
|
-
additionalUseFormOptions?: Omit<UseFormProps, 'values' | 'resolver'>;
|
|
19
|
-
} & FormHTMLAttributes<HTMLFormElement>;
|
|
20
|
-
|
|
21
|
-
type UseFormOptions = {
|
|
22
|
-
values: Record<string, any>;
|
|
23
|
-
resolver?: Resolver;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/** @deprecated Use `FormProvider` and `useForm` directly. */
|
|
27
|
-
export const ControlledForm = ({ onSubmit, values, schema, validationResolver, additionalUseFormOptions = {mode: 'onBlur'}, ...rest }: ControlledFormProps) => {
|
|
28
|
-
const useFormOptions: UseFormOptions = { values, ...additionalUseFormOptions };
|
|
29
|
-
if (schema && validationResolver) {
|
|
30
|
-
useFormOptions.resolver = validationResolver(schema);
|
|
31
|
-
}
|
|
32
|
-
const methods = useForm(useFormOptions);
|
|
33
|
-
|
|
34
|
-
return (
|
|
35
|
-
<FormProvider {...methods}>
|
|
36
|
-
<form onSubmit={methods.handleSubmit(onSubmit)} noValidate {...rest} />
|
|
37
|
-
</FormProvider>
|
|
38
|
-
);
|
|
39
|
-
};
|