Skip to content

Commit

Permalink
fix: allow modal to be wrapped in a React.Fragment (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leiksa committed Jan 5, 2023
1 parent 5bb7479 commit 5c27b72
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ const ModalComponent = forwardRef<'div', ModalProps>(
const contentScrollHeight = contentRef?.current?.scrollHeight

const components = useMemo(
() => Children.map(children, child => child?.type?.displayName || child?.type?.name),
() =>
Children.map(children, child => {
if (child.type === React.Fragment) {
return Children.map(
child.props.children,
_child => _child?.type?.displayName ?? _child?.type?.name
)
}
return child?.type?.displayName ?? child?.type?.name
}),
[children]
)

Expand Down Expand Up @@ -129,6 +138,18 @@ const ModalComponent = forwardRef<'div', ModalProps>(
<CloseElement onClick={closeModal} />
{Children.map(children, (child: JSX.Element) => {
if (!child) return null
if (child.type === React.Fragment) {
return Children.map(child.props.children, _child => {
const name = _child?.type?.displayName || _child?.type?.name

return cloneElement(_child, {
ref: setRef(name),
...getStyles(name),
..._child.props,
})
})
}

const name = child?.type?.displayName || child?.type?.name

return cloneElement(child, {
Expand Down

0 comments on commit 5c27b72

Please sign in to comment.