diff --git a/frontend/__tests__/components/ui/dropdown.test.tsx b/frontend/__tests__/components/ui/dropdown.test.tsx index 883cc1abec..9d3bbc81db 100644 --- a/frontend/__tests__/components/ui/dropdown.test.tsx +++ b/frontend/__tests__/components/ui/dropdown.test.tsx @@ -154,6 +154,30 @@ describe("Dropdown", () => { const selectedOption = screen.getByRole("option", { name: "Option 1" }); expect(selectedOption).toHaveAttribute("aria-selected", "true"); }); + + it("should preserve selected value in input and show all options when reopening dropdown", async () => { + const user = userEvent.setup(); + render(); + + const trigger = screen.getByTestId("dropdown-trigger"); + await user.click(trigger); + await user.click(screen.getByRole("option", { name: "Option 1" })); + + // Reopen the dropdown + await user.click(trigger); + + const input = screen.getByRole("combobox"); + expect(input).toHaveValue("Option 1"); + expect( + screen.getByRole("option", { name: "Option 1" }), + ).toBeInTheDocument(); + expect( + screen.getByRole("option", { name: "Option 2" }), + ).toBeInTheDocument(); + expect( + screen.getByRole("option", { name: "Option 3" }), + ).toBeInTheDocument(); + }); }); describe("Clear button", () => { diff --git a/frontend/src/ui/dropdown/dropdown.tsx b/frontend/src/ui/dropdown/dropdown.tsx index 854095a1f6..103de4bcb2 100644 --- a/frontend/src/ui/dropdown/dropdown.tsx +++ b/frontend/src/ui/dropdown/dropdown.tsx @@ -32,9 +32,10 @@ export function Dropdown({ testId, }: DropdownProps) { const [inputValue, setInputValue] = useState(defaultValue?.label ?? ""); + const [searchTerm, setSearchTerm] = useState(""); const filteredOptions = options.filter((option) => - option.label.toLowerCase().includes(inputValue.toLowerCase()), + option.label.toLowerCase().includes(searchTerm.toLowerCase()), ); const { @@ -51,6 +52,7 @@ export function Dropdown({ inputValue, onInputValueChange: ({ inputValue: newValue }) => { setInputValue(newValue ?? ""); + setSearchTerm(newValue ?? ""); }, defaultSelectedItem: defaultValue, onSelectedItemChange: ({ selectedItem: newSelectedItem }) => { @@ -61,9 +63,10 @@ export function Dropdown({ selectedItem: currentSelectedItem, }) => { if (newIsOpen) { - setInputValue(""); + setSearchTerm(""); } else { setInputValue(currentSelectedItem?.label ?? ""); + setSearchTerm(""); } }, });