forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:carbon-design-system/carbon
- Loading branch information
Showing
4 changed files
with
69 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
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. |
This file contains 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
This file contains 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
This file contains 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