feat(dashboard): add an accessible Select to the console

The console has no select primitive: form widgets fall back to the
native element, and the shared Dropdown has no listbox roles, no
keyboard navigation and no label association. Base UI, already a
dependency, ships one; this wraps it the same way dialog.tsx wraps the
Base UI dialog so pickers get a labelled, keyboard-driven listbox that
matches the console's buttons.

Assisted-by: LLM
Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-04 18:45:36 +07:00
1 parent 2b823ab79f
commit 3b1302ba8e
2 files changed
+171

No files matched your search

@@ -0,0 +1,64 @@
import { describe, it, expect, vi } from "vitest"
import { render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { SelectItem, SelectLabel, SelectPopup, SelectRoot, SelectTrigger, SelectValue } from "./select.tsx"
const items = [
{ value: "", label: "All" },
{ value: "db-0", label: "db-0" },
{ value: "db-1", label: "db-1" },
]
function PodSelect({ value = "", onValueChange = () => {} }: { value?: string; onValueChange?: (v: string) => void }) {
return (
<SelectRoot items={items} value={value} onValueChange={(v) => onValueChange(v ?? "")}>
<SelectLabel>Pod</SelectLabel>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectPopup>
{items.map((i) => (
<SelectItem key={i.value} value={i.value}>
{i.label}
</SelectItem>
))}
</SelectPopup>
</SelectRoot>
)
}
describe("Select", () => {
it("names the trigger by its label and shows the chosen item", () => {
render(<PodSelect value="db-1" />)
expect(screen.getByRole("combobox", { name: "Pod" })).toHaveTextContent("db-1")
})
it("shows the label of an empty-string value", () => {
render(<PodSelect />)
expect(screen.getByRole("combobox", { name: "Pod" })).toHaveTextContent("All")
})
it("lists the items and reports the one clicked", async () => {
const onValueChange = vi.fn()
render(<PodSelect onValueChange={onValueChange} />)
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
await userEvent.click(await screen.findByRole("option", { name: "db-1" }))
expect(onValueChange).toHaveBeenCalledWith("db-1")
})
it("can be driven from the keyboard", async () => {
const onValueChange = vi.fn()
render(<PodSelect onValueChange={onValueChange} />)
screen.getByRole("combobox", { name: "Pod" }).focus()
await userEvent.keyboard("{ArrowDown}")
expect(await screen.findByRole("listbox")).toBeInTheDocument()
await userEvent.keyboard("{ArrowDown}{Enter}")
expect(onValueChange).toHaveBeenCalledWith("db-0")
})
})
@@ -0,0 +1,107 @@
import { Select as SelectPrimitive } from "@base-ui/react/select"
import { Check, ChevronDown } from "lucide-react"
import { cn } from "@cozystack/ui"
const SelectRoot = SelectPrimitive.Root
function SelectLabel({
className,
...props
}: SelectPrimitive.Label.Props) {
return (
<SelectPrimitive.Label
data-slot="select-label"
className={cn("text-xs text-slate-600", className)}
{...props}
/>
)
}
function SelectTrigger({
className,
children,
...props
}: SelectPrimitive.Trigger.Props) {
return (
<SelectPrimitive.Trigger
data-slot="select-trigger"
className={cn(
"inline-flex h-7 min-w-0 items-center justify-between gap-1.5 rounded-lg border border-slate-300 bg-white px-2 text-xs text-slate-900 shadow-xs outline-none transition-colors select-none hover:bg-slate-50 focus-visible:ring-2 focus-visible:ring-slate-400 focus-visible:ring-offset-2 data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[popup-open]:bg-slate-50",
className
)}
{...props}
>
{children}
<SelectPrimitive.Icon className="shrink-0 text-slate-400">
<ChevronDown className="size-3.5" />
</SelectPrimitive.Icon>
</SelectPrimitive.Trigger>
)
}
function SelectValue({
className,
...props
}: SelectPrimitive.Value.Props) {
return (
<SelectPrimitive.Value
data-slot="select-value"
className={cn("truncate", className)}
{...props}
/>
)
}
function SelectPopup({
className,
children,
...props
}: SelectPrimitive.Popup.Props) {
return (
<SelectPrimitive.Portal>
<SelectPrimitive.Positioner className="z-50 outline-none" sideOffset={4} alignItemWithTrigger={false}>
<SelectPrimitive.Popup
data-slot="select-popup"
className={cn(
"max-h-[min(var(--available-height),20rem)] max-w-[min(var(--available-width),32rem)] min-w-[var(--anchor-width)] overflow-y-auto rounded-lg bg-white py-1 text-xs text-slate-900 shadow-lg ring-1 ring-slate-900/8 outline-none",
className
)}
{...props}
>
<SelectPrimitive.List>{children}</SelectPrimitive.List>
</SelectPrimitive.Popup>
</SelectPrimitive.Positioner>
</SelectPrimitive.Portal>
)
}
function SelectItem({
className,
children,
...props
}: SelectPrimitive.Item.Props) {
return (
<SelectPrimitive.Item
data-slot="select-item"
className={cn(
"grid cursor-default grid-cols-[1rem_1fr] items-center gap-1.5 px-2 py-1.5 outline-none select-none data-[highlighted]:bg-slate-100 data-[selected]:font-medium",
className
)}
{...props}
>
<SelectPrimitive.ItemIndicator className="col-start-1">
<Check className="size-3.5" />
</SelectPrimitive.ItemIndicator>
<SelectPrimitive.ItemText className="col-start-2 break-all">{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
)
}
export {
SelectRoot,
SelectLabel,
SelectTrigger,
SelectValue,
SelectPopup,
SelectItem,
}