Skip to content

Commit 64f2a43

Browse files
committed
docs(combo-box): add virtualization example
1 parent 2527b5a commit 64f2a43

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

docs/src/pages/components/ComboBox.svx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ Set `allowCustomValue` to `true` to let users enter custom text that isn't in th
115115

116116
<FileSource src="/framed/ComboBox/AllowCustomValue" />
117117

118+
## Virtualized items (large lists)
119+
120+
Enable virtualization for large lists to improve performance. Only visible items are rendered in the DOM.
121+
122+
In the example below, 10,000 items are provided to the combobox but only 11 items (8 visible items + 3 overscan items) are rendered in the DOM.
123+
124+
<FileSource src="/framed/ComboBox/VirtualizedComboBox" />
125+
126+
## Virtualized items (custom overscan)
127+
128+
Overscanning is the process of rendering extra items above and below the viewport to ensure smooth scrolling. The default overscan value is 3.
129+
130+
Specify a custom value for `overscan` to control how many extra items are rendered above/below the viewport for smoother scrolling.
131+
132+
<FileSource src="/framed/ComboBox/VirtualizeOverscan" />
133+
134+
## Virtualized items (custom threshold)
135+
136+
The threshold is the minimum number of items required before virtualization activates. The default threshold value is 100.
137+
138+
Specify a custom value for `threshold` to control when virtualization activates. Below the threshold, all items are rendered normally without virtualization.
139+
140+
<FileSource src="/framed/ComboBox/VirtualizeThreshold" />
141+
118142
## Top direction
119143

120144
Set `direction` to `"top"` to make the dropdown menu appear above the input.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { ComboBox } from "carbon-components-svelte";
3+
4+
const items = Array.from({ length: 10_000 }, (_, i) => ({
5+
id: i,
6+
text: "Item " + (i + 1),
7+
}));
8+
9+
let value = "";
10+
let selectedId = undefined;
11+
</script>
12+
13+
<ComboBox
14+
virtualize={{ overscan: 100 }}
15+
labelText="High overscan (overscan: 100)"
16+
placeholder="Filter..."
17+
{items}
18+
shouldFilterItem={(item, value) =>
19+
item.text.toLowerCase().includes(value.toLowerCase())}
20+
bind:selectedId
21+
bind:value
22+
/>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { ComboBox } from "carbon-components-svelte";
3+
4+
const items = Array.from({ length: 100 }, (_, i) => ({
5+
id: i,
6+
text: "Item " + (i + 1),
7+
}));
8+
9+
let value = "";
10+
let selectedId = undefined;
11+
</script>
12+
13+
<ComboBox
14+
virtualize={{ threshold: 200 }}
15+
labelText="Custom threshold (threshold: 200)"
16+
placeholder="Filter..."
17+
{items}
18+
shouldFilterItem={(item, value) =>
19+
item.text.toLowerCase().includes(value.toLowerCase())}
20+
bind:selectedId
21+
bind:value
22+
/>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script>
2+
import { ComboBox, Stack } from "carbon-components-svelte";
3+
4+
const items = Array.from({ length: 10_000 }, (_, i) => ({
5+
id: i,
6+
text: "Item " + (i + 1),
7+
}));
8+
9+
let value = "";
10+
let selectedId = undefined;
11+
$: selectedItem = items.find((item) => item.id === selectedId);
12+
</script>
13+
14+
<Stack gap={5}>
15+
<ComboBox
16+
virtualize={true}
17+
labelText="Virtualized ComboBox (10,000 items)"
18+
placeholder="Filter..."
19+
{items}
20+
shouldFilterItem={(item, value) =>
21+
item.text.toLowerCase().includes(value.toLowerCase())}
22+
bind:selectedId
23+
bind:value
24+
/>
25+
<div>
26+
<strong>Selected:</strong>
27+
{selectedItem?.text}
28+
</div>
29+
</Stack>

0 commit comments

Comments
 (0)