Skip to content

[toggle group] Type value as string to match Toggle - #3770

Merged
michaldudak merged 4 commits into
mui:masterfrom
markocupic024:toggle-group-any-type
Jan 16, 2026
Merged

michaldudak merged 4 commits into
mui:masterfrom
markocupic024:toggle-group-any-type

Conversation

@markocupic024

@markocupic024 markocupic024 commented Jan 16, 2026 •

Copy link
Copy Markdown
Contributor

Solves #3756

Toggle only accepts string as type for its values.

@pkg-pr-new

pkg-pr-new Bot commented Jan 16, 2026 •

Copy link
Copy Markdown
  • vite-css-base-ui-example

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

commit: 267cbfc

@mui-bot

mui-bot commented Jan 16, 2026 •

Copy link
Copy Markdown

Bundle size report

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

Details of bundle changes


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

@greptile-apps

greptile-apps Bot commented Jan 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Changed ToggleGroup value types from any[] to string[] to match Toggle's string value type, resolving type safety issue reported in #3756.

Key changes:

  • Updated value, defaultValue, and onValueChange parameter types from any[] to string[] in ToggleGroup props
  • Changed ToggleGroupContext.value type from readonly any[] to readonly string[]
  • Added undefined check in Toggle.tsx before calling indexOf to handle the stricter typing (value !== undefined && groupValue.indexOf(value) > -1)
  • Updated generated documentation to reflect the new types

This change improves type safety and developer experience by preventing invalid non-string values from being passed to the toggle group while maintaining full backward compatibility since Toggle already only accepted string values.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are straightforward type refinements from any[] to string[] that align with existing behavior - Toggle already only accepts string values. The undefined check addition prevents potential runtime issues. All changes are backwards compatible since any code using string values (the only valid usage) continues to work.
  • No files require special attention

Important Files Changed

Filename Overview
packages/react/src/toggle-group/ToggleGroup.tsx Changed any[] to string[] for value types in props and internal state, improving type safety
packages/react/src/toggle-group/ToggleGroupContext.ts Updated context value type from any[] to string[] to match Toggle's string value type
packages/react/src/toggle/Toggle.tsx Added undefined check before indexOf to prevent runtime errors with typed arrays

Sequence Diagram

sequenceDiagram
    participant User
    participant ToggleGroup
    participant Toggle
    participant Context as ToggleGroupContext
    
    User->>ToggleGroup: Provide value: string[]
    ToggleGroup->>ToggleGroup: useControlled(value)
    ToggleGroup->>Context: Store value: readonly string[]
    Context-->>Toggle: Provide groupValue: readonly string[]
    Toggle->>Toggle: Check value !== undefined && groupValue.indexOf(value) > -1
    Toggle->>Toggle: Set pressed state (boolean)
    
    User->>Toggle: Click toggle button
    Toggle->>Toggle: Calculate nextPressed
    Toggle->>ToggleGroup: Call setGroupValue(value: string, nextPressed, eventDetails)
    ToggleGroup->>ToggleGroup: Update newGroupValue: string[]
    ToggleGroup->>User: onValueChange(newGroupValue: string[], eventDetails)
    ToggleGroup->>ToggleGroup: setValueState(newGroupValue)
    ToggleGroup->>Context: Update value: readonly string[]
    Context-->>Toggle: Re-render with new groupValue
Loading

@greptile-apps

greptile-apps Bot commented Jan 16, 2026

Copy link
Copy Markdown
Contributor

Greptile found no issues!

From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section.

This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR".

@netlify

netlify Bot commented Jan 16, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 267cbfc
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/696a31e5a530a70008abcbe7
😎 Deploy Preview https://deploy-preview-3770--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@michaldudak michaldudak added typescript component: toggle group Changes related to the toggle group component. labels Jan 16, 2026
@michaldudak

Copy link
Copy Markdown
Member

Could you please run pnpm prettier on it?


It's worth noting that while technically it's a breaking change, any working code should still work, as it's not possible to provide anything other than a string to the Toggle.

@michaldudak michaldudak changed the title [ToggleGroup] Type value as string to match Toggle [toggle group] Type value as string to match Toggle Jan 16, 2026

@michaldudak michaldudak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All right, this looks good! Thanks for your contribution, Marko!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: toggle group Changes related to the toggle group component. typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants