Skip to content

fix(input)!: keep counter stepping in sync with the current value - #3266

Merged
interactivellama merged 2 commits into
salesforce:masterfrom
zigzagdev:fix/input-counter-stepping
Nov 2, 2026
Merged

interactivellama merged 2 commits into
salesforce:masterfrom
zigzagdev:fix/input-counter-stepping

Conversation

@zigzagdev

@zigzagdev zigzagdev commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Additional description

Fixes two bugs in the Input counter variant (variant="counter"), both regressions from the function-component rewrite:

  • Holding a stepper button now keeps counting in controlled mode. Before, it reported the same number on every repeat.
  • In uncontrolled mode, the increment and decrement buttons now become disabled when the value reaches maxValue or minValue. Before, they stayed enabled.

No props, types or exports change.

Motivation

Controlled: with a controlled value, holding the increment button called onChange with the same number on every repeat (1, 1, 1, …). The input never got past the first step.

Uncontrolled: with defaultValue="0" and maxValue={1}, clicking increment moved the input to 1 but left the increment button enabled. The same happened at minValue with the decrement button.

The pre-rewrite class avoided both. It read this.props.value on every step and called forceUpdate after writing to the input element.

Root cause and fix

  • Controlled: performStep schedules its own repeat with setTimeout. That callback called the performStep from the render where the button was pressed, and that closure captured the value prop from that render. Every repeat computed the next number from the original value. performStep is now stored in performStepRef on each render, and the timer calls performStepRef.current, so every repeat reads the current props.
  • Uncontrolled: a step writes the new value straight to the <input> element (inputElementRef.current.value = ...), and nothing triggered a render. The disabled state of the counter buttons is computed during render from that value, so it was never recomputed. The step now calls forceRender() (a useReducer counter) after the write.

Test Plans

  1. Run the Input unit tests with and without the fix. The two new tests should fail without it.
  2. Run typecheck and build.
  3. In Storybook, open an Input counter story with a controlled value. Hold the increment button for a few seconds. The value should keep rising and onChange should report a new number each time.
  4. For the uncontrolled case, use defaultValue="0" and maxValue={1}, click increment once, and confirm the increment button is disabled.

Test Results

New regression tests in components/input/__tests__/input.test.jsx:

  • keeps counting while a button is held in controlled mode (fake timers; asserts onChange reports ['1', '2', '3', '4'])
  • disables the increment button at maxValue in uncontrolled mode

[above text added by @interactivellama]

Screen Shots

before

2026-10-30.9.23.15.mov

after

2026-10-30.9.22.06.mov

CONTRIBUTOR checklist (do not remove)

Please complete for every pull request.

Green gate — all of these must pass locally (CI runs the same):

  • npm run lint passes with 0 errors (npm run lint:fix to auto-fix). Warnings are the tracked gradual-migration backlog; do not add new errors.
  • npm run typecheck is clean (tsc --noEmit).
  • npm test -- --run passes — this runs both Vitest projects (jsdom unit + real-browser browser) and the story HTML snapshots. If you changed rendered markup on purpose, update snapshots with npm test -- --run -u and review the diff.
  • npm run build succeeds (type-check + Vite library build + .d.ts + entry shims).

Behavior & docs:

  • First-time contributors: sign the CLA (a bot will comment with instructions).
  • Added/updated Vitest + React Testing Library tests for the change (template: components/button/__tests__). New props and bug fixes should get a regression test. Tests that genuinely need a real browser (focus traversal, layout/measurement, canvas) go in a *.browser.test.jsx file (the browser project); everything else is jsdom.
  • Reviewed the component's Storybook stories: npm run storybook → http://localhost:6007/. Confirm the story actually renders and behaves (deprecated Storyshots/Karma are gone; there is no separate browser-tests server).
  • Story/example code uses the component's real prop API (e.g. slots like content / visualPickerFigure, render props like renderItem, icon props, events), not assumptions — a common source of "component looks broken" reports has been stories authored against the wrong API. Cross-check against components/<name>/__examples__.
  • Storybook Accessibility (a11y) panel shows no new violations for the affected stories.
  • If you added or changed a type, remember runtime PropTypes were removed in the TS migration — types are the contract. Ship accurate .d.ts (they're generated by the build); JS consumers rely on them via editor type-acquisition.

REVIEWER checklist (do not remove)

  • CI is green (GitHub Actions CI workflow: lint, typecheck, test, build). Chromatic visual review, if configured, has been checked.
  • Change has adequate test coverage; bug fixes include a regression test.
  • Reviewed the affected Storybook stories and their a11y panels.
  • Rendered markup still conforms to SLDS — check the story HTML snapshot diff (components/__tests__/__snapshots__/story-snapshots.test.jsx.snap).
  • Public API / package surface: if exports changed, the deep-import contract still holds (@salesforce/design-system-react/components/<name> and nested paths resolve for ESM, CJS, and types). Removing/renaming a public component or export is a breaking change → feat!:/BREAKING CHANGE: and a major version bump.

Holding a counter button repeats performStep from a timer. The timer
called the performStep closure from the render where the button was
pressed, so in controlled mode it kept reading the old value prop and
reported the same number on every repeat. Call the latest performStep
through a ref instead, as the pre-rewrite class did by reading
this.props.value on each step.

In uncontrolled mode a step writes the new value straight to the input
element and nothing re-rendered, so the increment and decrement buttons
kept their old disabled state after reaching maxValue or minValue.
Re-render after the write, matching the forceUpdate call in the
pre-rewrite class.

Add regression tests for both cases.
@interactivellama

Copy link
Copy Markdown
Contributor

Videos match the bug, but the description does not. I believe the bugs being solved are:

  • Press-and-hold repeat uses a stale closure: onChange fires [6,6,6,…], the value sticks, and min/max stops go stale

@interactivellama
interactivellama merged commit 77e40ed into salesforce:master Nov 2, 2026
7 checks passed
@zigzagdev
zigzagdev deleted the fix/input-counter-stepping branch November 2, 2026 07:52
@zigzagdev

Copy link
Copy Markdown
Contributor Author

Thanks for the review and merge.

This behaviour was a stale closure repeating the first stepped value (e.g. 6, 6, 6…), not 1, 1, 1.

I'll be more careful to match the description to the observed behaviour next time.

Niznikr added a commit that referenced this pull request Nov 4, 2026
Conflicts came from #3268 (forwardRef, className on the root), #3266
(Input counter), #3270 (Carousel autoplay) and #3265 (MenuDropdown
open registration).

- Slider, VerticalNavigation, Popover, Tree: keep master's forwardRef,
  root className and classNameList/styleList, with the controlled
  value and selection from this branch.
- Input, Carousel, MenuDropdown: keep this branch's implementation,
  which already fixes the same stale closures through refs. Master's
  regression tests are kept; the uncontrolled Input maxValue test is
  rewritten for a controlled counter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants