How can I avoid removing empty filters entirely? #6057
Replies: 2 comments
|
FWIW, since I'm integrating this as the underlying table solution for a shared design system package, I just ended up bailing out of the built-in filtering and wrapping the tools with my own filtering state solution. Not particularly difficult to do and gives the adaptability I needed. I think that's perfectly fine (and it's nice that it's easy to bail out like this), but I do still think the filter removal behavior is a bit too opinionated as the internal behavior. |
|
I checked export function shouldAutoRemoveFilter(filterFn, value, column) {
if (typeof value === 'undefined') {
return true
}
if (filterFn?.autoRemove) {
return !!filterFn.autoRemove(value, column)
}
return typeof value === 'string' && !value
}
const keepEmptyFilterFn: FilterFn<any> = (row, columnId, filterValue) => {
// your actual matching logic, including the empty case
}
keepEmptyFilterFn.autoRemove = () => falseThen set it via |
Uh oh!
There was an error while loading. Please reload this page.
For my company's table UX, we want to make filters configurable outside the table itself via a menu system. This is similar to filter "pills" which can be added by selecting a column name to filter on. They are initialized with a default empty value, and the user can immediately add a more meaningful filter value in the menu. Removal of filters happens in this menu as well, by explicit action from the user.
I can't figure out how to achieve this with Tanstack Table's filter system, even after I overrode all built-in
filterFnoptions with versions that have.autoRemove = () => false;. So I dug into the source and found this. It appears that the library enforces removing filters if their value is empty regardless ofautoRemove?I think this behavior is surprisingly opinionated for such an otherwise versatile tool. Can that logic be changed to utilize
??instead, so that anyautoRemovesupplied value will be considered authoritative? Or better yet, canautoRemovebe globally configurable, so I don't have to override all the default filter functions at all?Marked as Q&A because hopefully there's a way to do this I didn't notice.
All reactions