|
| 1 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 2 | + |
| 3 | +import { Automation } from "@/api/automations"; |
| 4 | +import { createFakeAutomation } from "@/mocks"; |
| 5 | +import { useState } from "react"; |
| 6 | + |
| 7 | +import { |
| 8 | + Combobox, |
| 9 | + ComboboxCommandEmtpy, |
| 10 | + ComboboxCommandGroup, |
| 11 | + ComboboxCommandInput, |
| 12 | + ComboboxCommandItem, |
| 13 | + ComboboxCommandList, |
| 14 | + ComboboxContent, |
| 15 | + ComboboxTrigger, |
| 16 | +} from "./combobox"; |
| 17 | + |
| 18 | +const meta: Meta<typeof ComboboxStory> = { |
| 19 | + title: "UI/Combobox", |
| 20 | + component: ComboboxStory, |
| 21 | +}; |
| 22 | +export default meta; |
| 23 | + |
| 24 | +const INFER_AUTOMATION = { |
| 25 | + value: "UNASSIGNED" as const, |
| 26 | + name: "Infer Automation" as const, |
| 27 | +} as const; |
| 28 | + |
| 29 | +const MOCK_DATA = [ |
| 30 | + createFakeAutomation(), |
| 31 | + createFakeAutomation(), |
| 32 | + createFakeAutomation(), |
| 33 | + createFakeAutomation(), |
| 34 | + createFakeAutomation(), |
| 35 | +]; |
| 36 | + |
| 37 | +const getButtonLabel = (data: Array<Automation>, fieldValue: string) => { |
| 38 | + if (fieldValue === INFER_AUTOMATION.value) { |
| 39 | + return INFER_AUTOMATION.name; |
| 40 | + } |
| 41 | + const automation = data?.find((automation) => automation.id === fieldValue); |
| 42 | + if (automation?.name) { |
| 43 | + return automation.name; |
| 44 | + } |
| 45 | + return undefined; |
| 46 | +}; |
| 47 | + |
| 48 | +/** Because ShadCN only filters by `value` and not by a specific field, we need to write custom logic to filter objects by id */ |
| 49 | +const filterAutomations = ( |
| 50 | + value: string, |
| 51 | + search: string, |
| 52 | + data: Array<Automation> | undefined, |
| 53 | +) => { |
| 54 | + const searchTerm = search.toLowerCase(); |
| 55 | + const automation = data?.find((automation) => automation.id === value); |
| 56 | + if (!automation) { |
| 57 | + return 0; |
| 58 | + } |
| 59 | + const automationName = automation.name.toLowerCase(); |
| 60 | + if (automationName.includes(searchTerm)) { |
| 61 | + return 1; |
| 62 | + } |
| 63 | + return 0; |
| 64 | +}; |
| 65 | + |
| 66 | +function ComboboxStory() { |
| 67 | + const [selectedAutomationId, setSelectedAutomationId] = useState< |
| 68 | + "UNASSIGNED" | (string & {}) |
| 69 | + >(INFER_AUTOMATION.value); |
| 70 | + |
| 71 | + const buttonLabel = getButtonLabel(MOCK_DATA, selectedAutomationId); |
| 72 | + |
| 73 | + return ( |
| 74 | + <Combobox> |
| 75 | + <ComboboxTrigger selected={Boolean(buttonLabel)}> |
| 76 | + {buttonLabel ?? "Select automation"} |
| 77 | + </ComboboxTrigger> |
| 78 | + <ComboboxContent |
| 79 | + filter={(value, search) => filterAutomations(value, search, MOCK_DATA)} |
| 80 | + > |
| 81 | + <ComboboxCommandInput placeholder="Search for an automation..." /> |
| 82 | + <ComboboxCommandEmtpy>No automation found</ComboboxCommandEmtpy> |
| 83 | + <ComboboxCommandList> |
| 84 | + <ComboboxCommandGroup> |
| 85 | + <ComboboxCommandItem |
| 86 | + selected={selectedAutomationId === INFER_AUTOMATION.value} |
| 87 | + onSelect={setSelectedAutomationId} |
| 88 | + value={INFER_AUTOMATION.value} |
| 89 | + > |
| 90 | + {INFER_AUTOMATION.name} |
| 91 | + </ComboboxCommandItem> |
| 92 | + {MOCK_DATA.map((automation) => ( |
| 93 | + <ComboboxCommandItem |
| 94 | + key={automation.id} |
| 95 | + selected={selectedAutomationId === automation.id} |
| 96 | + onSelect={setSelectedAutomationId} |
| 97 | + value={automation.id} |
| 98 | + > |
| 99 | + {automation.name} |
| 100 | + </ComboboxCommandItem> |
| 101 | + ))} |
| 102 | + </ComboboxCommandGroup> |
| 103 | + </ComboboxCommandList> |
| 104 | + </ComboboxContent> |
| 105 | + </Combobox> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +export const story: StoryObj = { name: "Combobox" }; |
0 commit comments