-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathVirtualList.stories.tsx
More file actions
238 lines (222 loc) · 5.68 KB
/
VirtualList.stories.tsx
File metadata and controls
238 lines (222 loc) · 5.68 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import type { Meta, StoryObj } from '@storybook/react-vite'
import { VerticalContainer } from '../VerticalContainer/VerticalContainer'
import { Description } from '../Typography/Description'
import { Metadata } from '../Metadata/Metadata'
import { FilterableList } from './FilterableList'
import { VirtualList } from './VirtualList'
import { Badge } from '../Badge/Badge'
interface MockItem {
id: string
name: string
description: string
category?: string
isActive?: boolean
}
const meta: Meta<typeof VirtualList> = {
title: 'Components/VirtualList',
component: VirtualList,
decorators: [
Story => (
<div
style={{
height: '400px',
border: '1px solid #e5e5e5',
borderRadius: '8px',
}}
>
<Story />
</div>
),
],
argTypes: {
estimatedListItemHeight: {
control: 'number',
description: 'Estimated height of each item in pixels',
},
className: {
control: 'text',
description: 'Additional CSS classes',
},
},
}
export default meta
type Story = StoryObj<typeof VirtualList<MockItem>>
const generateMockItems = (count: number): MockItem[] => {
const categories = ['Documents', 'Images', 'Videos', 'Audio', 'Other']
return Array.from({ length: count }, (_, i) => ({
id: `item-${i}`,
name: `Item ${i + 1}`,
description: `This is the description for item ${i + 1}`,
category: categories[i % categories.length],
isActive: i === 10,
}))
}
function renderListItem(item: MockItem, height = 48) {
return (
<VerticalContainer
className="h-12 py-1"
style={{ height: `${height}px` }}
>
<Metadata
key={item.id}
label={item.name}
value={<Badge>{item.category}</Badge>}
className="h-6"
/>
<Description className="text-xs">{item.description}</Description>
</VerticalContainer>
)
}
export const Default: Story = {
args: {
items: generateMockItems(100),
estimatedListItemHeight: 48,
renderListItem,
},
}
export const WithSelection: Story = {
args: {
items: generateMockItems(100),
isSelected: (item: MockItem) => item.isActive === true,
estimatedListItemHeight: 48,
renderListItem,
},
}
export const LargeDataset: Story = {
args: {
items: generateMockItems(10000),
estimatedListItemHeight: 48,
renderListItem,
},
}
export const VariableHeightContent: Story = {
args: {
items: generateMockItems(50).map((item, i) => ({
...item,
description:
i % 3 === 0
? `This is a much longer description for ${item.name} that will wrap to multiple lines and demonstrate how the virtual list handles variable content heights.`
: item.description,
})),
estimatedListItemHeight: 80,
renderListItem: item => renderListItem(item, 80),
},
}
export const EmptyState: Story = {
args: {
items: [],
estimatedListItemHeight: 60,
renderListItem: () => null,
},
}
export const SingleItem: Story = {
args: {
items: generateMockItems(1),
estimatedListItemHeight: 48,
renderListItem,
},
}
export const WithScrollToSelected: Story = {
args: {
items: generateMockItems(200).map((item, i) => ({
...item,
isActive: i === 150,
})),
estimatedListItemHeight: 48,
isSelected: (item: MockItem) => item.isActive === true,
renderListItem,
},
}
// FilterableList Stories
export const FilterableListDefault: Story = {
render: () => {
const items = generateMockItems(50)
return (
<FilterableList
items={items}
placeholder="Search items..."
filterOptions={{
keys: ['name', 'description'],
threshold: 0.3,
}}
>
{filteredItems => (
<VirtualList
items={filteredItems}
estimatedListItemHeight={48}
renderListItem={renderListItem}
/>
)}
</FilterableList>
)
},
}
export const FilterableListWithCounter: Story = {
render: () => {
const items = generateMockItems(100)
return (
<FilterableList
items={items}
placeholder="Filter by name or description..."
filterOptions={{
keys: ['name', 'description'],
threshold: 0.3,
}}
>
{filteredItems => (
<VirtualList
items={filteredItems}
estimatedListItemHeight={48}
renderListItem={renderListItem}
/>
)}
</FilterableList>
)
},
}
export const FilterableListDisabled: Story = {
render: () => {
const items = generateMockItems(20)
return (
<div
style={{
height: '400px',
border: '1px solid #e5e5e5',
borderRadius: '8px',
}}
>
<FilterableList
items={items}
disabled
placeholder="Search is disabled"
filterOptions={{
keys: ['name', 'description'],
}}
>
{filteredItems => (
<VirtualList
items={filteredItems}
estimatedListItemHeight={60}
renderListItem={(item: MockItem) => (
<div
key={item.id}
style={{
padding: '12px 16px',
borderBottom: '1px solid #e5e5e5',
backgroundColor: '#fff',
opacity: 0.6,
}}
>
<div style={{ fontWeight: 500 }}>{item.name}</div>
<div style={{ fontSize: '14px', color: '#666' }}>
{item.description}
</div>
</div>
)}
/>
)}
</FilterableList>
</div>
)
},
}