1
1
import * as React from 'react' ;
2
2
import { useCallback } from 'react' ;
3
3
import PropTypes from 'prop-types' ;
4
- import get from 'lodash/get' ;
5
4
import MenuItem from '@material-ui/core/MenuItem' ;
6
5
import { TextFieldProps } from '@material-ui/core/TextField' ;
7
6
import { makeStyles } from '@material-ui/core/styles' ;
@@ -144,9 +143,10 @@ export const SelectInput = (props: SelectInputProps) => {
144
143
`If you're not wrapping the SelectInput inside a ReferenceInput, you must provide the choices prop`
145
144
) ;
146
145
147
- const { getChoiceText, getChoiceValue } = useChoices ( {
146
+ const { getChoiceText, getChoiceValue, getDisableValue } = useChoices ( {
148
147
optionText,
149
148
optionValue,
149
+ disableValue,
150
150
translateChoice,
151
151
} ) ;
152
152
@@ -201,6 +201,30 @@ export const SelectInput = (props: SelectInputProps) => {
201
201
onCreate,
202
202
optionText,
203
203
} ) ;
204
+
205
+ const createItem = create || onCreate ? getCreateItem ( ) : null ;
206
+ const finalChoices =
207
+ create || onCreate ? [ ...choices , createItem ] : choices ;
208
+
209
+ const renderMenuItem = useCallback (
210
+ choice => {
211
+ return choice ? (
212
+ < MenuItem
213
+ key = { getChoiceValue ( choice ) }
214
+ value = { getChoiceValue ( choice ) }
215
+ disabled = { getDisableValue ( choice ) }
216
+ >
217
+ { renderMenuItemOption (
218
+ ! ! createItem && choice ?. id === createItem . id
219
+ ? createItem
220
+ : choice
221
+ ) }
222
+ </ MenuItem >
223
+ ) : null ;
224
+ } ,
225
+ [ getChoiceValue , getDisableValue , renderMenuItemOption , createItem ]
226
+ ) ;
227
+
204
228
if ( loading ) {
205
229
return (
206
230
< Labeled
@@ -219,18 +243,6 @@ export const SelectInput = (props: SelectInputProps) => {
219
243
) ;
220
244
}
221
245
222
- const renderCreateItem = ( ) => {
223
- if ( onCreate || create ) {
224
- const createItem = getCreateItem ( ) ;
225
- return (
226
- < MenuItem value = { createItem . id } key = { createItem . id } >
227
- { createItem . name }
228
- </ MenuItem >
229
- ) ;
230
- }
231
- return null ;
232
- } ;
233
-
234
246
return (
235
247
< >
236
248
< ResettableTextField
@@ -273,16 +285,7 @@ export const SelectInput = (props: SelectInputProps) => {
273
285
{ renderEmptyItemOption ( ) }
274
286
</ MenuItem >
275
287
) : null }
276
- { choices . map ( choice => (
277
- < MenuItem
278
- key = { getChoiceValue ( choice ) }
279
- value = { getChoiceValue ( choice ) }
280
- disabled = { get ( choice , disableValue ) }
281
- >
282
- { renderMenuItemOption ( choice ) }
283
- </ MenuItem >
284
- ) ) }
285
- { renderCreateItem ( ) }
288
+ { finalChoices . map ( renderMenuItem ) }
286
289
</ ResettableTextField >
287
290
{ createElement }
288
291
</ >
0 commit comments