From 3243729f5225d65e2eab182d358666ca3a3532fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Ara=C3=BAjo?= Date: Sun, 7 Jul 2024 19:00:17 +0100 Subject: [PATCH] fix(controllers): fix `disabled` prop disabling controller and not only the field add a new prop `controllerDisabled` --- src/components/DatePickerController.tsx | 8 +- src/components/PhoneInputController.tsx | 9 +- src/components/SelectController.tsx | 172 ++++++++++++------------ src/components/TextFieldController.tsx | 6 +- src/types/fields.ts | 2 +- 5 files changed, 99 insertions(+), 98 deletions(-) diff --git a/src/components/DatePickerController.tsx b/src/components/DatePickerController.tsx index dcdc656..6977ad0 100644 --- a/src/components/DatePickerController.tsx +++ b/src/components/DatePickerController.tsx @@ -13,7 +13,7 @@ export type DatePickerControllerProps< > = FieldControllerProps & Omit< DatePickerProps, - 'label' | 'name' | 'disabled' | 'onChange' | 'value' | 'onBlur' + 'label' | 'name' | 'onChange' | 'value' | 'onBlur' >; export const DatePickerController = < @@ -22,12 +22,12 @@ export const DatePickerController = < PickerEnableAccessibleFieldDOMStructure extends boolean = false, >({ control, + controllerDisabled = false, label, name, optional = false, requiredLabel, onErrorMessage, - disabled = false, ...datePickerProps }: DatePickerControllerProps) => { const { fieldControllerLabel } = useFieldControllerLabels({ label, optional, requiredLabel }); @@ -38,13 +38,12 @@ export const DatePickerController = < fieldState: { invalid, error }, } = useController({ control, - disabled, + disabled: controllerDisabled, name, }); return ( ); }; diff --git a/src/components/PhoneInputController.tsx b/src/components/PhoneInputController.tsx index c73c021..ce2a001 100644 --- a/src/components/PhoneInputController.tsx +++ b/src/components/PhoneInputController.tsx @@ -10,19 +10,16 @@ import type { MuiTelInputProps } from 'mui-tel-input'; export const matchIsValidTel = _matchIsValidTel; export type PhoneInputControllerProps = FieldControllerProps & - DistributiveOmit< - MuiTelInputProps, - 'onChange' | 'error' | 'helperText' | 'label' | 'name' | 'disabled' | 'value' | 'onBlur' - >; + DistributiveOmit; export const PhoneInputController = ({ control, + controllerDisabled = false, label, name, optional = false, requiredLabel, onErrorMessage, - disabled = false, ...muiTelInputProps }: PhoneInputControllerProps) => { const { fieldControllerLabel } = useFieldControllerLabels({ label, optional, requiredLabel }); @@ -33,7 +30,7 @@ export const PhoneInputController = ({ fieldState: { invalid, error }, } = useController({ control, - disabled, + disabled: controllerDisabled, name, }); diff --git a/src/components/SelectController.tsx b/src/components/SelectController.tsx index 12c063a..eb68854 100644 --- a/src/components/SelectController.tsx +++ b/src/components/SelectController.tsx @@ -9,12 +9,12 @@ import { Stack, Typography, } from '@mui/material'; -import { Controller } from 'react-hook-form'; import { useAsyncFieldControllerLabels, useFieldControllerLabels, useFieldControllerWithOptionsLabels, } from '../hooks/index'; +import { useController } from 'react-hook-form'; import { useMemo } from 'react'; import type { AsyncFieldControllerProps, @@ -69,6 +69,7 @@ export interface SelectControllerProps({ control, + controllerDisabled = false, label, name, optional = false, @@ -98,94 +99,97 @@ export const SelectController = !loading && !loadingError, [loadingError, loading]); + const { + field, + fieldState: { invalid, error }, + } = useController({ + control, + disabled: controllerDisabled, + name, + }); + return ( - ( - - {fieldControllerLabel} - { + const found = options?.find((option) => optionValueAccessor(option) === selected); - if (found) { - if (optionExtraLabelAccessor?.(found) && displayExtraLabelWhenValueSelected) { - return `${optionLabelAccessor(found)}, ${optionExtraLabelAccessor(found)}`; - } + if (found) { + if (optionExtraLabelAccessor?.(found) && displayExtraLabelWhenValueSelected) { + return `${optionLabelAccessor(found)}, ${optionExtraLabelAccessor(found)}`; + } - return optionLabelAccessor(found); - } + return optionLabelAccessor(found); + } - return ''; - }} - {...selectProps} + return ''; + }} + {...selectProps} + > + {loading && ( + + + {fieldControllerLoadingLabel} + + + + )} + {loadingError && ( + + {fieldControllerLoadingErrorLabel} + + )} + {shouldDisplayOptions && options?.length === 0 && ( + - {loading && ( - - - {fieldControllerLoadingLabel} - - - - )} - {loadingError && ( - - {fieldControllerLoadingErrorLabel} - - )} - {shouldDisplayOptions && options?.length === 0 && ( - - {fieldControllerNoOptionsLabel} - - )} - {shouldDisplayOptions && - options?.map((option) => ( - - - - ))} - - {invalid && ( - - {onErrorMessage && error?.message ? onErrorMessage(error.message) : error?.message} - - )} - + {fieldControllerNoOptionsLabel} + + )} + {shouldDisplayOptions && + options?.map((option) => ( + + + + ))} + + {invalid && ( + + {onErrorMessage && error?.message ? onErrorMessage(error.message) : error?.message} + )} - /> + ); }; diff --git a/src/components/TextFieldController.tsx b/src/components/TextFieldController.tsx index 4137f2b..d4e1d2f 100644 --- a/src/components/TextFieldController.tsx +++ b/src/components/TextFieldController.tsx @@ -12,12 +12,12 @@ export type TextFieldControllerProps = FieldControllerPr export const TextFieldController = ({ control, + controllerDisabled = false, label, name, optional = false, requiredLabel, onErrorMessage, - disabled = false, maxLength, ...textFieldProps }: TextFieldControllerProps) => { @@ -29,13 +29,12 @@ export const TextFieldController = ({ fieldState: { invalid, error }, } = useController({ control, - disabled, + disabled: controllerDisabled, name, }); return ( ({ onChange(e); } }} + {...textFieldProps} /> ); }; diff --git a/src/types/fields.ts b/src/types/fields.ts index 7d16a99..99dd031 100644 --- a/src/types/fields.ts +++ b/src/types/fields.ts @@ -6,7 +6,7 @@ export interface FieldControllerProps { name: FieldPath; label: string; optional?: boolean; - disabled?: boolean; + controllerDisabled?: boolean; requiredLabel?: string; onErrorMessage?: (error: string) => string; }