Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(16916): adds React fragment support for Switcher's direct child #17008

Merged
Merged
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
041693e
fix(15337): fix release names in update automation
2nikhiltom Jun 17, 2024
275eb71
fix(15337): fix release names in update automation
2nikhiltom Jun 17, 2024
6def269
Merge branch 'carbon-design-system:main' into main
2nikhiltom Jun 20, 2024
ff5082b
Merge branch 'carbon-design-system:main' into main
2nikhiltom Jun 28, 2024
ee20e94
Merge branch 'main' of https://github.com/2nikhiltom/carbon
2nikhiltom Jun 28, 2024
216186e
Merge branch 'main' of https://github.com/carbon-design-system/carbon
2nikhiltom Jul 1, 2024
e53455e
Merge branch 'main' of https://github.com/carbon-design-system/carbon
2nikhiltom Jul 4, 2024
3b2d11c
Merge branch 'carbon-design-system:main' into main
2nikhiltom Jul 4, 2024
ffe7332
Merge branch 'main' of https://github.com/2nikhiltom/carbon
2nikhiltom Jul 4, 2024
2026dd4
Merge branch 'carbon-design-system:main' into main
2nikhiltom Jul 18, 2024
e975b64
Merge branch 'main' of https://github.com/2nikhiltom/carbon
2nikhiltom Jul 18, 2024
9600b56
Merge branch 'main' of https://github.com/2nikhiltom/carbon
2nikhiltom Jul 22, 2024
3e1d6f2
fix(16916): added React fragment support for Switcher child
2nikhiltom Jul 22, 2024
2ef379c
Merge branch 'main' into fix_16916_switcher_fragment
preetibansalui Jul 23, 2024
f6952f8
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Aug 13, 2024
390d745
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Aug 19, 2024
b3a7b20
docs: revert changes & adds usage docs for Switcher
2nikhiltom Aug 19, 2024
93d1479
Format fix
2nikhiltom Aug 19, 2024
b7b55d0
Update UsingFragmentsWithSwitcher.md
2nikhiltom Aug 21, 2024
d8a22c5
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Sep 4, 2024
bd2ddbc
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Sep 5, 2024
6a7e225
Merge branch 'main' into fix_16916_switcher_fragment
preetibansalui Sep 6, 2024
ffae373
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Sep 9, 2024
2dee859
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Sep 11, 2024
c9536c8
chore: format fix
2nikhiltom Sep 11, 2024
4f3ae85
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Sep 12, 2024
957ba1f
Merge branch 'main' into fix_16916_switcher_fragment
2nikhiltom Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/react/docs/UsingFragmentsWithSwitcher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
The `Switcher` component is designed to have `SwitcherItem` components as its
direct children. However, there may be cases where you want to use React
Fragments or other nested structures within the `Switcher`. To accommodate we
recommend using the [`react-keyed-flatten-children`]
(https://www.npmjs.com/package/react-keyed-flatten-children#react-keyed-flatten-children)
package.

### Using react-keyed-flatten-children

The `react-keyed-flatten-children` package allows you to flatten arrays and
React Fragments into a regular, one-dimensional array while preserving element
and fragment keys.

1. Install the package:

```
npm install react-keyed-flatten-children
```

2. Import and use in your component:

```jsx
import flattenChildren from 'react-keyed-flatten-children';

const YourComponent = () => (
<Switcher>
{flattenChildren(
<>
<SwitcherItem>Item 1</SwitcherItem>
<SwitcherItem>Item 2</SwitcherItem>
<>
<SwitcherItem>Item 3</SwitcherItem>
<SwitcherItem>Item 4</SwitcherItem>
</>
</>
)}
</Switcher>
);
```

This approach allows you to use Fragments and nested structures with components
like `<Switcher>` without modifying their source code. It preserves keys and
props, ensuring stable rendering across updates.
Loading