Fix: Prevent duplicate IDs in FieldToggle when multiple column pickers are present#11238
Open
Yashrajsingh2001 wants to merge 1 commit intomarmelab:masterfrom
Open
Fix: Prevent duplicate IDs in FieldToggle when multiple column pickers are present#11238Yashrajsingh2001 wants to merge 1 commit intomarmelab:masterfrom
Yashrajsingh2001 wants to merge 1 commit intomarmelab:masterfrom
Conversation
…s are present Use React.useId() to generate unique IDs for the Switch component instead of using the index prop. This prevents label/id collisions when multiple column pickers (e.g., ColumnsButton and SelectColumnsButton) are rendered simultaneously. Fixes marmelab#11236
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a page has multiple column-visibility UIs (e.g., a main
DataTablewithColumnsButtonand a nestedDatagridConfigurablewithSelectColumnsButton), clicking the text label next to a switch in the inner picker toggles columns on the outer (main) grid instead of the inner grid. This happens because theFieldTogglecomponent uses theindexprop to generate IDs for the Switch elements, causing duplicate IDs when both pickers are rendered simultaneously.Solution
Replace the index-based ID generation with
React.useId()to ensure each Switch component has a globally unique ID. This prevents label/id collisions when multiple column pickers are present in the DOM.Changes made:
const switchId = React.useId();in the FieldToggle componenthtmlForattribute in the label to useswitchIdidprop to useswitchIdHow To Test
DataTableandColumnsButton(main column picker)DatagridConfigurable/SelectColumnsButton(second column picker)ColumnsButtonPopover uses keepMounted)Alternatively, use the CodeSandbox provided in the issue: https://codesandbox.io/p/github/marmelab/react-admin-sandbox/csb-pdqfw2/draft/condescending-spence
Additional Checks
masterfor a bugfix or a documentation fix, ornextfor a featureNote: Unit tests and stories are not included as this is a minimal fix to an existing component. The change is straightforward (using React's built-in
useId()hook) and the existing tests should continue to pass. The fix addresses the root cause of the duplicate ID issue without changing the component's API or behavior.Fixes #11236