diff --git a/src/components/form-field/single-combobox/single-combobox-input.tsx b/src/components/form-field/single-combobox/single-combobox-input.tsx index 9c609e71..2ed89ee4 100644 --- a/src/components/form-field/single-combobox/single-combobox-input.tsx +++ b/src/components/form-field/single-combobox/single-combobox-input.tsx @@ -2,28 +2,28 @@ import { Combobox as HeadlessCombobox } from "@headlessui/react"; import React from "react"; import { CaretDownIcon } from "../../../icons"; -export interface SingleComboboxInputProps { +export interface SingleComboboxInputProps { id: string; - displayValue: string; + displayValue?(item: TValue): string; placeholder: string; onChange: (event: React.ChangeEvent) => void; showButton?: boolean; } -export const SingleComboboxInput = ({ +export const SingleComboboxInput = ({ id, displayValue, placeholder, onChange, showButton = true, -}: SingleComboboxInputProps) => { +}: SingleComboboxInputProps) => { return (
displayValue} + displayValue={displayValue} onChange={onChange} className="paragraph-100 flex h-8 w-full items-center rounded border border-neutral-400 py-2 pl-3 pr-8 focus-visible:border-primary-400 focus-visible:ring-2 focus-visible:ring-primary-200 disabled:bg-neutral-100 disabled:text-neutral-600 disabled:border-neutral-300" /> diff --git a/src/components/form-field/single-combobox/single-combobox.stories.tsx b/src/components/form-field/single-combobox/single-combobox.stories.tsx index 8bbe9703..8b765130 100644 --- a/src/components/form-field/single-combobox/single-combobox.stories.tsx +++ b/src/components/form-field/single-combobox/single-combobox.stories.tsx @@ -42,7 +42,6 @@ const SingleComboboxWithHooks = () => { > setQuery(event.target.value)} /> @@ -64,6 +63,56 @@ const SingleComboboxWithHooks = () => { ); }; +interface Person { + id: number; + name: string; + short: string; +} + +const peopleObjects: Person[] = [ + { id: 1, name: "Durward Reynolds", short: "Durward" }, + { id: 2, name: "Kenton Towne", short: "Kenton" }, + { id: 3, name: "Therese Wunsch", short: "Therese" }, + { id: 4, name: "Benedict Kessler", short: "Benedict" }, + { id: 5, name: "Katelyn Rohan", short: "Katelyn" }, +]; + +const SingleComboboxObjectValueWithHooks = () => { + const [selectedPerson, setSelectedPerson] = React.useState(); + const [query, setQuery] = React.useState(""); + + const filteredPeople = + query === "" + ? peopleObjects + : peopleObjects.filter((person) => { + return person.name.toLowerCase().includes(query.toLowerCase()); + }); + + return ( + { + setQuery(""); + setSelectedPerson(value); + }} + > + person.name} + onChange={(event) => setQuery(event.target.value)} + /> + + {filteredPeople.map((person) => ( + + {person.short} + + ))} + + + ); +}; + const SingleComboboxCustomValueWithHooks = () => { const [selectedPerson, setSelectedPerson] = React.useState(""); const [query, setQuery] = React.useState(""); @@ -85,7 +134,6 @@ const SingleComboboxCustomValueWithHooks = () => { > setQuery(event.target.value)} /> @@ -114,6 +162,14 @@ export const Default: Story = { ), }; +export const ObjectValue: Story = { + render: () => ( +
+ +
+ ), +}; + export const CustomValue: Story = { render: () => (
diff --git a/src/components/form-field/single-combobox/single-combobox.tsx b/src/components/form-field/single-combobox/single-combobox.tsx index 7bb52e0d..8c3fb8b4 100644 --- a/src/components/form-field/single-combobox/single-combobox.tsx +++ b/src/components/form-field/single-combobox/single-combobox.tsx @@ -8,7 +8,7 @@ import { SingleComboboxCustomOption } from "./single-combobox-custom-option"; import { SingleComboboxEmptyOption } from "./single-combobox-empty-option"; export interface SingleComboboxProps { - value: TValue; + value?: TValue; onChange: (value: TValue) => void; children: React.ReactNode; disabled?: boolean;