Repository navigation
fix(DataTable): don't match empty or object cell values when filtering - #23993
officialasishkumar wants to merge 4 commits into
Conversation
|
All contributors have signed the DCO. |
✅ Deploy Preview for v12-carbon-web-components ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
👷 Deploy request for v12-carbon-react pending review.Visit the deploys page to approve it
|
✅ Deploy Preview for v11-carbon-web-components ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for v11-carbon-react ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for carbon-elements ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
[AI agent review — Carbon grounded] · Looks Good · Reviewed by: IBM Bob · Commit: 3787902 This PR updates defaultFilterRows in DataTable to ignore empty, boolean, plain object, and React element cell values during filtering, and guards against missing cells. 0 findings. |
|
I have read the DCO document and I hereby sign the DCO. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #23993 +/- ##
==========================================
- Coverage 91.24% 90.54% -0.70%
==========================================
Files 905 814 -91
Lines 81020 78931 -2089
Branches 10697 10304 -393
==========================================
- Hits 73926 71469 -2457
- Misses 6902 7259 +357
- Partials 192 203 +11
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| } | ||
|
|
||
| return ( | ||
| typeof value !== 'object' || value.toString !== Object.prototype.toString |
There was a problem hiding this comment.
Objects without an inherited toString method pass this check but then cause String(value) to throw an error. Is there a reason you chose this approach over another?
There was a problem hiding this comment.
No strong reason, that was an oversight. It now also checks that toString is a function, so Object.create(null) is skipped, and I added a test for it.
`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 carbon-design-system#23990 Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
3787902 to
1c0b098
Compare
| typeof value.toString === 'function' && | ||
| value.toString !== Object.prototype.toString | ||
| ); | ||
| }; |
There was a problem hiding this comment.
Could the value be converted to a string once instead? Cross realm plain objects can still pass this check, and accessing toString here and again during String(value) can invoke a getter twice. String conversion can also throw an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#realms
Edit: Hmm, even if the value is converted to a string once, the conversion can still invoke inherited methods. Could the default filter limit string conversion to explicitly supported types instead of using prototype checks?
There was a problem hiding this comment.
Agreed. It now converts only strings, numbers and bigints to text and ignores everything else, so no toString is ever invoked (officialasishkumar@b04de82).
There was a problem hiding this comment.
| * Copyright IBM Corp. 2025, 2026 |
There was a problem hiding this comment.
Updated the copyright year (officialasishkumar@b04de82).
There was a problem hiding this comment.
| * Copyright IBM Corp. 2016, 2026 |
There was a problem hiding this comment.
Updated the copyright year (officialasishkumar@b04de82).
…ilter-empty-cells
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 <officialasishkumar@gmail.com>
Closes #23990
The default
filterRowsimplementation inDataTablecompared the search text againstString(cell.value)for everything except booleans. Emptynull/undefinedcells therefore matched searches such as "nu" or "def". Cells holding a plain object or a React element (for example a<Link>) matched "object" through[object Object]. Either way, the table kept rows with no visible match for what the user typed.defaultFilterRowsnow skipsnull,undefined, booleans, and objects that only have the defaultObject.prototype.toString. Strings, numbers (including0), and objects with their own string form, such as arrays andDates, are matched as before.It also reads the cell value with optional chaining. Previously, a missing cell threw a
TypeErrorwhenever no earlier cell in the row matched; the existing "should handle a missing cell gracefully" test only passed because its search term matched the first cell.Changelog
New
Changed
defaultFilterRowsignores cell values without filterable text instead of matching theirString()outputRemoved
Testing / Reviewing
DataTablewith rows such as{ id: 'a', name: 'Alpha', owner: null }and{ id: 'b', name: 'Beta', owner: 'Kim' }.nuinto the search. Rowais no longer shown; before this change it matched through"null".<a href="#">Docs</a>) and searchobject: no rows match.kimor a number: matching works as before.yarn test packages/react/src/components/DataTablepasses (31 suites, 275 tests). Four of the new filter tests fail without the change tofilter.ts.PR Checklist
As the author of this PR, before marking ready for review, confirm you:
Updated documentation and storybook examples(the existingfilterRowsdocs still describe the behavior accurately)Followed the(shared logic, not behind any v12 flag)required v12 migration documentation
for any code change that affects v12, or struck through this item because
the PR does not affect v12
Tested for cross-browser consistency(logic-only change with no browser-specific behavior; verified in jsdom and headless Chromium)More details can be found in the pull request guide.