@availity/mui-controlled-form 0.2.5 → 0.2.6
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/dist/index.d.mts +8 -16
- package/dist/index.d.ts +8 -16
- package/dist/index.js +128 -132
- package/dist/index.mjs +144 -148
- package/package.json +1 -1
- package/src/lib/AsyncAutocomplete.tsx +12 -16
- package/src/lib/Autocomplete.tsx +12 -16
- package/src/lib/Checkbox.stories.tsx +10 -5
- package/src/lib/Checkbox.tsx +26 -14
- package/src/lib/CodesAutocomplete.tsx +12 -16
- package/src/lib/ControlledForm.tsx +1 -1
- package/src/lib/Datepicker.tsx +1 -3
- package/src/lib/Input.stories.tsx +7 -2
- package/src/lib/Input.tsx +20 -15
- package/src/lib/OrganizationAutocomplete.tsx +12 -16
- package/src/lib/ProviderAutocomplete.tsx +12 -16
- package/src/lib/RadioGroup.tsx +5 -8
- package/src/lib/Select.stories.tsx +62 -2
- package/src/lib/Select.tsx +20 -16
- package/src/lib/TextField.stories.tsx +7 -2
- package/src/lib/TextField.tsx +31 -28
package/src/lib/Autocomplete.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Autocomplete, AutocompleteProps } from '@availity/mui-autocomplete';
|
|
2
|
-
import {
|
|
2
|
+
import { RegisterOptions, FieldValues, Controller, ControllerProps } from 'react-hook-form';
|
|
3
3
|
import { ChipTypeMap } from '@mui/material/Chip';
|
|
4
4
|
|
|
5
5
|
export type ControlledAutocompleteProps<
|
|
@@ -39,11 +39,8 @@ export const ControlledAutocomplete = <
|
|
|
39
39
|
value,
|
|
40
40
|
...rest
|
|
41
41
|
}: ControlledAutocompleteProps<T, Multiple, DisableClearable, FreeSolo, ChipComponent>) => {
|
|
42
|
-
const { control, getFieldState } = useFormContext();
|
|
43
|
-
const errorMessage = getFieldState(name).error?.message;
|
|
44
42
|
return (
|
|
45
43
|
<Controller
|
|
46
|
-
control={control}
|
|
47
44
|
name={name}
|
|
48
45
|
defaultValue={defaultValue}
|
|
49
46
|
rules={{
|
|
@@ -61,23 +58,22 @@ export const ControlledAutocomplete = <
|
|
|
61
58
|
value,
|
|
62
59
|
}}
|
|
63
60
|
shouldUnregister={shouldUnregister}
|
|
64
|
-
render={({ field: { onChange, value, onBlur } }) => (
|
|
61
|
+
render={({ field: { onChange, value, onBlur }, fieldState: { error } }) => (
|
|
65
62
|
<Autocomplete
|
|
66
63
|
{...rest}
|
|
67
64
|
FieldProps={{
|
|
68
65
|
...FieldProps,
|
|
69
66
|
required: typeof required === 'object' ? required.value : !!required,
|
|
70
|
-
error: !!
|
|
71
|
-
helperText:
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
),
|
|
67
|
+
error: !!error,
|
|
68
|
+
helperText: error?.message ? (
|
|
69
|
+
<>
|
|
70
|
+
{error.message}
|
|
71
|
+
<br />
|
|
72
|
+
{FieldProps?.helperText}
|
|
73
|
+
</>
|
|
74
|
+
) : (
|
|
75
|
+
FieldProps?.helperText
|
|
76
|
+
),
|
|
81
77
|
}}
|
|
82
78
|
onChange={(event, value, reason) => {
|
|
83
79
|
if (reason === 'clear') {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
-
import { ControlledCheckbox } from './Checkbox';
|
|
2
|
+
import { ControlledCheckbox, ControlledCheckboxProps } from './Checkbox';
|
|
3
3
|
import { ControlledForm } from './ControlledForm';
|
|
4
4
|
import { Button } from '@availity/mui-button';
|
|
5
5
|
import { useFormContext } from 'react-hook-form';
|
|
@@ -17,7 +17,7 @@ const meta: Meta<typeof ControlledCheckbox> = {
|
|
|
17
17
|
export default meta;
|
|
18
18
|
|
|
19
19
|
export const _ControlledCheckbox: StoryObj<typeof ControlledCheckbox> = {
|
|
20
|
-
render: () => {
|
|
20
|
+
render: (args: ControlledCheckboxProps) => {
|
|
21
21
|
const SubmittedValues = () => {
|
|
22
22
|
const {
|
|
23
23
|
getValues,
|
|
@@ -39,19 +39,24 @@ export const _ControlledCheckbox: StoryObj<typeof ControlledCheckbox> = {
|
|
|
39
39
|
} = useFormContext();
|
|
40
40
|
return (
|
|
41
41
|
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
42
|
-
<Button
|
|
42
|
+
<Button
|
|
43
|
+
disabled={!isSubmitSuccessful}
|
|
44
|
+
children="Reset"
|
|
45
|
+
color="secondary"
|
|
46
|
+
onClick={() => reset({ [args.name]: false })}
|
|
47
|
+
/>
|
|
43
48
|
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
44
49
|
</Grid>
|
|
45
50
|
);
|
|
46
51
|
};
|
|
47
52
|
return (
|
|
48
|
-
<ControlledForm onSubmit={(data) => data} values={{
|
|
53
|
+
<ControlledForm onSubmit={(data) => data} values={{ [args.name]: false }} noValidate>
|
|
49
54
|
<FormControl>
|
|
50
55
|
<FormLabel id="radio-group" component="div">
|
|
51
56
|
Radio Group
|
|
52
57
|
</FormLabel>
|
|
53
58
|
<FormGroup>
|
|
54
|
-
<FormControlLabel label="Option 1" control={<ControlledCheckbox
|
|
59
|
+
<FormControlLabel label="Option 1" control={<ControlledCheckbox {...args} />} />
|
|
55
60
|
<FormControlLabel label="Option 2" control={<ControlledCheckbox name="Option 2" />} />
|
|
56
61
|
<FormControlLabel label="Option 3" control={<ControlledCheckbox name="Option 3" />} />
|
|
57
62
|
</FormGroup>
|
package/src/lib/Checkbox.tsx
CHANGED
|
@@ -1,37 +1,49 @@
|
|
|
1
1
|
import { Checkbox, CheckboxProps } from '@availity/mui-checkbox';
|
|
2
|
-
import {
|
|
2
|
+
import { RegisterOptions, FieldValues, Controller, ControllerProps } from 'react-hook-form';
|
|
3
3
|
|
|
4
|
-
export type ControlledCheckboxProps = CheckboxProps &
|
|
5
|
-
|
|
6
|
-
} & Omit<
|
|
4
|
+
export type ControlledCheckboxProps = CheckboxProps &
|
|
5
|
+
Omit<
|
|
7
6
|
RegisterOptions<FieldValues, string>,
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
| 'required'
|
|
8
|
+
| 'disabled'
|
|
9
|
+
| 'valueAsNumber'
|
|
10
|
+
| 'valueAsDate'
|
|
11
|
+
| 'setValueAs'
|
|
12
|
+
| 'max'
|
|
13
|
+
| 'maxLength'
|
|
14
|
+
| 'min'
|
|
15
|
+
| 'minLength'
|
|
16
|
+
| 'pattern'
|
|
17
|
+
> &
|
|
18
|
+
Pick<ControllerProps, 'defaultValue' | 'shouldUnregister' | 'name'>;
|
|
10
19
|
|
|
11
20
|
export const ControlledCheckbox = ({
|
|
12
21
|
name,
|
|
13
|
-
setValueAs,
|
|
14
22
|
disabled,
|
|
15
23
|
onChange,
|
|
16
24
|
onBlur,
|
|
17
25
|
value,
|
|
26
|
+
defaultValue = false,
|
|
18
27
|
shouldUnregister,
|
|
19
28
|
deps,
|
|
20
29
|
...rest
|
|
21
30
|
}: ControlledCheckboxProps) => {
|
|
22
|
-
const { register } = useFormContext();
|
|
23
31
|
return (
|
|
24
|
-
<
|
|
25
|
-
{
|
|
26
|
-
{
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
<Controller
|
|
33
|
+
name={name}
|
|
34
|
+
defaultValue={defaultValue}
|
|
35
|
+
disabled={disabled}
|
|
36
|
+
rules={{
|
|
29
37
|
onChange,
|
|
30
38
|
onBlur,
|
|
31
39
|
value,
|
|
32
40
|
shouldUnregister,
|
|
33
41
|
deps,
|
|
34
|
-
}
|
|
42
|
+
}}
|
|
43
|
+
shouldUnregister={shouldUnregister}
|
|
44
|
+
render={({ field }) => (
|
|
45
|
+
<Checkbox {...rest} {...field} checked={field.value} onChange={(e) => field.onChange(e.target.checked)} />
|
|
46
|
+
)}
|
|
35
47
|
/>
|
|
36
48
|
);
|
|
37
49
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CodesAutocomplete, CodesAutocompleteProps } from '@availity/mui-autocomplete';
|
|
2
|
-
import {
|
|
2
|
+
import { Controller, RegisterOptions, ControllerProps, FieldValues } from 'react-hook-form';
|
|
3
3
|
|
|
4
4
|
export type ControlledCodesAutocompleteProps = Omit<CodesAutocompleteProps, 'name'> &
|
|
5
5
|
Omit<RegisterOptions<FieldValues, string>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> &
|
|
@@ -21,12 +21,9 @@ export const ControlledCodesAutocomplete = ({
|
|
|
21
21
|
FieldProps,
|
|
22
22
|
...rest
|
|
23
23
|
}: ControlledCodesAutocompleteProps) => {
|
|
24
|
-
const { control, getFieldState } = useFormContext();
|
|
25
|
-
const errorMessage = getFieldState(name).error?.message;
|
|
26
24
|
return (
|
|
27
25
|
<Controller
|
|
28
26
|
name={name}
|
|
29
|
-
control={control}
|
|
30
27
|
defaultValue={defaultValue}
|
|
31
28
|
rules={{
|
|
32
29
|
deps,
|
|
@@ -41,23 +38,22 @@ export const ControlledCodesAutocomplete = ({
|
|
|
41
38
|
value,
|
|
42
39
|
}}
|
|
43
40
|
shouldUnregister={shouldUnregister}
|
|
44
|
-
render={({ field: { onChange, value, onBlur } }) => (
|
|
41
|
+
render={({ field: { onChange, value, onBlur }, fieldState: { error } }) => (
|
|
45
42
|
<CodesAutocomplete
|
|
46
43
|
{...rest}
|
|
47
44
|
FieldProps={{
|
|
48
45
|
...FieldProps,
|
|
49
46
|
required: typeof required === 'object' ? required.value : !!required,
|
|
50
|
-
error: !!
|
|
51
|
-
helperText:
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
),
|
|
47
|
+
error: !!error,
|
|
48
|
+
helperText: error?.message ? (
|
|
49
|
+
<>
|
|
50
|
+
{error.message}
|
|
51
|
+
<br />
|
|
52
|
+
{FieldProps?.helperText}
|
|
53
|
+
</>
|
|
54
|
+
) : (
|
|
55
|
+
FieldProps?.helperText
|
|
56
|
+
),
|
|
61
57
|
}}
|
|
62
58
|
onChange={(event, value, reason) => {
|
|
63
59
|
if (reason === 'clear') {
|
|
@@ -29,7 +29,7 @@ export const ControlledForm = ({ onSubmit, values, schema, validationResolver, .
|
|
|
29
29
|
|
|
30
30
|
return (
|
|
31
31
|
<FormProvider {...methods}>
|
|
32
|
-
<form onSubmit={methods.handleSubmit(onSubmit)} {...rest} />
|
|
32
|
+
<form onSubmit={methods.handleSubmit(onSubmit)} noValidate {...rest} />
|
|
33
33
|
</FormProvider>
|
|
34
34
|
);
|
|
35
35
|
};
|
package/src/lib/Datepicker.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Datepicker, DatepickerProps } from '@availity/mui-datepicker';
|
|
2
|
-
import {
|
|
2
|
+
import { RegisterOptions, FieldValues, Controller, ControllerProps } from 'react-hook-form';
|
|
3
3
|
|
|
4
4
|
export type ControlledDatepickerProps = DatepickerProps &
|
|
5
5
|
Omit<RegisterOptions<FieldValues, string>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> &
|
|
@@ -23,11 +23,9 @@ export const ControlledDatepicker = ({
|
|
|
23
23
|
FieldProps,
|
|
24
24
|
...rest
|
|
25
25
|
}: ControlledDatepickerProps) => {
|
|
26
|
-
const { control } = useFormContext();
|
|
27
26
|
return (
|
|
28
27
|
<Controller
|
|
29
28
|
name={name}
|
|
30
|
-
control={control}
|
|
31
29
|
defaultValue={defaultValue}
|
|
32
30
|
rules={{
|
|
33
31
|
deps,
|
|
@@ -38,13 +38,18 @@ export const _ControlledInput: StoryObj<typeof ControlledInput> = {
|
|
|
38
38
|
} = useFormContext();
|
|
39
39
|
return (
|
|
40
40
|
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
41
|
-
<Button
|
|
41
|
+
<Button
|
|
42
|
+
disabled={!isSubmitSuccessful}
|
|
43
|
+
children="Reset"
|
|
44
|
+
color="secondary"
|
|
45
|
+
onClick={() => reset({ [args.name]: '' })}
|
|
46
|
+
/>
|
|
42
47
|
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
43
48
|
</Grid>
|
|
44
49
|
);
|
|
45
50
|
};
|
|
46
51
|
return (
|
|
47
|
-
<ControlledForm values={{
|
|
52
|
+
<ControlledForm values={{ [args.name]: '' }} onSubmit={(data) => data}>
|
|
48
53
|
<ControlledInput {...args} />
|
|
49
54
|
<Actions />
|
|
50
55
|
<SubmittedValues />
|
package/src/lib/Input.tsx
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Input, InputProps } from '@availity/mui-form-utils';
|
|
2
|
-
import {
|
|
2
|
+
import { RegisterOptions, FieldValues, Controller, ControllerProps } from 'react-hook-form';
|
|
3
3
|
|
|
4
|
-
export type ControlledInputProps = Omit<InputProps, 'error' | 'name' | 'required'> &
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
>;
|
|
4
|
+
export type ControlledInputProps = Omit<InputProps, 'error' | 'name' | 'required'> &
|
|
5
|
+
Omit<RegisterOptions<FieldValues, string>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> &
|
|
6
|
+
Pick<ControllerProps, 'defaultValue' | 'shouldUnregister' | 'name'>;
|
|
8
7
|
|
|
9
8
|
export const ControlledInput = ({
|
|
10
9
|
name,
|
|
@@ -15,7 +14,7 @@ export const ControlledInput = ({
|
|
|
15
14
|
min,
|
|
16
15
|
pattern,
|
|
17
16
|
validate,
|
|
18
|
-
|
|
17
|
+
defaultValue,
|
|
19
18
|
disabled,
|
|
20
19
|
onChange,
|
|
21
20
|
onBlur,
|
|
@@ -24,13 +23,12 @@ export const ControlledInput = ({
|
|
|
24
23
|
deps,
|
|
25
24
|
...rest
|
|
26
25
|
}: ControlledInputProps) => {
|
|
27
|
-
const { register, getFieldState } = useFormContext();
|
|
28
26
|
return (
|
|
29
|
-
<
|
|
30
|
-
{
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
{
|
|
27
|
+
<Controller
|
|
28
|
+
name={name}
|
|
29
|
+
defaultValue={defaultValue}
|
|
30
|
+
disabled={disabled}
|
|
31
|
+
rules={{
|
|
34
32
|
required,
|
|
35
33
|
maxLength,
|
|
36
34
|
minLength,
|
|
@@ -38,14 +36,21 @@ export const ControlledInput = ({
|
|
|
38
36
|
min,
|
|
39
37
|
pattern,
|
|
40
38
|
validate,
|
|
41
|
-
setValueAs,
|
|
42
|
-
disabled,
|
|
43
39
|
onChange,
|
|
44
40
|
onBlur,
|
|
45
41
|
value,
|
|
46
42
|
shouldUnregister,
|
|
47
43
|
deps,
|
|
48
|
-
}
|
|
44
|
+
}}
|
|
45
|
+
shouldUnregister={shouldUnregister}
|
|
46
|
+
render={({ field, fieldState: { error } }) => (
|
|
47
|
+
<Input
|
|
48
|
+
{...rest}
|
|
49
|
+
{...field}
|
|
50
|
+
error={!!error}
|
|
51
|
+
required={typeof required === 'object' ? required.value : !!required}
|
|
52
|
+
/>
|
|
53
|
+
)}
|
|
49
54
|
/>
|
|
50
55
|
);
|
|
51
56
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OrganizationAutocomplete, OrgAutocompleteProps } from '@availity/mui-autocomplete';
|
|
2
|
-
import {
|
|
2
|
+
import { Controller, RegisterOptions, FieldValues, ControllerProps } from 'react-hook-form';
|
|
3
3
|
|
|
4
4
|
export type ControlledOrgAutocompleteProps = Omit<OrgAutocompleteProps, 'name'> &
|
|
5
5
|
Omit<
|
|
@@ -22,12 +22,9 @@ export const ControlledOrganizationAutocomplete = ({
|
|
|
22
22
|
FieldProps,
|
|
23
23
|
...rest
|
|
24
24
|
}: ControlledOrgAutocompleteProps) => {
|
|
25
|
-
const { control, getFieldState } = useFormContext();
|
|
26
|
-
const errorMessage = getFieldState(name).error?.message;
|
|
27
25
|
return (
|
|
28
26
|
<Controller
|
|
29
27
|
name={name}
|
|
30
|
-
control={control}
|
|
31
28
|
defaultValue={defaultValue}
|
|
32
29
|
rules={{
|
|
33
30
|
deps,
|
|
@@ -40,23 +37,22 @@ export const ControlledOrganizationAutocomplete = ({
|
|
|
40
37
|
value,
|
|
41
38
|
}}
|
|
42
39
|
shouldUnregister={shouldUnregister}
|
|
43
|
-
render={({ field: { onChange, value, onBlur } }) => (
|
|
40
|
+
render={({ field: { onChange, value, onBlur }, fieldState: { error } }) => (
|
|
44
41
|
<OrganizationAutocomplete
|
|
45
42
|
{...rest}
|
|
46
43
|
FieldProps={{
|
|
47
44
|
...FieldProps,
|
|
48
45
|
required: typeof required === 'object' ? required.value : !!required,
|
|
49
|
-
error: !!
|
|
50
|
-
helperText:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
),
|
|
46
|
+
error: !!error,
|
|
47
|
+
helperText: error?.message ? (
|
|
48
|
+
<>
|
|
49
|
+
{error.message}
|
|
50
|
+
<br />
|
|
51
|
+
{FieldProps?.helperText}
|
|
52
|
+
</>
|
|
53
|
+
) : (
|
|
54
|
+
FieldProps?.helperText
|
|
55
|
+
),
|
|
60
56
|
}}
|
|
61
57
|
onChange={(event, value, reason) => {
|
|
62
58
|
if (reason === 'clear') {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ProviderAutocomplete, ProviderAutocompleteProps } from '@availity/mui-autocomplete';
|
|
2
|
-
import {
|
|
2
|
+
import { Controller, RegisterOptions, FieldValues, ControllerProps } from 'react-hook-form';
|
|
3
3
|
|
|
4
4
|
export type ControlledProviderAutocompleteProps = Omit<ProviderAutocompleteProps, 'name'> &
|
|
5
5
|
Omit<RegisterOptions<FieldValues, string>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> &
|
|
@@ -23,12 +23,9 @@ export const ControlledProviderAutocomplete = ({
|
|
|
23
23
|
FieldProps,
|
|
24
24
|
...rest
|
|
25
25
|
}: ControlledProviderAutocompleteProps) => {
|
|
26
|
-
const { control, getFieldState } = useFormContext();
|
|
27
|
-
const errorMessage = getFieldState(name).error?.message;
|
|
28
26
|
return (
|
|
29
27
|
<Controller
|
|
30
28
|
name={name}
|
|
31
|
-
control={control}
|
|
32
29
|
defaultValue={defaultValue}
|
|
33
30
|
rules={{
|
|
34
31
|
deps,
|
|
@@ -45,23 +42,22 @@ export const ControlledProviderAutocomplete = ({
|
|
|
45
42
|
value,
|
|
46
43
|
}}
|
|
47
44
|
shouldUnregister={shouldUnregister}
|
|
48
|
-
render={({ field: { onChange, value, onBlur } }) => (
|
|
45
|
+
render={({ field: { onChange, value, onBlur }, fieldState: { error } }) => (
|
|
49
46
|
<ProviderAutocomplete
|
|
50
47
|
{...rest}
|
|
51
48
|
FieldProps={{
|
|
52
49
|
...FieldProps,
|
|
53
50
|
required: typeof required === 'object' ? required.value : !!required,
|
|
54
|
-
error: !!
|
|
55
|
-
helperText:
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
),
|
|
51
|
+
error: !!error,
|
|
52
|
+
helperText: error?.message ? (
|
|
53
|
+
<>
|
|
54
|
+
{error.message}
|
|
55
|
+
<br />
|
|
56
|
+
{FieldProps?.helperText}
|
|
57
|
+
</>
|
|
58
|
+
) : (
|
|
59
|
+
FieldProps?.helperText
|
|
60
|
+
),
|
|
65
61
|
}}
|
|
66
62
|
onChange={(event, value, reason) => {
|
|
67
63
|
if (reason === 'clear') {
|
package/src/lib/RadioGroup.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RadioGroup, RadioGroupProps } from '@availity/mui-form-utils';
|
|
2
|
-
import {
|
|
2
|
+
import { ControllerProps, Controller, RegisterOptions, FieldValues } from 'react-hook-form';
|
|
3
3
|
import { FormControl, FormLabel, FormHelperText } from '@availity/mui-form-utils';
|
|
4
4
|
|
|
5
5
|
export type ControlledRadioGroupProps = RadioGroupProps & {
|
|
@@ -34,23 +34,20 @@ export const ControlledRadioGroup = ({
|
|
|
34
34
|
value,
|
|
35
35
|
...rest
|
|
36
36
|
}: ControlledRadioGroupProps) => {
|
|
37
|
-
const { control, getFieldState } = useFormContext();
|
|
38
|
-
const errorMessage = getFieldState(name).error?.message;
|
|
39
37
|
return (
|
|
40
38
|
<Controller
|
|
41
|
-
control={control}
|
|
42
39
|
name={name}
|
|
43
40
|
defaultValue={defaultValue}
|
|
44
41
|
rules={{ deps, onBlur, onChange, required, shouldUnregister, value }}
|
|
45
42
|
shouldUnregister={shouldUnregister}
|
|
46
|
-
render={({ field }) => (
|
|
47
|
-
<FormControl error={!!
|
|
43
|
+
render={({ field, fieldState: { error } }) => (
|
|
44
|
+
<FormControl error={!!error}>
|
|
48
45
|
<FormLabel required={typeof required === 'object' ? required.value : !!required}>{label}</FormLabel>
|
|
49
46
|
<RadioGroup {...field} {...rest} />
|
|
50
47
|
<FormHelperText>
|
|
51
|
-
{
|
|
48
|
+
{error?.message ? (
|
|
52
49
|
<>
|
|
53
|
-
{
|
|
50
|
+
{error.message}
|
|
54
51
|
<br />
|
|
55
52
|
{helperText}
|
|
56
53
|
</>
|
|
@@ -44,7 +44,7 @@ export const _ControlledSelect: StoryObj<typeof ControlledSelect> = {
|
|
|
44
44
|
disabled={!isSubmitSuccessful}
|
|
45
45
|
children="Reset"
|
|
46
46
|
color="secondary"
|
|
47
|
-
onClick={() => reset({
|
|
47
|
+
onClick={() => reset({ [args.name]: '' })}
|
|
48
48
|
/>
|
|
49
49
|
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
50
50
|
</Grid>
|
|
@@ -52,7 +52,7 @@ export const _ControlledSelect: StoryObj<typeof ControlledSelect> = {
|
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
return (
|
|
55
|
-
<ControlledForm values={{
|
|
55
|
+
<ControlledForm values={{ [args.name]: '' }} onSubmit={(data) => data} noValidate>
|
|
56
56
|
<FormControl>
|
|
57
57
|
<FormLabel id={`${args.id}-label`}>{args.label}</FormLabel>
|
|
58
58
|
<ControlledSelect {...args} labelId={`${args.id}-label`}>
|
|
@@ -72,3 +72,63 @@ export const _ControlledSelect: StoryObj<typeof ControlledSelect> = {
|
|
|
72
72
|
label: 'Select Label',
|
|
73
73
|
},
|
|
74
74
|
};
|
|
75
|
+
|
|
76
|
+
export const _ControlledMultiSelect: StoryObj<typeof ControlledSelect> = {
|
|
77
|
+
render: (args) => {
|
|
78
|
+
const SubmittedValues = () => {
|
|
79
|
+
const {
|
|
80
|
+
getValues,
|
|
81
|
+
formState: { isSubmitSuccessful },
|
|
82
|
+
} = useFormContext();
|
|
83
|
+
|
|
84
|
+
return isSubmitSuccessful ? (
|
|
85
|
+
<Paper sx={{ padding: '1.5rem', marginTop: '1.5rem' }}>
|
|
86
|
+
<Typography variant="h2">Submitted Values</Typography>
|
|
87
|
+
<pre>{JSON.stringify(getValues(), null, 2)}</pre>
|
|
88
|
+
</Paper>
|
|
89
|
+
) : null;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const Actions = () => {
|
|
93
|
+
const {
|
|
94
|
+
reset,
|
|
95
|
+
formState: { isSubmitSuccessful },
|
|
96
|
+
} = useFormContext();
|
|
97
|
+
return (
|
|
98
|
+
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
99
|
+
<Button
|
|
100
|
+
disabled={!isSubmitSuccessful}
|
|
101
|
+
children="Reset"
|
|
102
|
+
color="secondary"
|
|
103
|
+
onClick={() => reset({ [args.name]: [] })}
|
|
104
|
+
/>
|
|
105
|
+
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
106
|
+
</Grid>
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<ControlledForm values={{ [args.name]: [] }} onSubmit={(data) => data} noValidate>
|
|
112
|
+
<FormControl>
|
|
113
|
+
<FormLabel id={`${args.id}-label`}>{args.label}</FormLabel>
|
|
114
|
+
<ControlledSelect {...args} labelId={`${args.id}-label`}>
|
|
115
|
+
<MenuItem value={1}>Option 1</MenuItem>
|
|
116
|
+
<MenuItem value={2}>Option 2</MenuItem>
|
|
117
|
+
<MenuItem value={3}>Option 3</MenuItem>
|
|
118
|
+
<MenuItem value={4}>Option 4</MenuItem>
|
|
119
|
+
<MenuItem value={5}>Option 5</MenuItem>
|
|
120
|
+
<MenuItem value={6}>Option 6</MenuItem>
|
|
121
|
+
</ControlledSelect>
|
|
122
|
+
<Actions />
|
|
123
|
+
<SubmittedValues />
|
|
124
|
+
</FormControl>
|
|
125
|
+
</ControlledForm>
|
|
126
|
+
);
|
|
127
|
+
},
|
|
128
|
+
args: {
|
|
129
|
+
name: 'controlledMutliSelect',
|
|
130
|
+
required: 'This is required.',
|
|
131
|
+
label: 'Select Label',
|
|
132
|
+
multiple: true,
|
|
133
|
+
},
|
|
134
|
+
};
|
package/src/lib/Select.tsx
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Select, SelectProps } from '@availity/mui-form-utils';
|
|
2
|
-
import {
|
|
2
|
+
import { RegisterOptions, FieldValues, Controller, ControllerProps } from 'react-hook-form';
|
|
3
3
|
|
|
4
|
-
export type ControlledSelectProps = Omit<SelectProps, 'error' | 'required'> &
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
>;
|
|
4
|
+
export type ControlledSelectProps = Omit<SelectProps, 'error' | 'required'> &
|
|
5
|
+
Omit<RegisterOptions<FieldValues, string>, 'disabled' | 'valueAsNumber' | 'valueAsDate' | 'setValueAs'> &
|
|
6
|
+
Pick<ControllerProps, 'defaultValue' | 'shouldUnregister' | 'name'>;
|
|
8
7
|
|
|
9
8
|
export const ControlledSelect = ({
|
|
10
9
|
name,
|
|
@@ -15,23 +14,21 @@ export const ControlledSelect = ({
|
|
|
15
14
|
min,
|
|
16
15
|
pattern,
|
|
17
16
|
validate,
|
|
18
|
-
setValueAs,
|
|
19
17
|
disabled,
|
|
20
18
|
onChange,
|
|
21
19
|
onBlur,
|
|
22
20
|
value,
|
|
21
|
+
defaultValue,
|
|
23
22
|
shouldUnregister,
|
|
24
23
|
deps,
|
|
25
24
|
...rest
|
|
26
25
|
}: ControlledSelectProps) => {
|
|
27
|
-
const { register, getFieldState } = useFormContext();
|
|
28
|
-
|
|
29
26
|
return (
|
|
30
|
-
<
|
|
31
|
-
{
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
{
|
|
27
|
+
<Controller
|
|
28
|
+
name={name}
|
|
29
|
+
defaultValue={defaultValue}
|
|
30
|
+
disabled={disabled}
|
|
31
|
+
rules={{
|
|
35
32
|
required,
|
|
36
33
|
maxLength,
|
|
37
34
|
minLength,
|
|
@@ -39,14 +36,21 @@ export const ControlledSelect = ({
|
|
|
39
36
|
min,
|
|
40
37
|
pattern,
|
|
41
38
|
validate,
|
|
42
|
-
setValueAs,
|
|
43
|
-
disabled,
|
|
44
39
|
onChange,
|
|
45
40
|
onBlur,
|
|
46
41
|
value,
|
|
47
42
|
shouldUnregister,
|
|
48
43
|
deps,
|
|
49
|
-
}
|
|
44
|
+
}}
|
|
45
|
+
shouldUnregister={shouldUnregister}
|
|
46
|
+
render={({ field, fieldState: { error } }) => (
|
|
47
|
+
<Select
|
|
48
|
+
{...rest}
|
|
49
|
+
{...field}
|
|
50
|
+
error={!!error}
|
|
51
|
+
required={typeof required === 'object' ? required.value : !!required}
|
|
52
|
+
/>
|
|
53
|
+
)}
|
|
50
54
|
/>
|
|
51
55
|
);
|
|
52
56
|
};
|
|
@@ -43,13 +43,18 @@ export const _ControlledTextField: StoryObj<typeof ControlledTextField> = {
|
|
|
43
43
|
} = useFormContext();
|
|
44
44
|
return (
|
|
45
45
|
<Grid container direction="row" justifyContent="space-between" marginTop={1}>
|
|
46
|
-
<Button
|
|
46
|
+
<Button
|
|
47
|
+
disabled={!isSubmitSuccessful}
|
|
48
|
+
children="Reset"
|
|
49
|
+
color="secondary"
|
|
50
|
+
onClick={() => reset({ [args.name]: '' })}
|
|
51
|
+
/>
|
|
47
52
|
<Button type="submit" disabled={isSubmitSuccessful} children="Submit" />
|
|
48
53
|
</Grid>
|
|
49
54
|
);
|
|
50
55
|
};
|
|
51
56
|
return (
|
|
52
|
-
<ControlledForm values={{
|
|
57
|
+
<ControlledForm values={{ [args.name]: '' }} onSubmit={(data) => data}>
|
|
53
58
|
<ControlledTextField {...args} />
|
|
54
59
|
<Actions />
|
|
55
60
|
<SubmittedValues />
|