@@ -9,12 +9,12 @@ import {
99 Stack ,
1010 Typography ,
1111} from '@mui/material' ;
12- import { Controller } from 'react-hook-form' ;
1312import {
1413 useAsyncFieldControllerLabels ,
1514 useFieldControllerLabels ,
1615 useFieldControllerWithOptionsLabels ,
1716} from '../hooks/index' ;
17+ import { useController } from 'react-hook-form' ;
1818import { useMemo } from 'react' ;
1919import type {
2020 AsyncFieldControllerProps ,
@@ -69,6 +69,7 @@ export interface SelectControllerProps<FV extends FieldValues, Value extends Obj
6969
7070export 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} ;
0 commit comments