Skip to content

Commit 82978d7

Browse files
fix(table-core): auto-remove empty arrHas filter values like the other array filters (TanStack#6356)
* fix(table-core): auto-remove empty arrHas filter values like the other array filters arrHas was the only array-valued filter function without an autoRemove handler, so an empty array filter value was never removed and hid every row. * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 1240d1f commit 82978d7

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

packages/table-core/src/fns/filterFns.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,16 @@ export const filterFn_inNumberRange = Object.assign(
296296
/**
297297
* Keeps rows whose scalar column value equals at least one filter value.
298298
*/
299-
export const filterFn_arrHas = <
300-
TFeatures extends TableFeatures,
301-
TData extends RowData,
302-
>(
303-
row: Row<TFeatures, TData>,
304-
columnId: string,
305-
filterValue: Array<unknown>,
306-
) => {
307-
return filterValue.some((val) => row.getValue<unknown>(columnId) === val)
308-
}
299+
export const filterFn_arrHas = Object.assign(
300+
<TFeatures extends TableFeatures, TData extends RowData>(
301+
row: Row<TFeatures, TData>,
302+
columnId: string,
303+
filterValue: Array<unknown>,
304+
) => {
305+
return filterValue.some((val) => row.getValue<unknown>(columnId) === val)
306+
},
307+
{ autoRemove: (val: any) => testFalsy(val) || !val?.length },
308+
)
309309

310310
/**
311311
* Keeps rows whose array or string column value includes at least one filter value.

packages/table-core/tests/unit/fns/filterFns.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,26 @@ describe('Filter Functions', () => {
563563
})
564564
})
565565
})
566+
567+
describe('Array Filters', () => {
568+
describe('filterFns.arrHas.autoRemove', () => {
569+
const autoRemove = filterFns.arrHas.autoRemove!
570+
571+
it('should auto-remove when the filter value is undefined', () => {
572+
expect(autoRemove(undefined)).toBe(true)
573+
})
574+
it('should auto-remove when the filter value is null', () => {
575+
expect(autoRemove(null)).toBe(true)
576+
})
577+
it('should auto-remove when the filter value is an empty string', () => {
578+
expect(autoRemove('')).toBe(true)
579+
})
580+
it('should auto-remove when the filter value is an empty array', () => {
581+
expect(autoRemove([])).toBe(true)
582+
})
583+
it('should NOT auto-remove when the filter value is a non-empty array', () => {
584+
expect(autoRemove(['a'])).toBe(false)
585+
})
586+
})
587+
})
566588
})

0 commit comments

Comments
 (0)