-
I'm just trying to get a simple ComboBox component to work. So I've built my And when selecting an item from the listbox, the item gets the
Any ideas what's the matter with my implementation? I'd expect that the ComboBox sets the correct props to <ComboBoxField>
{items.map((item) => (
<ComboBoxItem key={item} id={item}>
{item}
</ComboBoxItem>
))}
</ComboBoxField> import {
ComboBox as ComboBoxPrimitive,
ComboBoxProps,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemProps,
Popover,
} from "react-aria-components";
import { cva } from "class-variance-authority";
import { FieldText } from "./FieldText";
import { ChevronDown, Check } from "lucide-react";
import { Button } from "./Button"; // is built on top of a react-aria-components Button
export const ComboBoxField = <T extends { id: string; name: string }>({
label,
description,
errorMessage,
"aria-label": ariaLabel,
children,
...props
}: ComboBoxProps<T> & {
label?: string;
description?: string;
errorMessage?: string;
"aria-label"?: string;
}) => {
return (
<ComboBoxPrimitive
{...props}
menuTrigger="focus"
shouldFocusWrap
aria-label={label || ariaLabel || "Combobox"}
>
{label && <Label className="text-sm font-medium">{label}</Label>}
<div className="relative [&>button]:absolute [&>button]:right-0 [&>button]:top-0">
<Input />
<Button intent="ghost" size="sm">
<ChevronDown className="size-4" />
</Button>
</div>
{description && <FieldText>{description}</FieldText>}
{errorMessage && <FieldText intent="error">{errorMessage}</FieldText>}
<Popover className="bg-background rounded-md shadow p-2 w-[--trigger-width]">
<ListBox
className="max-h-[300px] w-full overflow-auto outline-none"
selectionMode="single"
>
{children}
</ListBox>
</Popover>
</ComboBoxPrimitive>
);
};
const comboBoxItemStyles = cva([
"flex",
"items-center",
"gap-3",
"w-full",
"text-sm",
"px-4",
"py-2",
"outline-none",
"cursor-pointer",
"rounded-md",
"transition-colors",
"data-[selected]:font-semibold",
"data-[focused]:bg-secondary",
"data-[focused]:text-secondary-foreground",
])();
export const ComboBoxItem = ({ children, ...props }: ListBoxItemProps) => {
return (
<ListBoxItem {...props} className={comboBoxItemStyles}>
{({ isSelected }) => (
<>
{children}
{isSelected && <Check className="size-4" />}
</>
)}
</ListBoxItem>
);
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I found out why: I forgot to add => But this means that the docs are definitely out of date – what can we do about it? Who to tag? |
Beta Was this translation helpful? Give feedback.
I found out why: I forgot to add
textValue
to<ListBoxItem>
. Got a warning on the console about it.=> But this means that the docs are definitely out of date – what can we do about it? Who to tag?