Debian Codegraph @

packages/mui/src/SelectWidget/SelectWidget.tsx

raw
1 import { ChangeEvent, FocusEvent } from 'react';
2 import MenuItem from '@mui/material/MenuItem';
3 import TextField, { TextFieldProps } from '@mui/material/TextField';
4 import {
5 ariaDescribedByIds,
6 enumOptionsIndexForValue,
7 enumOptionsValueForIndex,
8 labelValue,
9 FormContextType,
10 RJSFSchema,
11 StrictRJSFSchema,
12 WidgetProps,
13 } from '@rjsf/utils';
14
15 /** The `SelectWidget` is a widget for rendering dropdowns.
16 * It is typically used with string properties constrained with enum options.
17 *
18 * @param props - The `WidgetProps` for this component
19 */
20 export default function SelectWidget<
21 T = any,
22 S extends StrictRJSFSchema = RJSFSchema,
23 F extends FormContextType = any
24 >({
25 schema,
26 id,
27 name, // remove this from textFieldProps
28 options,
29 label,
30 hideLabel,
31 required,
32 disabled,
33 placeholder,
34 readonly,
35 value,
36 multiple,
37 autofocus,
38 onChange,
39 onBlur,
40 onFocus,
41 rawErrors = [],
42 registry,
43 uiSchema,
44 hideError,
45 formContext,
46 ...textFieldProps
47 }: WidgetProps<T, S, F>) {
48 const { enumOptions, enumDisabled, emptyValue: optEmptyVal } = options;
49
50 multiple = typeof multiple === 'undefined' ? false : !!multiple;
51
52 const emptyValue = multiple ? [] : '';
53 const isEmpty = typeof value === 'undefined' || (multiple && value.length < 1) || (!multiple && value === emptyValue);
54
55 const _onChange = ({ target: { value } }: ChangeEvent<{ value: string }>) =>
56 onChange(enumOptionsValueForIndex<S>(value, enumOptions, optEmptyVal));
57 const _onBlur = ({ target: { value } }: FocusEvent<HTMLInputElement>) =>
58 onBlur(id, enumOptionsValueForIndex<S>(value, enumOptions, optEmptyVal));
59 const _onFocus = ({ target: { value } }: FocusEvent<HTMLInputElement>) =>
60 onFocus(id, enumOptionsValueForIndex<S>(value, enumOptions, optEmptyVal));
61 const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, multiple);
62
63 return (
64 <TextField
65 id={id}
66 name={id}
67 label={labelValue(label || undefined, hideLabel, undefined)}
68 value={!isEmpty && typeof selectedIndexes !== 'undefined' ? selectedIndexes : emptyValue}
69 required={required}
70 disabled={disabled || readonly}
71 autoFocus={autofocus}
72 placeholder={placeholder}
73 error={rawErrors.length > 0}
74 onChange={_onChange}
75 onBlur={_onBlur}
76 onFocus={_onFocus}
77 {...(textFieldProps as TextFieldProps)}
78 select // Apply this and the following props after the potential overrides defined in textFieldProps
79 InputLabelProps={{
80 ...textFieldProps.InputLabelProps,
81 shrink: !isEmpty,
82 }}
83 SelectProps={{
84 ...textFieldProps.SelectProps,
85 multiple,
86 }}
87 aria-describedby={ariaDescribedByIds<T>(id)}
88 >
89 {Array.isArray(enumOptions) &&
90 enumOptions.map(({ value, label }, i: number) => {
91 const disabled: boolean = Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1;
92 return (
93 <MenuItem key={i} value={String(i)} disabled={disabled}>
94 {label}
95 </MenuItem>
96 );
97 })}
98 </TextField>
99 );
100 }