-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI v2] feat: Adds reusable Combobox UI component (#16637)
- Loading branch information
1 parent
3747d2a
commit e299e5a
Showing
5 changed files
with
285 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { Button } from "@/components/ui/button"; | ||
import { | ||
Command, | ||
CommandEmpty, | ||
CommandGroup, | ||
CommandInput, | ||
CommandItem, | ||
CommandList, | ||
} from "@/components/ui/command"; | ||
import { Icon } from "@/components/ui/icons"; | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from "@/components/ui/popover"; | ||
import { cn } from "@/lib/utils"; | ||
import { createContext, use, useState } from "react"; | ||
|
||
const ComboboxContext = createContext<{ | ||
open: boolean; | ||
setOpen: (open: boolean) => void; | ||
} | null>(null); | ||
|
||
const Combobox = ({ children }: { children: React.ReactNode }) => { | ||
const [open, setOpen] = useState(false); | ||
return ( | ||
<ComboboxContext.Provider value={{ open, setOpen }}> | ||
<Popover open={open} onOpenChange={setOpen}> | ||
{children} | ||
</Popover> | ||
</ComboboxContext.Provider> | ||
); | ||
}; | ||
|
||
const ComboboxTrigger = ({ | ||
selected = false, | ||
children, | ||
}: { selected?: boolean; withForm?: boolean; children: React.ReactNode }) => { | ||
const comboboxCtx = use(ComboboxContext); | ||
if (!comboboxCtx) { | ||
throw new Error("'ComboboxTrigger' must be a child of `Combobox`"); | ||
} | ||
const { open } = comboboxCtx; | ||
|
||
return ( | ||
<PopoverTrigger asChild className="w-full"> | ||
<Button | ||
aria-expanded={open} | ||
variant="outline" | ||
role="combobox" | ||
className={cn( | ||
"w-full justify-between", | ||
selected && "text-muted-foreground", | ||
)} | ||
> | ||
{children} | ||
<Icon id="ChevronsUpDown" className="h-4 w-4 opacity-50" /> | ||
</Button> | ||
</PopoverTrigger> | ||
); | ||
}; | ||
|
||
const ComboboxContent = ({ | ||
filter, | ||
children, | ||
}: { | ||
filter?: (value: string, search: string, keywords?: string[]) => number; | ||
children: React.ReactNode; | ||
}) => { | ||
return ( | ||
<PopoverContent fullWidth> | ||
<Command filter={filter}>{children}</Command> | ||
</PopoverContent> | ||
); | ||
}; | ||
|
||
const ComboboxCommandInput = ({ placeholder }: { placeholder?: string }) => { | ||
return <CommandInput placeholder={placeholder} className="h-9" />; | ||
}; | ||
|
||
const ComboboxCommandList = ({ children }: { children: React.ReactNode }) => { | ||
return <CommandList>{children}</CommandList>; | ||
}; | ||
|
||
const ComboboxCommandEmtpy = ({ children }: { children: React.ReactNode }) => { | ||
return <CommandEmpty>{children}</CommandEmpty>; | ||
}; | ||
|
||
const ComboboxCommandGroup = ({ children }: { children: React.ReactNode }) => { | ||
return <CommandGroup>{children}</CommandGroup>; | ||
}; | ||
|
||
const ComboboxCommandItem = ({ | ||
onSelect, | ||
selected = false, | ||
value, | ||
children, | ||
}: { | ||
onSelect: (value: string) => void; | ||
selected?: boolean; | ||
value: string; | ||
children: React.ReactNode; | ||
}) => { | ||
const comboboxCtx = use(ComboboxContext); | ||
if (!comboboxCtx) { | ||
throw new Error("'ComboboxCommandItem' must be a child of `Combobox`"); | ||
} | ||
const { setOpen } = comboboxCtx; | ||
|
||
return ( | ||
<CommandItem | ||
value={value} | ||
onSelect={() => { | ||
setOpen(false); | ||
onSelect(value); | ||
}} | ||
> | ||
{children} | ||
<Icon | ||
id="Check" | ||
className={cn("ml-auto", selected ? "opacity-100" : "opacity-0")} | ||
/> | ||
</CommandItem> | ||
); | ||
}; | ||
|
||
export { | ||
Combobox, | ||
ComboboxTrigger, | ||
ComboboxContent, | ||
ComboboxCommandInput, | ||
ComboboxCommandList, | ||
ComboboxCommandEmtpy, | ||
ComboboxCommandGroup, | ||
ComboboxCommandItem, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { Automation } from "@/api/automations"; | ||
import { createFakeAutomation } from "@/mocks"; | ||
import { useState } from "react"; | ||
|
||
import { | ||
Combobox, | ||
ComboboxCommandEmtpy, | ||
ComboboxCommandGroup, | ||
ComboboxCommandInput, | ||
ComboboxCommandItem, | ||
ComboboxCommandList, | ||
ComboboxContent, | ||
ComboboxTrigger, | ||
} from "./combobox"; | ||
|
||
const meta: Meta<typeof ComboboxStory> = { | ||
title: "UI/Combobox", | ||
component: ComboboxStory, | ||
}; | ||
export default meta; | ||
|
||
const INFER_AUTOMATION = { | ||
value: "UNASSIGNED" as const, | ||
name: "Infer Automation" as const, | ||
} as const; | ||
|
||
const MOCK_DATA = [ | ||
createFakeAutomation(), | ||
createFakeAutomation(), | ||
createFakeAutomation(), | ||
createFakeAutomation(), | ||
createFakeAutomation(), | ||
]; | ||
|
||
const getButtonLabel = (data: Array<Automation>, fieldValue: string) => { | ||
if (fieldValue === INFER_AUTOMATION.value) { | ||
return INFER_AUTOMATION.name; | ||
} | ||
const automation = data?.find((automation) => automation.id === fieldValue); | ||
if (automation?.name) { | ||
return automation.name; | ||
} | ||
return undefined; | ||
}; | ||
|
||
/** Because ShadCN only filters by `value` and not by a specific field, we need to write custom logic to filter objects by id */ | ||
const filterAutomations = ( | ||
value: string, | ||
search: string, | ||
data: Array<Automation> | undefined, | ||
) => { | ||
const searchTerm = search.toLowerCase(); | ||
const automation = data?.find((automation) => automation.id === value); | ||
if (!automation) { | ||
return 0; | ||
} | ||
const automationName = automation.name.toLowerCase(); | ||
if (automationName.includes(searchTerm)) { | ||
return 1; | ||
} | ||
return 0; | ||
}; | ||
|
||
function ComboboxStory() { | ||
const [selectedAutomationId, setSelectedAutomationId] = useState< | ||
"UNASSIGNED" | (string & {}) | ||
>(INFER_AUTOMATION.value); | ||
|
||
const buttonLabel = getButtonLabel(MOCK_DATA, selectedAutomationId); | ||
|
||
return ( | ||
<Combobox> | ||
<ComboboxTrigger selected={Boolean(buttonLabel)}> | ||
{buttonLabel ?? "Select automation"} | ||
</ComboboxTrigger> | ||
<ComboboxContent | ||
filter={(value, search) => filterAutomations(value, search, MOCK_DATA)} | ||
> | ||
<ComboboxCommandInput placeholder="Search for an automation..." /> | ||
<ComboboxCommandEmtpy>No automation found</ComboboxCommandEmtpy> | ||
<ComboboxCommandList> | ||
<ComboboxCommandGroup> | ||
<ComboboxCommandItem | ||
selected={selectedAutomationId === INFER_AUTOMATION.value} | ||
onSelect={setSelectedAutomationId} | ||
value={INFER_AUTOMATION.value} | ||
> | ||
{INFER_AUTOMATION.name} | ||
</ComboboxCommandItem> | ||
{MOCK_DATA.map((automation) => ( | ||
<ComboboxCommandItem | ||
key={automation.id} | ||
selected={selectedAutomationId === automation.id} | ||
onSelect={setSelectedAutomationId} | ||
value={automation.id} | ||
> | ||
{automation.name} | ||
</ComboboxCommandItem> | ||
))} | ||
</ComboboxCommandGroup> | ||
</ComboboxCommandList> | ||
</ComboboxContent> | ||
</Combobox> | ||
); | ||
} | ||
|
||
export const story: StoryObj = { name: "Combobox" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export { | ||
Combobox, | ||
ComboboxTrigger, | ||
ComboboxContent, | ||
ComboboxCommandInput, | ||
ComboboxCommandList, | ||
ComboboxCommandEmtpy, | ||
ComboboxCommandGroup, | ||
ComboboxCommandItem, | ||
} from "./combobox"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters