-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathcomboxbox.stories.tsx
More file actions
109 lines (96 loc) · 2.88 KB
/
comboxbox.stories.tsx
File metadata and controls
109 lines (96 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type { Meta, StoryObj } from "@storybook/react";
import { Automation } from "@/api/automations";
import { createFakeAutomation } from "@/mocks";
import { useDeferredValue, useMemo, 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 UNASSIGNED = "UNASSIGNED";
const INFER_AUTOMATION = {
value: UNASSIGNED,
name: "Infer Automation" as const,
} as const;
const MOCK_DATA = Array.from({ length: 5 }, 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;
};
function ComboboxStory() {
const [search, setSearch] = useState("");
const [selectedAutomationId, setSelectedAutomationId] = useState<
typeof UNASSIGNED | (string & {})
>(INFER_AUTOMATION.value);
const deferredSearch = useDeferredValue(search);
const filteredData = useMemo(() => {
return MOCK_DATA.filter((automation) =>
automation.name.toLowerCase().includes(deferredSearch.toLowerCase()),
);
}, [deferredSearch]);
const isInferredOptionFiltered = INFER_AUTOMATION.name
.toLowerCase()
.includes(deferredSearch.toLowerCase());
const buttonLabel = getButtonLabel(MOCK_DATA, selectedAutomationId);
return (
<Combobox>
<ComboboxTrigger selected={Boolean(buttonLabel)}>
{buttonLabel ?? "Select automation"}
</ComboboxTrigger>
<ComboboxContent>
<ComboboxCommandInput
value={search}
onValueChange={setSearch}
placeholder="Search for an automation..."
/>
<ComboboxCommandEmtpy>No automation found</ComboboxCommandEmtpy>
<ComboboxCommandList>
<ComboboxCommandGroup>
{isInferredOptionFiltered && (
<ComboboxCommandItem
selected={selectedAutomationId === INFER_AUTOMATION.value}
onSelect={(value) => {
setSelectedAutomationId(value);
setSearch("");
}}
value={INFER_AUTOMATION.value}
>
{INFER_AUTOMATION.name}
</ComboboxCommandItem>
)}
{filteredData.map((automation) => (
<ComboboxCommandItem
key={automation.id}
selected={selectedAutomationId === automation.id}
onSelect={(value) => {
setSelectedAutomationId(value);
setSearch("");
}}
value={automation.id}
>
{automation.name}
</ComboboxCommandItem>
))}
</ComboboxCommandGroup>
</ComboboxCommandList>
</ComboboxContent>
</Combobox>
);
}
export const story: StoryObj = { name: "Combobox" };