Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/components/Input/dropdown-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface TDropdownProps extends InputProps {
onSelectOption: (value: string) => void;
isAutocomplete?: boolean;
options: TOptionList[];
defaultOption?: TOptionList;
listHeight?: string;
}

Expand All @@ -25,6 +26,7 @@ export const InputDropdown = forwardRef<HTMLInputElement, TDropdownProps>(
disabled,
label,
options,
defaultOption,
textAlignment = "left",
inputSize = "md",
status = "neutral",
Expand All @@ -41,7 +43,7 @@ export const InputDropdown = forwardRef<HTMLInputElement, TDropdownProps>(
const [items, setItems] = useState<TOptionList[]>(options);
const [shouldFilterList, setShouldFilterList] = useState(false);
const [selectedItem, setSelectedItem] = useState<TOptionList | null>(
null,
defaultOption ?? null,
);
const [isAnimating, setIsAnimating] = useState(false);

Expand All @@ -60,8 +62,7 @@ export const InputDropdown = forwardRef<HTMLInputElement, TDropdownProps>(
openMenu,
highlightedIndex,
} = useCombobox({
defaultSelectedItem:
options.find((item) => item.value === value) ?? null,
defaultSelectedItem: defaultOption ?? null,
items,
itemToString(item) {
return item ? reactNodeToString(item.text) : "";
Expand Down Expand Up @@ -112,8 +113,10 @@ export const InputDropdown = forwardRef<HTMLInputElement, TDropdownProps>(
(option) => option.value === value,
);
defaultItem && setSelectedItem(defaultItem);
} else if (defaultOption) {
setSelectedItem(defaultOption);
}
}, [options]);
}, [options, value, defaultOption]);

return (
<div className="dropdown__wrapper" {...getToggleButtonProps()}>
Expand All @@ -133,7 +136,7 @@ export const InputDropdown = forwardRef<HTMLInputElement, TDropdownProps>(
onKeyDown={() => setShouldFilterList(true)}
readOnly={!isAutocomplete}
type="select"
value={value}
value={selectedItem?.value}
{...getInputProps()}
{...rest}
/>
Expand Down