import React, { useMemo, useState, useCallback } from 'react'; import Select from 'react-select'; import { TypeAheadEnumParam } from 'wdk-client/Utils/WdkModel'; import { Props } from 'wdk-client/Views/Question/Params/Utils'; import { ValueType, InputActionMeta } from 'react-select/src/types'; import { isMultiPick } from 'wdk-client/Views/Question/Params/EnumParamUtils'; import { safeHtml } from 'wdk-client/Utils/ComponentUtils'; type TypeAheadParamProps = { parameter: TypeAheadEnumParam; selectedValues: string[]; onChange: (newValue: string[]) => void; } type Option = { value: string, label: string }; export const TypeAheadEnumParamComponent = (props: TypeAheadParamProps) => { const vocabularyByValue = useMemo( () => props.parameter.vocabulary.reduce( (memo, entry) => { memo[entry[0]] = entry; return memo; }, {} as Record ), [ props.parameter.vocabulary ] ); const options = useMemo( () => props.parameter.vocabulary.map(([ value, label ]) => ({ value, label })), [ props.parameter.vocabulary ] ); const [ searchTerm, setSearchTerm ] = useState(''); const selection = useMemo( () => { return props.selectedValues.map( value => ({ value, label: vocabularyByValue[value][1] }) ); }, [ props.selectedValues, isMultiPick(props.parameter) ] ); const onInputChange = useCallback((inputValue: string, { action }: InputActionMeta) => { if (action === 'input-change') { setSearchTerm(inputValue); } }, []); const onChange = useCallback((newSelection: ValueType