import type { OptionOnSelectData } from '@fluentui/react-combobox'; import { Dropdown, Field, Option } from '@fluentui/react-components'; import type { FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps } from '@rjsf/utils'; import { ariaDescribedByIds, enumOptionValueDecoder, enumOptionValueEncoder, enumOptionsIndexForValue, getOptionValueFormat, labelValue, logUnsupportedDefaultForEnum, SelectedOptionDescription, } from '@rjsf/utils'; function getValue(data: OptionOnSelectData, multiple: boolean) { if (multiple) { return data.selectedOptions; } return data.selectedOptions[0]; } /** The `SelectWidget` is a widget for rendering dropdowns. * It is typically used with string properties constrained with enum options. * * @param props - The `WidgetProps` for this component */ function SelectWidget({ id, htmlName, options, label, hideLabel, value, required, disabled, readonly, multiple = false, autofocus = false, rawErrors = [], onChange, onBlur, onFocus, schema, placeholder, registry, uiSchema, }: WidgetProps) { const { enumOptions, enumDisabled, emptyValue: optEmptyVal } = options; const optionValueFormat = getOptionValueFormat(options); const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, multiple); let selectedIndexesAsArray: string[] = []; if (typeof selectedIndexes === 'string') { selectedIndexesAsArray = [selectedIndexes]; } else if (Array.isArray(selectedIndexes)) { selectedIndexesAsArray = selectedIndexes.map((index) => String(index)); } const dropdownValue = selectedIndexesAsArray .map((index) => (enumOptions ? enumOptions[Number(index)].label : undefined)) .join(', '); const handleBlur = () => onBlur(id, selectedIndexes); const handleFocus = () => onFocus(id, selectedIndexes); const handleChange = (_: any, data: OptionOnSelectData) => { const newValue = getValue(data, multiple); return onChange(enumOptionValueDecoder(newValue, enumOptions, optionValueFormat, optEmptyVal)); }; const showPlaceholderOption = !multiple && schema.default === undefined; logUnsupportedDefaultForEnum(id, schema, enumOptions, multiple); return ( {showPlaceholderOption && } {Array.isArray(enumOptions) && enumOptions.map(({ value: enumValue, label: enumLabel }, i) => { const isDisabled = enumDisabled && enumDisabled.includes(enumValue); return ( ); })} ); } export default SelectWidget;