Skip to content

Commit e92acb3

Browse files
committed
feat: update Breadcrumb and ButtonGroup components to handle null children and improve child rendering
1 parent aaa15cb commit e92acb3

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/components/breadcrumb/Breadcrumb.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ export const Breadcrumb: React.FC<BreadcrumbProps> = props => {
1212

1313
const {splitter = <IconChevronRight size={16}/>, children, ...rest} = props
1414

15-
const count = React.Children.count(children)
15+
const validChildren = React.Children.toArray(children).filter(child => child != null)
16+
const count = validChildren.length
1617

1718
return (
1819
<div {...mergeComponentProps(`breadcrumb`, rest)}>
19-
{React.Children.map(children, (child, index) => (
20+
{validChildren.map((child, index) => (
2021
<React.Fragment key={index}>
2122
{child}
2223
{index < count - 1 ? (

src/components/button-group/ButtonGroup.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@ import "./ButtonGroup.style.scss"
44
import {Component, Color, mergeComponentProps} from "../../utils";
55

66
export interface ButtonGroupType extends Component<HTMLDivElement> {
7-
children: ReactElement<ButtonProps>[]
7+
children: ReactElement<ButtonProps> | ReactElement<ButtonProps>[]
88
color?: Color
99
}
1010

1111
export const ButtonGroup: React.FC<ButtonGroupType> = (props) => {
1212

1313
const {children, color = "secondary", ...args} = props
1414

15+
const validChildren = React.Children.toArray(children).filter(child => child != null)
16+
const count = validChildren.length
17+
1518
return <div {...mergeComponentProps(`button-group button-group--${color}`, args)}>
1619

17-
{children.map((child, i) => {
20+
{validChildren.map((child, i) => {
21+
22+
let className = "button-group__item"
23+
if (i === count - 1) {
24+
className = "button-group__last"
25+
} else if (i === 0) {
26+
className = "button-group__first"
27+
}
28+
1829
return <div
19-
key={child.key ?? `button-group-${i}`}
20-
className={`${i == 0 || i == children.length - 1 ? i == 0 ? "button-group__first" : "button-group__last" : "button-group__item"}`}>
30+
key={`button-group-${i}`}
31+
className={className}>
2132
{child}
2233
</div>
2334
})}

0 commit comments

Comments
 (0)