From 95bdde81fab1bc9a39c48d21e8ad961fd053750c Mon Sep 17 00:00:00 2001 From: Tomas Plevko Date: Fri, 31 Jan 2025 13:21:38 +0100 Subject: [PATCH] feat(Form): add outside of list values and placeholders to typeahead component --- .../Canvas/FormV2/fields/EnumField.tsx | 13 ++++++++++++- packages/ui/src/components/typeahead/Typeahead.tsx | 13 +++++++++++-- .../ui/src/components/typeahead/Typeahead.types.tsx | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/ui/src/components/Visualization/Canvas/FormV2/fields/EnumField.tsx b/packages/ui/src/components/Visualization/Canvas/FormV2/fields/EnumField.tsx index 78f9c5d2f..d3ac83f75 100644 --- a/packages/ui/src/components/Visualization/Canvas/FormV2/fields/EnumField.tsx +++ b/packages/ui/src/components/Visualization/Canvas/FormV2/fields/EnumField.tsx @@ -24,7 +24,17 @@ export const EnumField: FunctionComponent = ({ propName, required }) if (!value) { return undefined; } - return items.find((item) => item.name === value); + + // Object values are stringified. Double check later different approach. + if (typeof value === 'object') { + return { + value: JSON.stringify(value), + name: JSON.stringify(value), + description: '', + }; + } + + return items.find((item) => item.name === value) ?? { value: value, name: value, description: '' }; }, [items, value]); const onItemChange = useCallback( @@ -65,6 +75,7 @@ export const EnumField: FunctionComponent = ({ propName, required }) = ({ selectedItem, - items, + items: itemsProps, id, + placeholder = 'Select or write an option', onChange, onCleanInput, 'data-testid': dataTestId, @@ -26,6 +27,13 @@ export const Typeahead: FunctionComponent = ({ const [isOpen, setIsOpen] = useState(false); const inputRef = useRef(null); + const items = useMemo(() => { + if (itemsProps.find((item) => item.name === selectedItem?.name) || selectedItem === undefined) { + return itemsProps; + } + return [...itemsProps, { name: selectedItem?.name, value: selectedItem?.value }]; + }, [itemsProps, selectedItem]); + const onItemChanged = useCallback( (_event: unknown, name: string | number | undefined) => { if (!isDefined(name)) { @@ -65,7 +73,7 @@ export const Typeahead: FunctionComponent = ({ const filteredItems = useMemo( () => items.filter((item) => { - const hasNameMatch = item.name.includes(inputValue); + const hasNameMatch = item.name?.includes(inputValue); const hasDescriptionMatch = item.description?.includes(inputValue); return !inputValue || hasNameMatch || hasDescriptionMatch; @@ -90,6 +98,7 @@ export const Typeahead: FunctionComponent = ({ id={`typeahead-select-input-${id}`} data-testid={`typeahead-select-input-${dataTestId}`} ref={inputRef} + placeholder={placeholder} onClick={onToggleClick} value={inputValue} onChange={onTextInputChange} diff --git a/packages/ui/src/components/typeahead/Typeahead.types.tsx b/packages/ui/src/components/typeahead/Typeahead.types.tsx index fb909c361..f09f84d9a 100644 --- a/packages/ui/src/components/typeahead/Typeahead.types.tsx +++ b/packages/ui/src/components/typeahead/Typeahead.types.tsx @@ -11,6 +11,7 @@ export interface TypeaheadProps extends IDataTestID { selectedItem?: TypeaheadItem; items: TypeaheadItem[]; id?: string; + placeholder?: string; onChange?: (item?: TypeaheadItem) => void; onCleanInput?: () => void; }