diff --git a/site/src/components/Filter/FilterCombobox/FilterCombobox.test.tsx b/site/src/components/Filter/FilterCombobox/FilterCombobox.test.tsx index cba6ace07b0..3e36c2d7872 100644 --- a/site/src/components/Filter/FilterCombobox/FilterCombobox.test.tsx +++ b/site/src/components/Filter/FilterCombobox/FilterCombobox.test.tsx @@ -669,9 +669,17 @@ describe("FilterCombobox", () => { await user.tab(); expect(filtersButton).toHaveFocus(); + expect(input).toHaveAttribute("aria-expanded", "false"); + expect(input).not.toHaveAttribute("aria-controls"); await user.keyboard("{Enter}"); expect(input).toHaveFocus(); - await user.keyboard("{ArrowDown}{ArrowRight}"); + await user.keyboard("{ArrowDown}"); + expect(input).toHaveAttribute("aria-expanded", "true"); + expect(input).toHaveAttribute( + "aria-controls", + screen.getByRole("listbox").id, + ); + await user.keyboard("{ArrowRight}"); await screen.findByRole("option", { name: "alice" }); await user.keyboard("{ArrowDown}{Enter}"); @@ -1723,6 +1731,8 @@ describe("FilterCombobox", () => { await waitFor(() => expect(screen.getByRole("status")).toHaveTextContent("No filters found"), ); + expect(input).toHaveAttribute("aria-expanded", "false"); + expect(input).not.toHaveAttribute("aria-controls"); }); it("announces a category search with no match", async () => { diff --git a/site/src/components/Filter/FilterCombobox/primitives.tsx b/site/src/components/Filter/FilterCombobox/primitives.tsx index dda37b9a5da..24482cf2705 100644 --- a/site/src/components/Filter/FilterCombobox/primitives.tsx +++ b/site/src/components/Filter/FilterCombobox/primitives.tsx @@ -1,4 +1,4 @@ -import { Command as CommandPrimitive } from "cmdk"; +import { Command as CommandPrimitive, useCommandState } from "cmdk"; import { cn } from "cn"; import { XIcon } from "lucide-react"; import { @@ -543,6 +543,7 @@ export const FilterComboboxChipsInput: React.FC< return ( + + + ); +}; + +type ChipsInputElementProps = React.ComponentProps<"input">; + +/** + * Renders cmdk's input from the props `asChild` passes in. cmdk sets its ARIA + * props after the caller's, always marking the input expanded and pointing it + * at its list and highlighted row, even while neither is mounted. + */ +const ChipsInputElement: React.FC = ({ + "aria-controls": ariaControls, + "aria-activedescendant": ariaActiveDescendant, + ...props +}) => { + // With filtering off, cmdk counts every mounted item, and items mount only + // in the open popup's lists. + const expanded = useCommandState((state) => state.filtered.count > 0); + + return ( + ); };