Skip to content

Commit e615835

Browse files
committed
taglist filter support
1 parent 7991977 commit e615835

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

packages/react-aria-components/src/GridList.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ class GridListLoaderNode extends CollectionNode<any> {
542542
constructor(key: Key) {
543543
super(GridListLoaderNode.type, key);
544544
}
545+
546+
filter(): CollectionNode<any> | null {
547+
return this.clone();
548+
}
545549
}
546550

547551
export const GridListLoadMoreItem = createLeafComponent(GridListLoaderNode, function GridListLoadingIndicator(props: GridListLoadMoreItemProps, ref: ForwardedRef<HTMLDivElement>, item: Node<object>) {

packages/react-aria-components/src/TagGroup.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import {ContextValue, DOMProps, Provider, RenderProps, SlotProps, StyleRenderPro
1818
import {filterDOMProps, mergeProps, useObjectRef} from '@react-aria/utils';
1919
import {forwardRefType, GlobalDOMAttributes, HoverEvents, Key, LinkDOMProps, PressEvents} from '@react-types/shared';
2020
import {LabelContext} from './Label';
21-
import {ListState, Node, useListState} from 'react-stately';
21+
import {ListState, Node, UNSTABLE_useFilteredListState, useListState} from 'react-stately';
2222
import {ListStateContext} from './ListBox';
2323
import React, {createContext, ForwardedRef, forwardRef, JSX, ReactNode, useContext, useEffect, useRef} from 'react';
2424
import {TextContext} from './Text';
25+
import {UNSTABLE_InternalAutocompleteContext} from './Autocomplete';
2526

2627
export interface TagGroupProps extends Omit<AriaTagGroupProps<unknown>, 'children' | 'items' | 'label' | 'description' | 'errorMessage' | 'keyboardDelegate'>, DOMProps, SlotProps, GlobalDOMAttributes<HTMLDivElement> {}
2728

@@ -74,16 +75,19 @@ interface TagGroupInnerProps {
7475
}
7576

7677
function TagGroupInner({props, forwardedRef: ref, collection}: TagGroupInnerProps) {
78+
let {filter} = useContext(UNSTABLE_InternalAutocompleteContext) || {};
7779
let tagListRef = useRef<HTMLDivElement>(null);
7880
let [labelRef, label] = useSlot(
7981
!props['aria-label'] && !props['aria-labelledby']
8082
);
81-
let state = useListState({
83+
let tagGroupState = useListState({
8284
...props,
8385
children: undefined,
8486
collection
8587
});
8688

89+
let filteredState = UNSTABLE_useFilteredListState(tagGroupState, filter);
90+
8791
// Prevent DOM props from going to two places.
8892
let domProps = filterDOMProps(props, {global: true});
8993
let domPropOverrides = Object.fromEntries(Object.entries(domProps).map(([k]) => [k, undefined]));
@@ -96,7 +100,7 @@ function TagGroupInner({props, forwardedRef: ref, collection}: TagGroupInnerProp
96100
...props,
97101
...domPropOverrides,
98102
label
99-
}, state, tagListRef);
103+
}, filteredState, tagListRef);
100104

101105
return (
102106
<div
@@ -109,7 +113,7 @@ function TagGroupInner({props, forwardedRef: ref, collection}: TagGroupInnerProp
109113
values={[
110114
[LabelContext, {...labelProps, elementType: 'span', ref: labelRef}],
111115
[TagListContext, {...gridProps, ref: tagListRef}],
112-
[ListStateContext, state],
116+
[ListStateContext, filteredState],
113117
[TextContext, {
114118
slots: {
115119
description: descriptionProps,
@@ -201,6 +205,14 @@ class TagItemNode extends CollectionNode<any> {
201205
constructor(key: Key) {
202206
super(TagItemNode.type, key);
203207
}
208+
209+
filter(_, __, filterFn: (textValue: string) => boolean): CollectionNode<any> | null {
210+
if (filterFn(this.textValue)) {
211+
return this.clone();
212+
}
213+
214+
return null;
215+
}
204216
}
205217

206218
/**

packages/react-aria-components/stories/Autocomplete.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ AutocompleteWithAsyncListBox.story = {
946946

947947
// TODO: I'm skipping Breadcrumbs, Tabs for now, not sure it makes sense to filter that via Autocomplete
948948
// Filtering the Taggroup might make sense
949+
// TODO make all of the below examples async loading as well?
949950

950951
export const AutocompleteWithGridList = () => {
951952
return (

0 commit comments

Comments
 (0)