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..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,10 +1,11 @@ /** - * 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. */ +import React from 'react'; import { defaultFilterRows } from '../filter'; describe('defaultFilterRows', () => { @@ -58,6 +59,98 @@ 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 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 not call the string conversion of objects', () => { + const toString = jest.fn(() => 'Allison'); + + const result = defaultFilterRows({ + ...defaultOptions, + rowIds: ['1', '2'], + cellsById: { + ...cellsById, + '1-name': { value: { toString } }, + '2-name': { value: ['Alice', 'Allison'] }, + }, + inputValue: 'allison', + }); + + 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', () => { + 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 +179,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..8d4f1c128704 100644 --- a/packages/react/src/components/DataTable/tools/filter.ts +++ b/packages/react/src/components/DataTable/tools/filter.ts @@ -1,13 +1,31 @@ /** - * 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. */ +/** + * 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 getFilterableText = (value: unknown): string | null => { + if ( + typeof value === 'string' || + typeof value === 'number' || + typeof value === 'bigint' + ) { + return String(value); + } + + return null; +}; + /** * 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 are + * ignored. */ export const defaultFilterRows = ({ rowIds, @@ -34,11 +52,9 @@ export const defaultFilterRows = ({ return rowIds.filter((rowId) => headers.some(({ key }) => { const cellId = getCellId(rowId, key); - const cell = cellsById[cellId]; - - if (typeof cell.value === 'boolean') return false; + const text = getFilterableText(cellsById[cellId]?.value); - return String(cell.value).toLowerCase().includes(normalizedInput); + return text?.toLowerCase().includes(normalizedInput) ?? false; }) ); };