Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested filtering in useListData #6124

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

gijsroge
Copy link

@gijsroge gijsroge commented Mar 29, 2024

Closes 4930

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

There is no example of the normal filter feature for useListData, if you want I can add a ListBox example in Spectrum's Storybook.

Alternatively you can test it using jest: yarn jest -t "useListData should support filtering nested items"

🧢 Your Project:

@snowystinger
Copy link
Member

GET_BUILD

@rspbot
Copy link

rspbot commented Apr 1, 2024

Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and test, I need to get a little feedback on a small bit of functionality.

You're welcome to chime in with an opinion as well

@@ -152,9 +152,28 @@ export function useListData<T>(options: ListOptions<T>): ListData<T> {
filterText: initialFilterText
});

const filterItems = (items: T[], filterText: string): T[] => {
return items.reduce((acc: T[], item: any) => {
if (item.children) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assuming something about the data structure of an item that isn't specified in the types. What if section objects use a different property name for their children or they come from a different data source entirely? I don't think useListData should be prescriptive about the objects that get passed into it.

If your items have children maybe useTreeData would be more appropriate?

Copy link
Author

@gijsroge gijsroge Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I see what you mean. The reason for this PR was mostly to expand support for Listbox filtering (nested items), AFAIK listbox does not support useTreeData and useTreeData does not support filtering out of the box?

What would be your ideal scenario to add support for this? Maybe a more generic way of filtering that does not depend on a specific property?

Maybe something like this?

useListData<T>({
  initialItems: items,
  filterKeys: ['children'],
  filter: (item: any, text) => {
    return contains(item.name, text)
  }
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So listbox does actually support being passed items from useTreeData, but you are right about useTreeData not supporting filtering out of the box. I've raised a question to the team about whether we'd prefer to commit to having nested item filter support supported useListData (which would then mean we'd need to update the rest of the useListData methods to work for nested data) or if we'd instead add filtering to useTreeData.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience. We had an internal discussion and decided we'd like to add filtering to useTreeData instead of having useListData be another tree implementation.

Would you like to have a go at that in this PR?

Copy link
Author

@gijsroge gijsroge May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snowystinger understood, feels better to do it in useTreeData indeed. I will take a stab at it. Do I start a new PR for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either one, whatever you're more comfortable with. If you use this one, we'll just rename it

@gijsroge
Copy link
Author

gijsroge commented May 2, 2024

@devongovett to address your valid concerns about having assumptions about the users data structure, i've added a filterKey option instead.

Example in storybook

function SearchableListBox() {
const { contains } = useFilter({ sensitivity: 'base' })
const list = useListData({
initialItems: withSection,
filterKey: 'children',
filter: (item, text) => {
return contains(item.name, text)
}
});
return (
<>
<input type="text" onChange={(e) => list.setFilterText(e.target.value)} />
<ListBox width="150px" items={list.items} aria-labelledby="labelSearchableListBox">
{(item) => (
<Section key={item.name} items={item.children} title={item.name}>
{(item) => <Item key={item.name}>{item.name}</Item>}
</Section>
)}
</ListBox >
</>
);
}

cc @snowystinger @reidbarber

@snowystinger
Copy link
Member

GET_BUILD

@rspbot
Copy link

rspbot commented May 8, 2024

@rspbot
Copy link

rspbot commented May 8, 2024

## API Changes

unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any', access: 'private' }
unknown top level export { type: 'any', access: 'private' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'identifier', name: 'Column' }
unknown top level export { type: 'identifier', name: 'Column' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }

@react-stately/data

ListOptions

 ListOptions<T> {
   filter?: (T, string) => boolean
+  filterKey?: string
   getKey?: (T) => Key
   initialFilterText?: string
   initialItems?: Array<T>
   initialSelectedKeys?: 'all' | Iterable<Key>
 

it changed:

  • useListData

filter?: (item: T, filterText: string) => boolean
filter?: (item: T, filterText: string) => boolean,
/** The key of the item that contains the nested items it should filter. */
filterKey?: string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to reconsider this prop name, there may be other useful circumstances beyond filtering for knowing the key which contains children/nested items.
For this PR I think it's fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useListData should filter on items in sections as well
7 participants