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(#15332) use toArray or checks for mapping children #15333

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ export default class ContentSwitcher extends React.Component<
role="tablist"
onChange={undefined}>
{children &&
React.Children.map(children, (child: ReactElement, index) =>
React.cloneElement(child, {
React.Children.toArray(children).map((child, index) =>
React.cloneElement(child as ReactElement, {
index,
onClick: composeEventHandlers([
this.handleChildChange,
child.props.onClick,
(child as ReactElement).props.onClick,
]),
onKeyDown: this.handleChildChange,
selected: index === this.state.selectedIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function ProgressIndicator({
return (
<ul className={className} {...rest}>
{React.Children.map(children, (child, index) => {
if (!React.isValidElement(child)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This could be written in a few different ways; but I wanted to make as minimal impact; since bottom of this loop returns null I am just adding this one check for valid Element here instead of on each of the index checks below.

return null;
}

// only setup click handlers if onChange event is passed
const onClick = onChange ? () => onChange(index) : undefined;
if (index === currentIndex) {
Expand Down
32 changes: 17 additions & 15 deletions packages/react/src/components/UIShell/Switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,29 @@ const Switcher = forwardRef<HTMLUListElement, SwitcherProps>(function Switcher(
}
};

const childrenWithProps = React.Children.map(children, (child, index) => {
// only setup click handlers if onChange event is passed
if (
React.isValidElement(child) &&
child.type &&
getDisplayName(child.type) === 'Switcher'
) {
const childrenWithProps = React.Children.toArray(children).map(
(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,
});
}

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

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

return (
<ul
Expand Down