import { ChangeEvent, FocusEvent } from 'react'; import FormSelect from 'react-bootstrap/FormSelect'; import { ariaDescribedByIds, FormContextType, enumOptionsIndexForValue, enumOptionsValueForIndex, RJSFSchema, StrictRJSFSchema, WidgetProps, } from '@rjsf/utils'; export default function SelectWidget< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any >({ schema, id, options, required, disabled, readonly, value, multiple, autofocus, onChange, onBlur, onFocus, placeholder, rawErrors = [], }: WidgetProps) { const { enumOptions, enumDisabled, emptyValue: optEmptyValue } = options; const emptyValue = multiple ? [] : ''; function getValue(event: FocusEvent | ChangeEvent | any, multiple?: boolean) { if (multiple) { return [].slice .call(event.target.options as any) .filter((o: any) => o.selected) .map((o: any) => o.value); } else { return event.target.value; } } const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, multiple); return ( 0 ? 'is-invalid' : ''} onBlur={ onBlur && ((event: FocusEvent) => { const newValue = getValue(event, multiple); onBlur(id, enumOptionsValueForIndex(newValue, enumOptions, optEmptyValue)); }) } onFocus={ onFocus && ((event: FocusEvent) => { const newValue = getValue(event, multiple); onFocus(id, enumOptionsValueForIndex(newValue, enumOptions, optEmptyValue)); }) } onChange={(event: ChangeEvent) => { const newValue = getValue(event, multiple); onChange(enumOptionsValueForIndex(newValue, enumOptions, optEmptyValue)); }} aria-describedby={ariaDescribedByIds(id)} > {!multiple && schema.default === undefined && } {(enumOptions as any).map(({ value, label }: any, i: number) => { const disabled: any = Array.isArray(enumDisabled) && (enumDisabled as any).indexOf(value) != -1; return ( ); })} ); }