Skip to content

Commit 8a3bd22

Browse files
zernoniaclaude
andauthored
feat(Listbox): filter (#2824 item 7) (#3063)
* feat(Listbox): opt-in `filter` on ListboxRoot (#2824 item 7) `filter(text, search, value)` on `ListboxRoot` / `useListboxRoot` filters the items by the `ListboxFilter` value. Filtered-out items stay mounted but render nothing, so navigation, typeahead, select-all and range selection only see matches; a group with no match is hidden. In a `ListboxVirtualizer` the filter runs over `options` (via `textContent`), so filtered-out options take no virtual index or `aria-setsize` slot. Omitted or `null`, nothing is filtered (v2 behaviour). Combobox and Autocomplete keep their own engine until their PR. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(Listbox): regenerate API meta for `filter` Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(Combobox): keep the inner Listbox `filter` off in Combobox and Autocomplete Both roots forward `$attrs` to their inner `ListboxRoot`, so a `filter` attribute turned the Listbox filter on through `ComboboxInput` on top of their own filtering. Bind `:filter="null"` until the Combobox PR moves them onto it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(ColorSwatchPicker): omit the inherited Listbox `filter` prop Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(Listbox): filter review follow-ups - re-measure a reused item whose value changed while filtered out - clear a virtualized highlight whose option no longer matches - dev warning for object options filtered without `textContent` - a group with no registered items stays visible (SSR) - docs: recommend ListboxVirtualizer for large filtered lists - tests: typeahead, select-all and Shift ranges over filtered items (plain and virtualized), `filtered.count`, Combobox/Autocomplete stay inert Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b9de54b commit 8a3bd22

18 files changed

Lines changed: 1059 additions & 32 deletions

‎docs/content/docs/components/listbox.md‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Contains all the parts of a listbox. An `input` will also render when used withi
8989

9090
### Filter
9191

92-
Input element to perform filtering.
92+
Input element to perform filtering. Its value is the search term the root's `filter` runs against; without a `filter` on the root it only moves the highlight, and you filter the items yourself (see [Filtering](#filtering)).
9393

9494
<!-- @include: @/meta/ListboxFilter.md -->
9595

@@ -221,12 +221,80 @@ const selectedPeople = ref([people[0], people[1]])
221221
</template>
222222
```
223223

224+
### Filtering
225+
226+
Pass a `filter` function to `ListboxRoot` to filter the items by the `ListboxFilter` value. It is called as `filter(text, search, value)` with the item's `textValue` (or its text content) and returns whether the item matches. `useFilter` provides locale-aware `contains`, `startsWith` and `endsWith` matchers that fit this signature.
227+
228+
Items that do not match are not rendered, so keyboard navigation, typeahead and select-all skip them, and a `ListboxGroup` whose items all fail to match is hidden. Filtering is opt-in: without `filter` (or with `null`) the listbox renders every item.
229+
230+
```vue line=5,10
231+
<script setup lang="ts">
232+
import { ListboxContent, ListboxFilter, ListboxItem, ListboxRoot, useFilter } from 'reka-ui'
233+
234+
const fruits = ['Apple', 'Banana', 'Blueberry', 'Cherry', 'Grape', 'Mango']
235+
const { contains } = useFilter({ sensitivity: 'base' })
236+
</script>
237+
238+
<template>
239+
<ListboxRoot
240+
:filter="contains"
241+
>
242+
<ListboxFilter />
243+
<ListboxContent>
244+
<ListboxItem
245+
v-for="fruit in fruits"
246+
:key="fruit"
247+
:value="fruit"
248+
>
249+
{{ fruit }}
250+
</ListboxItem>
251+
</ListboxContent>
252+
</ListboxRoot>
253+
</template>
254+
```
255+
256+
Set `textValue` on an item whose content is not plain text, so the filter matches the text you want. The root exposes the result as `filtered` (`{ search, active, count }`, read it through a template ref), which you can use to render an empty state.
257+
258+
Each item stays a mounted component while it is filtered out, and clearing or widening the search renders every item that matches again. For a large list (thousands of items) use a `ListboxVirtualizer`, which only renders the visible window of the matching options.
259+
260+
With a `ListboxVirtualizer`, the filter runs over its `options` before they are virtualized: it matches `textContent(option)` (or the option's string form, so pass `textContent` for object options), and a filtered-out option takes no virtual index and does not count in `aria-setsize`.
261+
262+
```vue line=10,14
263+
<script setup lang="ts">
264+
import { ListboxContent, ListboxFilter, ListboxItem, ListboxRoot, ListboxVirtualizer, useFilter } from 'reka-ui'
265+
266+
const people = Array.from({ length: 1000 }, (_, i) => ({ id: i, name: `Person ${i}` }))
267+
const { contains } = useFilter({ sensitivity: 'base' })
268+
</script>
269+
270+
<template>
271+
<ListboxRoot
272+
:filter="contains"
273+
>
274+
<ListboxFilter />
275+
<ListboxContent class="h-72 overflow-auto">
276+
<ListboxVirtualizer
277+
v-slot="{ option }"
278+
:options="people"
279+
:text-content="(person) => person.name"
280+
>
281+
<ListboxItem :value="option">
282+
{{ option.name }}
283+
</ListboxItem>
284+
</ListboxVirtualizer>
285+
</ListboxContent>
286+
</ListboxRoot>
287+
</template>
288+
```
289+
224290
### Custom filtering
225291

292+
To filter on a server, or to control the list yourself, leave `filter` unset, bind the `ListboxFilter` value and render only the items you want.
293+
226294
```vue line=13,15-16,21,24
227295
<script setup lang="ts">
228296
import { ListboxContent, ListboxFilter, ListboxItem, ListboxRoot, useFilter } from 'reka-ui'
229-
import { ref } from 'vue'
297+
import { computed, ref } from 'vue'
230298
231299
const people = [
232300
{ id: 1, name: 'Durward Reynolds' },

‎docs/content/docs/guides/migration-v3-agent.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ Converted so far: `SwitchRoot`, `TabsRoot`, `Toggle`, `ToggleGroupRoot`, `Checkb
223223

224224
**Verify.** In each affected listbox: PageUp/PageDown move to the first/last option; ArrowLeft/ArrowRight move the caret in a filter; typing letters in a filter still filters; Enter still selects unless `getNavigationIntent` returns `null` for it. Run the type-check for the handler retyping.
225225

226+
### 9. Listbox filtering (no rewrite)
227+
228+
**What changed.** Nothing for existing code. `ListboxRoot` gained an opt-in `filter` prop: with `:filter="contains"` (from `useFilter`), the items whose text does not match the `ListboxFilter` value are not rendered, and keyboard navigation, typeahead and select-all skip them. Without `filter` (or with `null`) the listbox filters nothing, exactly as in v2. `ComboboxRoot` and `AutocompleteRoot` keep their own filtering and `ignoreFilter`; they do not accept `filter` yet.
229+
230+
**Rewrite rules.** Do not add `filter` to an existing listbox: one whose items are already filtered by hand keeps working unchanged. Mention listboxes that compute a filtered item list from the `ListboxFilter` value in the report as optional candidates; switching one over means passing `filter` and rendering the full list.
231+
226232
### Report
227233

228234
When you are done, give the developer:

‎docs/content/docs/guides/migration-v3.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,7 @@ Two additions come with it:
244244
- `textValue` on `ListboxItem` sets the text the root's label registry records for the item, and defaults to the item's text content. Set it when the visible content is not plain text (an icon, a component that updates on its own).
245245
246246
The `update:modelValue` and `beforeUpdate:modelValue` events of `ListboxRoot` are now typed as `T | T[] | undefined`: an array with `multiple`, and `undefined` when a single selection is cleared. A handler typed for a single `T` may need widening; the runtime payload is unchanged.
247+
248+
## Listbox filtering
249+
250+
`ListboxRoot` has a new, opt-in `filter` prop. Nothing changes until you pass one: without it (or with `null`) the listbox filters nothing, and a list you already filter by hand from the `ListboxFilter` value keeps working as in v2. With a filter function, for example `contains` from `useFilter({ sensitivity: 'base' })`, items whose text does not match the `ListboxFilter` value are not rendered, a group whose items all fail to match is hidden, and keyboard navigation, typeahead and select-all only see the matching items. In a `ListboxVirtualizer`, the filter runs over `options` (matching `textContent(option)`), so a filtered-out option takes no virtual index. See [Filtering](../components/listbox#filtering). `ComboboxRoot` and `AutocompleteRoot` keep their own filtering and `ignoreFilter` for now.

‎docs/content/meta/ComboboxVirtualizer.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
'name': 'textContent',
25-
'description': '<p>Text content for each item to achieve type-ahead feature</p>\n',
25+
'description': '<p>Text content for each item, used by typeahead and by a <code>ListboxRoot</code> <code>filter</code> (which otherwise matches the option\'s string form)</p>\n',
2626
'type': '((option: T) =&gt; string)',
2727
'required': false
2828
}
@@ -56,7 +56,7 @@
5656
| `estimateSize` | Estimated size (in px) of each item | `number \| ((index: number) => number)` | No | - |
5757
| `options` | List of items | `T` | Yes | - |
5858
| `overscan` | Number of items rendered outside the visible area | `number` | No | - |
59-
| `textContent` | Text content for each item to achieve type-ahead feature | `((option: T) => string)` | No | - |
59+
| `textContent` | Text content for each item, used by typeahead and by a ListboxRoot filter (which otherwise matches the option's string form) | `((option: T) => string)` | No | - |
6060

6161
**Slots**
6262

‎docs/content/meta/ListboxRoot.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
'type': 'boolean',
4040
'required': false
4141
},
42+
{
43+
'name': 'filter',
44+
'description': '<p>Filters the items by the <code>ListboxFilter</code> search term: called as <code>filter(text, search, value)</code> with the item\'s\n<code>textValue</code> (or text content; in a <code>ListboxVirtualizer</code>, <code>textContent(option)</code>), it returns whether the item\nmatches. Items that do not match are not rendered, so keyboard navigation, typeahead and select-all skip them.\nOmitted or <code>null</code>, the listbox does not filter (filter the items yourself, or on a server).</p>\n',
45+
'type': 'ListboxFilterFunction&lt;AcceptableValue&gt; | null',
46+
'required': false
47+
},
4248
{
4349
'name': 'getNavigationIntent',
4450
'description': '<p>Resolves what a keydown means for keyboard navigation. Return <code>undefined</code> to fall back to the default mapping\n(arrows, Home, End, PageUp, PageDown — orientation and reading-direction aware), <code>null</code> to opt the key out (for\nEnter this also skips the selection), or <code>\'select\'</code> to select the highlighted item as Enter does. <code>ListboxFilter</code>\nnever passes unmodified printable keys (they stay text input), and Space on a focused item already selects it, so a\n<code>\'select\'</code> intent for Space does not select it twice.</p>\n',
@@ -146,6 +152,7 @@
146152
| `defaultValue` | The value of the listbox when initially rendered. Use when you do not need to control the state of the Listbox | `AcceptableValue \| AcceptableValue[]` | No | - |
147153
| `dir` | The reading direction of the listbox when applicable. <br> If omitted, inherits globally from ConfigProvider or assumes LTR (left-to-right) reading mode. | `"ltr" \| "rtl"` | No | - |
148154
| `disabled` | When true, prevents the user from interacting with listbox | `boolean` | No | - |
155+
| `filter` | Filters the items by the ListboxFilter search term: called as filter(text, search, value) with the item's textValue (or text content; in a ListboxVirtualizer, textContent(option)), it returns whether the item matches. Items that do not match are not rendered, so keyboard navigation, typeahead and select-all skip them. Omitted or null, the listbox does not filter (filter the items yourself, or on a server). | `ListboxFilterFunction<AcceptableValue> \| null` | No | - |
149156
| `getNavigationIntent` | Resolves what a keydown means for keyboard navigation. Return undefined to fall back to the default mapping (arrows, Home, End, PageUp, PageDown — orientation and reading-direction aware), null to opt the key out (for Enter this also skips the selection), or 'select' to select the highlighted item as Enter does. ListboxFilter never passes unmodified printable keys (they stay text input), and Space on a focused item already selects it, so a 'select' intent for Space does not select it twice. | `((event: KeyboardEvent) => ListboxNavigationIntent \| null)` | No | - |
150157
| `highlightOnHover` | When true, hover over item will trigger highlight | `boolean` | No | - |
151158
| `loop` | When true, keyboard navigation will loop from last item to first, and vice versa. | `boolean` | No | - |

‎docs/content/meta/ListboxVirtualizer.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
'name': 'textContent',
25-
'description': '<p>Text content for each item to achieve type-ahead feature</p>\n',
25+
'description': '<p>Text content for each item, used by typeahead and by a <code>ListboxRoot</code> <code>filter</code> (which otherwise matches the option\'s string form)</p>\n',
2626
'type': '((option: T) =&gt; string)',
2727
'required': false
2828
}
@@ -56,7 +56,7 @@
5656
| `estimateSize` | Estimated size (in px) of each item | `number \| ((index: number) => number)` | No | - |
5757
| `options` | List of items | `T` | Yes | - |
5858
| `overscan` | Number of items rendered outside the visible area | `number` | No | - |
59-
| `textContent` | Text content for each item to achieve type-ahead feature | `((option: T) => string)` | No | - |
59+
| `textContent` | Text content for each item, used by typeahead and by a ListboxRoot filter (which otherwise matches the option's string form) | `((option: T) => string)` | No | - |
6060

6161
**Slots**
6262

‎packages/core/src/Autocomplete/AutocompleteRoot.vue‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ provideAutocompleteRootContext({
269269
:disabled="disabled"
270270
:highlight-on-hover="highlightOnHover"
271271
:loop="loop"
272+
:filter="null"
272273
@highlight="emits('highlight', $event as any)"
273274
>
274275
<slot

‎packages/core/src/ColorSwatchPicker/ColorSwatchPickerRoot.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ListboxRootEmits, ListboxRootProps } from '@/Listbox'
44
import type { ChangeEventDetails } from '@/shared'
55
import { useForwardPropsEmits } from '@/shared'
66
7-
export interface ColorSwatchPickerRootProps extends Omit<ListboxRootProps, 'by'> {
7+
export interface ColorSwatchPickerRootProps extends Omit<ListboxRootProps, 'by' | 'filter'> {
88
defaultValue?: string | string[]
99
modelValue?: string | string[]
1010
}

‎packages/core/src/Combobox/ComboboxRoot.vue‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ export type ComboboxRootEmits<T = AcceptableValue> = {
4949
}
5050
5151
// `getNavigationIntent` is omitted until the Combobox PR routes `ComboboxInput` through the intent resolution (#2824 item 8);
52-
// today the root would declare it without forwarding it.
53-
export interface ComboboxRootProps<T = AcceptableValue> extends Omit<ListboxRootProps<T>, 'orientation' | 'selectionBehavior' | 'getNavigationIntent'> {
52+
// today the root would declare it without forwarding it. `filter` likewise waits for the Combobox PR (#2824 item 7), which
53+
// moves this root's own filter engine onto it with `ignoreFilter` as the deprecated alias.
54+
export interface ComboboxRootProps<T = AcceptableValue> extends Omit<ListboxRootProps<T>, 'orientation' | 'selectionBehavior' | 'getNavigationIntent' | 'filter'> {
5455
/** The controlled open state of the Combobox. Can be binded with `v-model:open`. */
5556
open?: boolean
5657
/** The open state of the combobox when it is initially rendered. <br> Use when you do not need to control its open state. */
@@ -282,6 +283,7 @@ provideComboboxRootContext({
282283
:disabled="disabled"
283284
:highlight-on-hover="highlightOnHover"
284285
:loop="loop"
286+
:filter="null"
285287
:by="props.by as any"
286288
@highlight="emits('highlight', $event as any)"
287289
>

0 commit comments

Comments
 (0)