diff --git a/packages/react/react/src/components/Separated/Separated.tsx b/packages/react/react/src/components/Separated/Separated.tsx index 1f5adcbbb..d200df33f 100644 --- a/packages/react/react/src/components/Separated/Separated.tsx +++ b/packages/react/react/src/components/Separated/Separated.tsx @@ -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) { - const childrenArray = Children.toArray(children).filter(isValidElement); +export function Separated({ children, with: separator, first = false, last = false }: Props) { + const childrenArray = Children.toArray(children); const childrenLength = childrenArray.length; return ( <> - {childrenArray.map((child, i) => ( - + {first && separator} + {childrenArray.map((child, index) => ( + {child} - {i + 1 !== childrenLength ? separator : null} + {index + 1 !== childrenLength && separator} ))} + {last && separator} ); } diff --git a/packages/react/react/src/components/Separated/Seperated.en.md b/packages/react/react/src/components/Separated/Seperated.en.md index 1f1e5269c..cd65d0eb5 100644 --- a/packages/react/react/src/components/Separated/Seperated.en.md +++ b/packages/react/react/src/components/Separated/Seperated.en.md @@ -4,7 +4,21 @@ title: Separated # Separated -Used when +A convenient component used to insert a repeating component between each element. + +```tsx + + {children} + +``` + +## Example ```tsx }> diff --git a/packages/react/react/src/components/Separated/Seperated.ko.md b/packages/react/react/src/components/Separated/Seperated.ko.md index 541987012..5577af957 100644 --- a/packages/react/react/src/components/Separated/Seperated.ko.md +++ b/packages/react/react/src/components/Separated/Seperated.ko.md @@ -6,6 +6,20 @@ title: Separated 각 요소 사이에 반복되는 컴포넌트를 삽입하고자 할 때 사용할 수 있는 편리한 컴포넌트입니다. +```tsx + + {children} + +``` + +## Example + ```tsx }> {LIST.map(item => ( diff --git a/packages/react/react/src/components/Separated/index.spec.tsx b/packages/react/react/src/components/Separated/index.spec.tsx index 6fe1e86a1..cc281142e 100644 --- a/packages/react/react/src/components/Separated/index.spec.tsx +++ b/packages/react/react/src/components/Separated/index.spec.tsx @@ -34,4 +34,58 @@ describe('Separated', () => { expect(screen.queryByTestId(/separator/)).not.toBeInTheDocument(); }); + + it('should render separator at the beginning when first prop is true', () => { + const CHILDREN_COUNT = 10; + const separator = ; + const children = Array.from({ length: CHILDREN_COUNT }, (_, i) => ( +
+ container_{i} +
+ )); + + const { container } = render( + + {children} + + ); + + expect(container.firstChild).toHaveAttribute('data-testid', 'separator'); + expect(screen.getAllByText(/container/)).toHaveLength(CHILDREN_COUNT); + expect(screen.getAllByTestId(/separator/)).toHaveLength(CHILDREN_COUNT); + }); + + it('should render separator at the end when last prop is true', () => { + const CHILDREN_COUNT = 10; + const separator = ; + const children = Array.from({ length: CHILDREN_COUNT }, (_, i) => ( +
+ container_{i} +
+ )); + + const { container } = render( + + {children} + + ); + + expect(container.lastChild).toHaveAttribute('data-testid', 'separator'); + expect(screen.getAllByText(/container/)).toHaveLength(CHILDREN_COUNT); + expect(screen.getAllByTestId(/separator/)).toHaveLength(CHILDREN_COUNT); + }); + + it('should render all ReactNode types', () => { + const separator = ; + + render( + + string + {0} + string + + ); + + expect(screen.getAllByTestId(/separator/)).toHaveLength(2); + }); });