Skip to content

fix(react): prevent nested text counter updates in controlled inputs - #24033

Open
heloiselui wants to merge 3 commits into
carbon-design-system:mainfrom
heloiselui:23936-fix-text-count
Open

heloiselui wants to merge 3 commits into
carbon-design-system:mainfrom
heloiselui:23936-fix-text-count

Conversation

@heloiselui

Copy link
Copy Markdown
Contributor

Closes #23936

Fixes React error #185 (“Maximum update depth exceeded”) when controlled TextInput and TextArea receive rapid input updates. Counters now use value directly, 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.js before merging.

Changelog

New

  • Regression tests.

Changed

  • Calculate text counts from value for controlled fields, preserving uncontrolled and ref behavior.
  • Preserve controlled TextArea values when pasted text exceeds the word limit.

Removed

-None.

Testing / Reviewing

  1. Go to React Deploy Preview > TextInput > Test Rapid Controlled Input
  2. Click Send 150 input events for each field.
  3. Verify each reports 150/150 onChange calls, with no update depth errors in the console.
  4. Repeat with enableCounter enabled and disabled.

PR Checklist

As the author of this PR, before marking ready for review, confirm you:

  • Reviewed every line of the diff
  • [ ] Updated documentation and storybook examples
  • [ ] Followed the
    required v12 migration documentation
    for any code change that affects v12, or struck through this item because
    the PR does not affect v12
  • Wrote passing tests that cover this change
  • [ ] Addressed any impact on accessibility (a11y)
  • [ ] Tested for cross-browser consistency
  • Validated that this code is ready for review and status checks should pass

More details can be found in the pull request guide.

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.68%. Comparing base (4b4fce0) to head (8aac50e).
⚠️ Report is 19 commits behind head on main.

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     
Flag Coverage Δ
angular ?
main-packages 88.13% <100.00%> (-0.11%) ⬇️
web-components 91.45% <ø> (-0.43%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v12-carbon-web-components ready!

Name Link
🔨 Latest commit 8aac50e
🔍 Latest deploy log https://app.netlify.com/projects/v12-carbon-web-components/deploys/6afb1178d21916d12512e9fb
😎 Deploy Preview https://deploy-preview-24033--v12-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v11-carbon-react ready!

Name Link
🔨 Latest commit 8aac50e
🔍 Latest deploy log https://app.netlify.com/projects/v11-carbon-react/deploys/6afb1178a72ab217ce79da17
😎 Deploy Preview https://deploy-preview-24033--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v12-carbon-react ready!

Name Link
🔨 Latest commit 8aac50e
🔍 Latest deploy log https://app.netlify.com/projects/v12-carbon-react/deploys/6afb1178123cc7cdc1ba5aba
😎 Deploy Preview https://deploy-preview-24033--v12-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 8aac50e
🔍 Latest deploy log https://app.netlify.com/projects/carbon-elements/deploys/6afb1178744672cacf697714
😎 Deploy Preview https://deploy-preview-24033--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v11-carbon-web-components ready!

Name Link
🔨 Latest commit 8aac50e
🔍 Latest deploy log https://app.netlify.com/projects/v11-carbon-web-components/deploys/6afb1178f44bc371ec6ff045
😎 Deploy Preview https://deploy-preview-24033--v11-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@heloiselui
heloiselui marked this pull request as ready for review November 11, 2026 20:24
@heloiselui
heloiselui requested a review from a team as a code owner November 11, 2026 20:24
@carbon-monitor-agent

Copy link
Copy Markdown
Collaborator

[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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Comment on lines +214 to +215
value: string | number,
counterMode: TextAreaProps['counterMode']

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

counterMode's type is derived from TextAreaProps but value's type isn't. Should they both be?

Suggested change
value: string | number,
counterMode: TextAreaProps['counterMode']
value: NonNullable<TextAreaProps['value']>,
counterMode: TextAreaProps['counterMode']

This branch was successfully deployed

1 active deployment
@carbon/react storybook preview — 8aac50ec Deployed Nov 16, 2026 by heloiselui via react-storybook-preview #45276
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TextInput's textCount effect throws "Maximum update depth exceeded" (React #185) when input arrives at machine speed

4 participants