Repository navigation
fix(react): prevent nested text counter updates in controlled inputs - #24033
heloiselui wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24033 +/- ##
==========================================
- Coverage 91.16% 90.68% -0.49%
==========================================
Files 906 814 -92
Lines 81101 79018 -2083
Branches 10739 10313 -426
==========================================
- Hits 73938 71659 -2279
- Misses 6971 7155 +184
- Partials 192 204 +12
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:
|
✅ Deploy Preview for v12-carbon-web-components ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for v11-carbon-react ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for v12-carbon-react ready!
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. |
✅ Deploy Preview for v11-carbon-web-components 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: 84455cb This PR derives text counts directly from value for controlled TextInput and TextArea components to prevent React error 185 (Maximum update depth exceeded) during rapid input. 0 findings. |
| counterMode?: 'character' | 'word'; | ||
| } | ||
|
|
||
| function getTextCount( |
There was a problem hiding this comment.
Suggestion to convert this function and setTextCount to arrow functions for consistency with the other functions in this file.
| input.dispatchEvent(new Event('input', { bubbles: true })); | ||
| await Promise.resolve(); | ||
| } | ||
| await new Promise((resolve) => setTimeout(resolve, 50)); |
There was a problem hiding this comment.
Should the test wait for the final input value and counter instead of using a fixed 50 ms delay? Thinking of the case where a slow CI environment won't like this delay.
diff --git a/packages/react/src/components/TextInput/__tests__/TextInput-test.js b/packages/react/src/components/TextInput/__tests__/TextInput-test.js
index 4fc722ad83..e33f1ff2be 100644
--- a/packages/react/src/components/TextInput/__tests__/TextInput-test.js
+++ b/packages/react/src/components/TextInput/__tests__/TextInput-test.js
@@ -8,7 +8,7 @@
import React from 'react';
import TextInput from '../TextInput';
import userEvent from '@testing-library/user-event';
-import { act, render, screen } from '@testing-library/react';
+import { act, render, screen, waitFor } from '@testing-library/react';
import { AILabel } from '../../AILabel';
import { FeatureFlags } from '../../FeatureFlags';
@@ -438,14 +438,15 @@ describe('TextInput', () => {
input.dispatchEvent(new Event('input', { bubbles: true }));
await Promise.resolve();
}
- await new Promise((resolve) => setTimeout(resolve, 50));
+ await waitFor(() => {
+ expect(input).toHaveValue('x'.repeat(150));
+ if (enableCounter) {
+ expect(screen.getByText('150/200')).toBeInTheDocument();
+ }
+ });
expect(onError).not.toHaveBeenCalled();
expect(onChange).toHaveBeenCalledTimes(150);
- expect(input).toHaveValue('x'.repeat(150));
- if (enableCounter) {
- expect(screen.getByText('150/200')).toBeInTheDocument();
- }
} finally {
unmount();
window.removeEventListener('error', onError);| value: string | number, | ||
| counterMode: TextAreaProps['counterMode'] |
There was a problem hiding this comment.
counterMode's type is derived from TextAreaProps but value's type isn't. Should they both be?
| value: string | number, | |
| counterMode: TextAreaProps['counterMode'] | |
| value: NonNullable<TextAreaProps['value']>, | |
| counterMode: TextAreaProps['counterMode'] |
Closes #23936
Fixes React error #185 (“Maximum update depth exceeded”) when controlled
TextInputandTextAreareceive rapid input updates. Counters now usevaluedirectly, while uncontrolled fields keep their existing behavior. Controlled textareas also keep the parent’s value when a paste exceeds the word limit.Note
I'll remove the temporary test story from
TextInput.stories.jsbefore merging.Changelog
New
Changed
valuefor controlled fields, preserving uncontrolled and ref behavior.TextAreavalues when pasted text exceeds the word limit.Removed
-
None.Testing / Reviewing
React Deploy Preview>TextInput>Test Rapid Controlled InputSend 150 input eventsfor each field.150/150 onChange calls, with no update depth errors in the console.enableCounterenabled and disabled.PR Checklist
As the author of this PR, before marking ready for review, confirm you:
[ ] Updated documentation and storybook examples[ ] Followed therequired v12 migration documentation
for any code change that affects v12, or struck through this item because
the PR does not affect v12
[ ] Addressed any impact on accessibility (a11y)[ ] Tested for cross-browser consistencyMore details can be found in the pull request guide.