From ebc5d4bf986c475935f7fe8ef184b298cd184c83 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 7 Nov 2026 14:27:39 +0530 Subject: [PATCH 1/3] fix(DataTable): don't match empty or object cell values when filtering `defaultFilterRows` matched the search text against `String(cell.value)` for every value except booleans. Rows with an empty `null` or `undefined` cell matched searches such as "nu" or "def", and cells whose value is a plain object or a React element matched "object" through `[object Object]`, so the table kept rows with no visible match. Skip `null`, `undefined`, booleans, and objects that rely on the default `Object.prototype.toString`. Strings, numbers (including 0), and objects with their own string form, such as arrays and dates, are matched as before. Also read the cell value with optional chaining, so a missing cell no longer throws a TypeError when no earlier cell in the row matched. Closes #23990 Signed-off-by: Asish Kumar --- .../DataTable/tools/__tests__/filter-test.js | 73 +++++++++++++++++++ .../src/components/DataTable/tools/filter.ts | 25 ++++++- 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/packages/react/src/components/DataTable/tools/__tests__/filter-test.js b/packages/react/src/components/DataTable/tools/__tests__/filter-test.js index 45875e1deca0..d5d82f7d9a09 100644 --- a/packages/react/src/components/DataTable/tools/__tests__/filter-test.js +++ b/packages/react/src/components/DataTable/tools/__tests__/filter-test.js @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import React from 'react'; import { defaultFilterRows } from '../filter'; describe('defaultFilterRows', () => { @@ -58,6 +59,67 @@ describe('defaultFilterRows', () => { expect(result).toEqual([]); }); + it.each([ + ['null', null], + ['undefined', undefined], + ])('should not match the text %s in an empty cell', (inputValue, value) => { + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1'], + cellsById: { + ...cellsById, + '1-name': { value }, + }, + inputValue, + }); + + expect(result).toEqual([]); + }); + + it('should not match the string form of objects and React elements', () => { + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1', '2'], + cellsById: { + ...cellsById, + '1-name': { value: { first: 'Alice' } }, + '2-name': { value: Bob }, + }, + inputValue: 'object', + }); + + expect(result).toEqual([]); + }); + + it('should match objects that define their own string form', () => { + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1', '2'], + cellsById: { + ...cellsById, + '1-name': { value: ['Alice', 'Allison'] }, + '2-name': { value: { toString: () => 'Allison' } }, + }, + inputValue: 'allison', + }); + + expect(result).toEqual(['1', '2']); + }); + + it('should match the number zero', () => { + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1'], + cellsById: { + ...cellsById, + '1-age': { value: 0 }, + }, + inputValue: '0', + }); + + expect(result).toEqual(['1']); + }); + it('should perform a case-insensitive search', () => { const result = defaultFilterRows({ ...defaultOptions, @@ -86,6 +148,17 @@ describe('defaultFilterRows', () => { expect(result).toEqual([]); }); + it('should not throw when no cell in a row matches and a cell is missing', () => { + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1'], + cellsById: { '1-name': { value: 'Alice' } }, + inputValue: 'xyz', + }); + + expect(result).toEqual([]); + }); + it('should handle a missing cell gracefully', () => { const incompleteCellsById = { '1-name': { value: 'Alice' } }; diff --git a/packages/react/src/components/DataTable/tools/filter.ts b/packages/react/src/components/DataTable/tools/filter.ts index 019df28a7f3a..79b90e062eb1 100644 --- a/packages/react/src/components/DataTable/tools/filter.ts +++ b/packages/react/src/components/DataTable/tools/filter.ts @@ -5,9 +5,26 @@ * LICENSE file in the root directory of this source tree. */ +/** + * Determines whether a cell value has text that a filter can match. Without this + * check, empty cells and values without a meaningful string form (e.g., React + * elements) would match on their `String()` output, such as `null`, `undefined`, + * or `[object Object]`. + */ +const hasFilterableText = (value: unknown) => { + if (value === null || value === undefined || typeof value === 'boolean') { + return false; + } + + return ( + typeof value !== 'object' || value.toString !== Object.prototype.toString + ); +}; + /** * Filters row IDs based on whether any of the cell values in the row include - * the input value as a substring. Boolean cell values are ignored. + * the input value as a substring. Cell values without filterable text, such as + * booleans, empty cells, and React elements, are ignored. */ export const defaultFilterRows = ({ rowIds, @@ -34,11 +51,11 @@ export const defaultFilterRows = ({ return rowIds.filter((rowId) => headers.some(({ key }) => { const cellId = getCellId(rowId, key); - const cell = cellsById[cellId]; + const value = cellsById[cellId]?.value; - if (typeof cell.value === 'boolean') return false; + if (!hasFilterableText(value)) return false; - return String(cell.value).toLowerCase().includes(normalizedInput); + return String(value).toLowerCase().includes(normalizedInput); }) ); }; From 1c0b098cb5654a89191e0e7076fae177d85c0a75 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 16 Nov 2026 11:25:55 +0530 Subject: [PATCH 2/3] fix(DataTable): skip objects without toString when filtering Signed-off-by: Asish Kumar --- .../DataTable/tools/__tests__/filter-test.js | 14 ++++++++++++++ .../react/src/components/DataTable/tools/filter.ts | 13 ++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/react/src/components/DataTable/tools/__tests__/filter-test.js b/packages/react/src/components/DataTable/tools/__tests__/filter-test.js index d5d82f7d9a09..2e8fc2785570 100644 --- a/packages/react/src/components/DataTable/tools/__tests__/filter-test.js +++ b/packages/react/src/components/DataTable/tools/__tests__/filter-test.js @@ -91,6 +91,20 @@ describe('defaultFilterRows', () => { expect(result).toEqual([]); }); + it('should not throw on objects without a prototype', () => { + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1'], + cellsById: { + ...cellsById, + '1-name': { value: Object.create(null) }, + }, + inputValue: 'object', + }); + + expect(result).toEqual([]); + }); + it('should match objects that define their own string form', () => { const result = defaultFilterRows({ ...defaultOptions, diff --git a/packages/react/src/components/DataTable/tools/filter.ts b/packages/react/src/components/DataTable/tools/filter.ts index 79b90e062eb1..35dc63d6f720 100644 --- a/packages/react/src/components/DataTable/tools/filter.ts +++ b/packages/react/src/components/DataTable/tools/filter.ts @@ -16,15 +16,22 @@ const hasFilterableText = (value: unknown) => { return false; } + if (typeof value !== 'object') { + return true; + } + + // Objects without a `toString` method (e.g., `Object.create(null)`) cannot be + // converted with `String()`. return ( - typeof value !== 'object' || value.toString !== Object.prototype.toString + typeof value.toString === 'function' && + value.toString !== Object.prototype.toString ); }; /** * Filters row IDs based on whether any of the cell values in the row include - * the input value as a substring. Cell values without filterable text, such as - * booleans, empty cells, and React elements, are ignored. + * the input value as a substring. Cell values without filterable text are + * ignored. */ export const defaultFilterRows = ({ rowIds, From b04de82acd71c23f588a643b38509246000599fa Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 16 Nov 2026 20:45:02 +0530 Subject: [PATCH 3/3] fix(DataTable): limit default filter to primitive cell values Convert only strings, numbers and bigints to text so the filter never calls toString on arbitrary objects, and update copyright years. Signed-off-by: Asish Kumar --- .../DataTable/tools/__tests__/filter-test.js | 27 +++++++++++--- .../src/components/DataTable/tools/filter.ts | 36 ++++++++----------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/packages/react/src/components/DataTable/tools/__tests__/filter-test.js b/packages/react/src/components/DataTable/tools/__tests__/filter-test.js index 2e8fc2785570..1cc38f1f14fd 100644 --- a/packages/react/src/components/DataTable/tools/__tests__/filter-test.js +++ b/packages/react/src/components/DataTable/tools/__tests__/filter-test.js @@ -1,5 +1,5 @@ /** - * Copyright IBM Corp. 2025 + * Copyright IBM Corp. 2025, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. @@ -105,19 +105,36 @@ describe('defaultFilterRows', () => { expect(result).toEqual([]); }); - it('should match objects that define their own string form', () => { + it('should not call the string conversion of objects', () => { + const toString = jest.fn(() => 'Allison'); + const result = defaultFilterRows({ ...defaultOptions, rowIds: ['1', '2'], cellsById: { ...cellsById, - '1-name': { value: ['Alice', 'Allison'] }, - '2-name': { value: { toString: () => 'Allison' } }, + '1-name': { value: { toString } }, + '2-name': { value: ['Alice', 'Allison'] }, }, inputValue: 'allison', }); - expect(result).toEqual(['1', '2']); + expect(result).toEqual([]); + expect(toString).not.toHaveBeenCalled(); + }); + + it('should match bigint values', () => { + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1'], + cellsById: { + ...cellsById, + '1-age': { value: BigInt(42) }, + }, + inputValue: '42', + }); + + expect(result).toEqual(['1']); }); it('should match the number zero', () => { diff --git a/packages/react/src/components/DataTable/tools/filter.ts b/packages/react/src/components/DataTable/tools/filter.ts index 35dc63d6f720..8d4f1c128704 100644 --- a/packages/react/src/components/DataTable/tools/filter.ts +++ b/packages/react/src/components/DataTable/tools/filter.ts @@ -1,31 +1,25 @@ /** - * Copyright IBM Corp. 2016, 2025 + * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ /** - * Determines whether a cell value has text that a filter can match. Without this - * check, empty cells and values without a meaningful string form (e.g., React - * elements) would match on their `String()` output, such as `null`, `undefined`, - * or `[object Object]`. + * Converts a cell value to searchable text. Only primitive values with an + * unambiguous text form are supported, so no user-defined or inherited + * `toString` method is ever invoked. Returns `null` for everything else. */ -const hasFilterableText = (value: unknown) => { - if (value === null || value === undefined || typeof value === 'boolean') { - return false; +const getFilterableText = (value: unknown): string | null => { + if ( + typeof value === 'string' || + typeof value === 'number' || + typeof value === 'bigint' + ) { + return String(value); } - if (typeof value !== 'object') { - return true; - } - - // Objects without a `toString` method (e.g., `Object.create(null)`) cannot be - // converted with `String()`. - return ( - typeof value.toString === 'function' && - value.toString !== Object.prototype.toString - ); + return null; }; /** @@ -58,11 +52,9 @@ export const defaultFilterRows = ({ return rowIds.filter((rowId) => headers.some(({ key }) => { const cellId = getCellId(rowId, key); - const value = cellsById[cellId]?.value; - - if (!hasFilterableText(value)) return false; + const text = getFilterableText(cellsById[cellId]?.value); - return String(value).toLowerCase().includes(normalizedInput); + return text?.toLowerCase().includes(normalizedInput) ?? false; }) ); };