import { FocusEvent } from 'react'; import { Dropdown } from 'primereact/dropdown'; import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps, } from '@snups/rjsf-utils'; import { MultiSelect } from 'primereact/multiselect'; /** 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 */ export default function SelectWidget( props: WidgetProps, ) { const { multiple = false } = props; return multiple ? : ; } function SingleSelectWidget({ schema, id, name, // remove this from dropdownProps options, label, hideLabel, required, disabled, placeholder, readonly, value, multiple, autofocus, onChange, onBlur, onFocus, errorSchema, rawErrors = [], registry, uiSchema, hideError, formContext, ...dropdownProps }: WidgetProps) { const { enumOptions, enumDisabled, emptyValue: optEmptyVal } = options; const primeProps = (options.prime || {}) as object; multiple = typeof multiple === 'undefined' ? false : multiple; const emptyValue = multiple ? [] : ''; const isEmpty = typeof value === 'undefined' || (multiple && value.length < 1) || (!multiple && value === emptyValue); const _onChange = (e: { value: any }) => onChange(enumOptionsValueForIndex(e.value, enumOptions, optEmptyVal)); const _onBlur = ({ target }: FocusEvent) => onBlur(id, enumOptionsValueForIndex(target && target.value, enumOptions, optEmptyVal)); const _onFocus = ({ target }: FocusEvent) => onFocus(id, enumOptionsValueForIndex(target && target.value, enumOptions, optEmptyVal)); const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, multiple); const { ...dropdownRemainingProps } = dropdownProps; return ( ({ label, value: String(i), disabled: Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1, }))} onChange={_onChange} onBlur={_onBlur} onFocus={_onFocus} placeholder={placeholder} disabled={disabled || readonly} autoFocus={autofocus} aria-describedby={ariaDescribedByIds(id)} {...dropdownRemainingProps} /> ); } function MultiSelectWidget({ id, options, disabled, placeholder, readonly, value, multiple = false, autofocus, onChange, onBlur, onFocus, }: WidgetProps) { const { enumOptions, enumDisabled, emptyValue: optEmptyVal } = options; const primeProps = (options.prime || {}) as object; const emptyValue = multiple ? [] : ''; const isEmpty = typeof value === 'undefined' || (multiple && value.length < 1) || (!multiple && value === emptyValue); const _onChange = (e: { value: any }) => onChange(enumOptionsValueForIndex(e.value, enumOptions, optEmptyVal)); const _onBlur = ({ target }: FocusEvent) => onBlur(id, enumOptionsValueForIndex(target && target.value, enumOptions, optEmptyVal)); const _onFocus = ({ target }: FocusEvent) => onFocus(id, enumOptionsValueForIndex(target && target.value, enumOptions, optEmptyVal)); const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, multiple); return ( ({ label, value: String(i), disabled: Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1, }))} onChange={_onChange} onBlur={_onBlur} onFocus={_onFocus} placeholder={placeholder} disabled={disabled || readonly} autoFocus={autofocus} display={options.display === 'chip' ? 'chip' : 'comma'} aria-describedby={ariaDescribedByIds(id)} pt={{ root: { style: { position: 'relative' } } }} /> ); }