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
Show file tree
Hide file tree
Changes from 14 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
58 changes: 37 additions & 21 deletions packages/react/src/components/UIShell/Switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ const Switcher = forwardRef<HTMLUListElement, SwitcherProps>(function Switcher(
currentIndex: number;
direction: number;
}) => {
const enabledIndices = React.Children.toArray(children).reduce<number[]>(
const flattenedChildren = React.Children.toArray(children).flatMap(
(child) =>
React.isValidElement(child) && child.type === React.Fragment
? React.Children.toArray(child.props.children)
: child
);
const enabledIndices = flattenedChildren.reduce<number[]>(
(acc, curr, i) => {
if (Object.keys((curr as any).props).length !== 0) {
acc.push(i);
Expand Down Expand Up @@ -109,30 +115,40 @@ const Switcher = forwardRef<HTMLUListElement, SwitcherProps>(function Switcher(
}
};

const childrenWithProps = React.Children.toArray(children).map(
const childrenWithProps = React.Children.toArray(children).flatMap(
(child, index) => {
// only setup click handlers if onChange event is passed
if (
React.isValidElement(child) &&
child.type &&
getDisplayName(child.type) === 'Switcher'
) {
return React.cloneElement(child as React.ReactElement<any>, {
handleSwitcherItemFocus,
index,
key: index,
expanded,
});
if (React.isValidElement(child)) {
if (child.type === React.Fragment) {
// Handle React.Fragment case
return React.Children.toArray(child.props.children).map(
(fragmentChild, fragmentIndex) =>
React.isValidElement(fragmentChild)
? React.cloneElement(fragmentChild as React.ReactElement<any>, {
handleSwitcherItemFocus,
index: `${index}-${fragmentIndex}`,
key: `${index}-${fragmentIndex}`,
expanded,
})
: fragmentChild
);
} else if (getDisplayName(child.type) === 'Switcher') {
return React.cloneElement(child as React.ReactElement<any>, {
handleSwitcherItemFocus,
index,
key: index,
expanded,
});
} else {
return React.cloneElement(child as React.ReactElement<any>, {
index,
key: index,
expanded,
});
}
}

return React.cloneElement(child as React.ReactElement<any>, {
index,
key: index,
expanded,
});
return child;
}
);

return (
<ul
ref={ref as React.RefObject<HTMLUListElement>}
Expand Down
42 changes: 22 additions & 20 deletions packages/react/src/components/UIShell/UIShell.HeaderBase.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,26 +570,28 @@ export const HeaderWActionsAndSwitcher = (args) => (
<Switcher
aria-label="Switcher Container"
expanded={isSideNavExpanded}>
<SwitcherItem aria-label="Link 1" href="#">
Link 1
</SwitcherItem>
<SwitcherDivider />
<SwitcherItem href="#" aria-label="Link 2">
Link 2
</SwitcherItem>
<SwitcherItem href="#" aria-label="Link 3">
Link 3
</SwitcherItem>
<SwitcherItem href="#" aria-label="Link 4">
Link 4
</SwitcherItem>
<SwitcherItem href="#" aria-label="Link 5">
Link 5
</SwitcherItem>
<SwitcherDivider />
<SwitcherItem href="#" aria-label="Link 6">
Link 6
</SwitcherItem>
<>
<SwitcherItem aria-label="Link 1" href="#">
Link 1
</SwitcherItem>
<SwitcherDivider />
<SwitcherItem href="#" aria-label="Link 2">
Link 2
</SwitcherItem>
<SwitcherItem href="#" aria-label="Link 3">
Link 3
</SwitcherItem>
<SwitcherItem href="#" aria-label="Link 4">
Link 4
</SwitcherItem>
<SwitcherItem href="#" aria-label="Link 5">
Link 5
</SwitcherItem>
<SwitcherDivider />
<SwitcherItem href="#" aria-label="Link 6">
Link 6
</SwitcherItem>
</>
</Switcher>
</HeaderPanel>
</Header>
Expand Down
Loading