Skip to content

Commit

Permalink
feat: add first, last props to allow separator at the beginning and end
Browse files Browse the repository at this point in the history
  • Loading branch information
metacode22 committed Sep 1, 2024
1 parent 1e903e0 commit dc41563
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/react/react/src/components/Separated/Separated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import { Children, Fragment, isValidElement, PropsWithChildren, ReactNode } from

interface Props extends PropsWithChildren {
with: ReactNode;
first?: boolean;
last?: boolean;
}

export function Separated({ children, with: separator }: Props) {
export function Separated({ children, with: separator, first = false, last = false }: Props) {
const childrenArray = Children.toArray(children).filter(isValidElement);
const childrenLength = childrenArray.length;

return (
<>
{first && separator}
{childrenArray.map((child, i) => (
<Fragment key={i}>
{child}
{i + 1 !== childrenLength ? separator : null}
</Fragment>
))}
{last && separator}
</>
);
}

0 comments on commit dc41563

Please sign in to comment.