Skip to content

[tabs] Fix selecting tabs with function values - #5886

Merged
michaldudak merged 1 commit into
mui:masterfrom
michaldudak:claude/fix-tabs-function-values
Oct 6, 2026
Merged

michaldudak merged 1 commit into
mui:masterfrom
michaldudak:claude/fix-tabs-function-values

Conversation

@michaldudak

Copy link
Copy Markdown
Member

Tab values can be any type (TabsTab.Value is any), but a tab whose value is a function can't be selected.

TabsRoot passes the new value straight to the useControlled setter, and React treats a function passed to a state setter as an updater. Clicking a tab with value={() => 3} stores 3, which matches no tab, so selection falls back to the first tab. A function defaultValue has the same problem, because useControlled passes default to React.useState, which calls it as a lazy initializer.

The fix:

  • TabsRoot wraps values in updaters (setValue(() => newValue)) both for user changes and for automatic fallbacks.
  • useControlled initializes its state lazily (useState(() => defaultProp)), so a function default is stored rather than called.

Why the existing test didn't catch it

should support values of different types already includes () => 3, but it ran its assertions inside Promise.allSettled, which swallows every rejection, so the test could never fail. Five of its six assertions were already failing. It's now a sequential loop that also checks aria-selected.

Validation

  • New and rewritten tests fail without the source change (3 failures) and pass with it.
  • TabsRoot and useControlled suites pass in jsdom and Chromium. ESLint and Prettier pass.

Found during an audit of the test suite for tests that can't fail.

Tab values may be any type, but a function value was passed straight to the
`useControlled` setter, which React treats as a state updater. Selecting such a
tab selected whatever the function returned (and fell back to the first tab).
A function `defaultValue` was likewise called as a lazy initializer.

Wrap values in updaters in TabsRoot and initialize useControlled lazily.

The existing 'should support values of different types' test could never
fail: its assertions ran inside Promise.allSettled, which swallowed every
rejection. Rewrite it as a sequential loop.
@michaldudak michaldudak added component: tabs Changes related to the tabs component. type: bug It doesn't behave as expected. labels Oct 5, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 5, 2026

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5886
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5886
    

commit: 0197ca5

@code-infra-dashboard

code-infra-dashboard Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+12B(0.00%) 🔺+6B(0.00%)

Details of bundle changes

Performance

Total duration: 509.40 ms +40.92 ms(+8.7%) | Renders: 76 (+0) | Paint: 932.43 ms +63.20 ms(+7.3%)

Test Duration Renders
Slider mount (300 instances) 70.78 ms 🔺+19.72 ms(+38.6%) 2 (+0)
Checkbox mount (500 instances) 44.82 ms 🔺+14.44 ms(+47.5%) 1 (+0)

13 tests within noise — details

Metric alarms

Test Metric Change
Slider mount (300 instances) bench:paint 🔺 +33.89 ms
Checkbox mount (500 instances) bench:paint 🔺 +21.40 ms

Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Oct 5, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0197ca5
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ac3a5816d74500009b85882
😎 Deploy Preview https://deploy-preview-5886--base-ui.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.

@michaldudak
michaldudak merged commit f705ed9 into mui:master Oct 6, 2026
25 checks passed
@michaldudak
michaldudak deleted the claude/fix-tabs-function-values branch October 6, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: tabs Changes related to the tabs component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants