Skip to content

Commit 3243729

Browse files
committed
fix(controllers): fix disabled prop disabling controller and not only the field
add a new prop `controllerDisabled`
1 parent ccd0d7c commit 3243729

File tree

5 files changed

+99
-98
lines changed

5 files changed

+99
-98
lines changed

src/components/DatePickerController.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type DatePickerControllerProps<
1313
> = FieldControllerProps<FV> &
1414
Omit<
1515
DatePickerProps<PVD, PickerEnableAccessibleFieldDOMStructure>,
16-
'label' | 'name' | 'disabled' | 'onChange' | 'value' | 'onBlur'
16+
'label' | 'name' | 'onChange' | 'value' | 'onBlur'
1717
>;
1818

1919
export const DatePickerController = <
@@ -22,12 +22,12 @@ export const DatePickerController = <
2222
PickerEnableAccessibleFieldDOMStructure extends boolean = false,
2323
>({
2424
control,
25+
controllerDisabled = false,
2526
label,
2627
name,
2728
optional = false,
2829
requiredLabel,
2930
onErrorMessage,
30-
disabled = false,
3131
...datePickerProps
3232
}: DatePickerControllerProps<FV, PVD, PickerEnableAccessibleFieldDOMStructure>) => {
3333
const { fieldControllerLabel } = useFieldControllerLabels({ label, optional, requiredLabel });
@@ -38,13 +38,12 @@ export const DatePickerController = <
3838
fieldState: { invalid, error },
3939
} = useController({
4040
control,
41-
disabled,
41+
disabled: controllerDisabled,
4242
name,
4343
});
4444

4545
return (
4646
<DatePicker
47-
{...datePickerProps}
4847
{...restField}
4948
aria-required={optional ? 'false' : 'true'}
5049
label={fieldControllerLabel}
@@ -57,6 +56,7 @@ export const DatePickerController = <
5756
},
5857
}}
5958
value={stringToDate(value)}
59+
{...datePickerProps}
6060
/>
6161
);
6262
};

src/components/PhoneInputController.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ import type { MuiTelInputProps } from 'mui-tel-input';
1010
export const matchIsValidTel = _matchIsValidTel;
1111

1212
export type PhoneInputControllerProps<FV extends FieldValues> = FieldControllerProps<FV> &
13-
DistributiveOmit<
14-
MuiTelInputProps,
15-
'onChange' | 'error' | 'helperText' | 'label' | 'name' | 'disabled' | 'value' | 'onBlur'
16-
>;
13+
DistributiveOmit<MuiTelInputProps, 'onChange' | 'error' | 'helperText' | 'label' | 'name' | 'value' | 'onBlur'>;
1714

1815
export const PhoneInputController = <FV extends FieldValues>({
1916
control,
17+
controllerDisabled = false,
2018
label,
2119
name,
2220
optional = false,
2321
requiredLabel,
2422
onErrorMessage,
25-
disabled = false,
2623
...muiTelInputProps
2724
}: PhoneInputControllerProps<FV>) => {
2825
const { fieldControllerLabel } = useFieldControllerLabels({ label, optional, requiredLabel });
@@ -33,7 +30,7 @@ export const PhoneInputController = <FV extends FieldValues>({
3330
fieldState: { invalid, error },
3431
} = useController({
3532
control,
36-
disabled,
33+
disabled: controllerDisabled,
3734
name,
3835
});
3936

src/components/SelectController.tsx

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import {
99
Stack,
1010
Typography,
1111
} from '@mui/material';
12-
import { Controller } from 'react-hook-form';
1312
import {
1413
useAsyncFieldControllerLabels,
1514
useFieldControllerLabels,
1615
useFieldControllerWithOptionsLabels,
1716
} from '../hooks/index';
17+
import { useController } from 'react-hook-form';
1818
import { useMemo } from 'react';
1919
import type {
2020
AsyncFieldControllerProps,
@@ -69,6 +69,7 @@ export interface SelectControllerProps<FV extends FieldValues, Value extends Obj
6969

7070
export const SelectController = <FV extends FieldValues, Value extends ObjectLike>({
7171
control,
72+
controllerDisabled = false,
7273
label,
7374
name,
7475
optional = false,
@@ -98,94 +99,97 @@ export const SelectController = <FV extends FieldValues, Value extends ObjectLik
9899

99100
const shouldDisplayOptions = useMemo(() => !loading && !loadingError, [loadingError, loading]);
100101

102+
const {
103+
field,
104+
fieldState: { invalid, error },
105+
} = useController({
106+
control,
107+
disabled: controllerDisabled,
108+
name,
109+
});
110+
101111
return (
102-
<Controller
103-
control={control}
104-
name={name}
105-
render={({ field, fieldState: { invalid, error } }) => (
106-
<FormControl
107-
{...muiProps?.formControlProps}
108-
error={invalid}
109-
fullWidth
110-
>
111-
<InputLabel {...muiProps?.inputLabelProps}>{fieldControllerLabel}</InputLabel>
112-
<Select
113-
{...field}
114-
aria-required={optional ? 'false' : 'true'}
115-
label={fieldControllerLabel}
116-
renderValue={(selected) => {
117-
const found = options?.find((option) => optionValueAccessor(option) === selected);
112+
<FormControl
113+
{...muiProps?.formControlProps}
114+
error={invalid}
115+
fullWidth
116+
>
117+
<InputLabel {...muiProps?.inputLabelProps}>{fieldControllerLabel}</InputLabel>
118+
<Select
119+
{...field}
120+
aria-required={optional ? 'false' : 'true'}
121+
label={fieldControllerLabel}
122+
renderValue={(selected) => {
123+
const found = options?.find((option) => optionValueAccessor(option) === selected);
118124

119-
if (found) {
120-
if (optionExtraLabelAccessor?.(found) && displayExtraLabelWhenValueSelected) {
121-
return `${optionLabelAccessor(found)}, ${optionExtraLabelAccessor(found)}`;
122-
}
125+
if (found) {
126+
if (optionExtraLabelAccessor?.(found) && displayExtraLabelWhenValueSelected) {
127+
return `${optionLabelAccessor(found)}, ${optionExtraLabelAccessor(found)}`;
128+
}
123129

124-
return optionLabelAccessor(found);
125-
}
130+
return optionLabelAccessor(found);
131+
}
126132

127-
return '';
128-
}}
129-
{...selectProps}
133+
return '';
134+
}}
135+
{...selectProps}
136+
>
137+
{loading && (
138+
<MenuItem
139+
{...muiProps?.loadingMenuItemProps}
140+
disabled
141+
>
142+
<Stack
143+
alignItems='center'
144+
flexDirection='row'
145+
justifyContent='space-between'
146+
width='100%'
147+
{...muiProps?.loadingStackProps}
148+
>
149+
<Typography {...muiProps?.loadingTypographyProps}>{fieldControllerLoadingLabel}</Typography>
150+
<CircularProgress
151+
size={30}
152+
{...muiProps?.loadingCircularProgressProps}
153+
/>
154+
</Stack>
155+
</MenuItem>
156+
)}
157+
{loadingError && (
158+
<MenuItem
159+
{...muiProps?.errorMenuItemProps}
160+
disabled
161+
>
162+
<Typography {...muiProps?.errorTypographyProps}>{fieldControllerLoadingErrorLabel}</Typography>
163+
</MenuItem>
164+
)}
165+
{shouldDisplayOptions && options?.length === 0 && (
166+
<MenuItem
167+
disabled
168+
{...muiProps?.noOptionsMenuItemProps}
130169
>
131-
{loading && (
132-
<MenuItem
133-
{...muiProps?.loadingMenuItemProps}
134-
disabled
135-
>
136-
<Stack
137-
alignItems='center'
138-
flexDirection='row'
139-
justifyContent='space-between'
140-
width='100%'
141-
{...muiProps?.loadingStackProps}
142-
>
143-
<Typography {...muiProps?.loadingTypographyProps}>{fieldControllerLoadingLabel}</Typography>
144-
<CircularProgress
145-
size={30}
146-
{...muiProps?.loadingCircularProgressProps}
147-
/>
148-
</Stack>
149-
</MenuItem>
150-
)}
151-
{loadingError && (
152-
<MenuItem
153-
{...muiProps?.errorMenuItemProps}
154-
disabled
155-
>
156-
<Typography {...muiProps?.errorTypographyProps}>{fieldControllerLoadingErrorLabel}</Typography>
157-
</MenuItem>
158-
)}
159-
{shouldDisplayOptions && options?.length === 0 && (
160-
<MenuItem
161-
disabled
162-
{...muiProps?.noOptionsMenuItemProps}
163-
>
164-
<Typography {...muiProps?.noOptionsTypographyProps}>{fieldControllerNoOptionsLabel}</Typography>
165-
</MenuItem>
166-
)}
167-
{shouldDisplayOptions &&
168-
options?.map((option) => (
169-
<MenuItem
170-
{...muiProps?.menuItemProps}
171-
key={optionValueAccessor(option)}
172-
value={optionValueAccessor(option)}
173-
>
174-
<ListItemText
175-
{...muiProps?.listItemTextProps}
176-
primary={optionLabelAccessor(option)}
177-
secondary={optionExtraLabelAccessor?.(option)}
178-
/>
179-
</MenuItem>
180-
))}
181-
</Select>
182-
{invalid && (
183-
<FormHelperText {...muiProps?.formHelperTextProps}>
184-
{onErrorMessage && error?.message ? onErrorMessage(error.message) : error?.message}
185-
</FormHelperText>
186-
)}
187-
</FormControl>
170+
<Typography {...muiProps?.noOptionsTypographyProps}>{fieldControllerNoOptionsLabel}</Typography>
171+
</MenuItem>
172+
)}
173+
{shouldDisplayOptions &&
174+
options?.map((option) => (
175+
<MenuItem
176+
{...muiProps?.menuItemProps}
177+
key={optionValueAccessor(option)}
178+
value={optionValueAccessor(option)}
179+
>
180+
<ListItemText
181+
{...muiProps?.listItemTextProps}
182+
primary={optionLabelAccessor(option)}
183+
secondary={optionExtraLabelAccessor?.(option)}
184+
/>
185+
</MenuItem>
186+
))}
187+
</Select>
188+
{invalid && (
189+
<FormHelperText {...muiProps?.formHelperTextProps}>
190+
{onErrorMessage && error?.message ? onErrorMessage(error.message) : error?.message}
191+
</FormHelperText>
188192
)}
189-
/>
193+
</FormControl>
190194
);
191195
};

src/components/TextFieldController.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ export type TextFieldControllerProps<FV extends FieldValues> = FieldControllerPr
1212

1313
export const TextFieldController = <FV extends FieldValues>({
1414
control,
15+
controllerDisabled = false,
1516
label,
1617
name,
1718
optional = false,
1819
requiredLabel,
1920
onErrorMessage,
20-
disabled = false,
2121
maxLength,
2222
...textFieldProps
2323
}: TextFieldControllerProps<FV>) => {
@@ -29,13 +29,12 @@ export const TextFieldController = <FV extends FieldValues>({
2929
fieldState: { invalid, error },
3030
} = useController({
3131
control,
32-
disabled,
32+
disabled: controllerDisabled,
3333
name,
3434
});
3535

3636
return (
3737
<TextField
38-
{...textFieldProps}
3938
{...restField}
4039
aria-required={optional ? 'false' : 'true'}
4140
error={invalid}
@@ -48,6 +47,7 @@ export const TextFieldController = <FV extends FieldValues>({
4847
onChange(e);
4948
}
5049
}}
50+
{...textFieldProps}
5151
/>
5252
);
5353
};

src/types/fields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface FieldControllerProps<FV extends FieldValues> {
66
name: FieldPath<FV>;
77
label: string;
88
optional?: boolean;
9-
disabled?: boolean;
9+
controllerDisabled?: boolean;
1010
requiredLabel?: string;
1111
onErrorMessage?: (error: string) => string;
1212
}

0 commit comments

Comments
 (0)