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
base: main
Are you sure you want to change the base?
Conversation
GET_BUILD |
Build successful! 🎉 |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
}
})
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@devongovett to address your valid concerns about having assumptions about the users data structure, i've added a Example in storybook react-spectrum/packages/@react-spectrum/listbox/stories/ListBox.stories.tsx Lines 1054 to 1078 in 5ffd5e1
|
GET_BUILD |
## API Changes
unknown top level export { type: 'any' } @react-stately/dataListOptions ListOptions<T> {
filter?: (T, string) => boolean
+ filterKey?: string
getKey?: (T) => Key
initialFilterText?: string
initialItems?: Array<T>
initialSelectedKeys?: 'all' | Iterable<Key>
it changed:
|
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 |
There was a problem hiding this comment.
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.
Closes 4930
✅ Pull Request Checklist:
📝 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: