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: allow modal to be wrapped in a React.Fragment #1826

Merged
merged 1 commit into from
Jan 5, 2023
Merged
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
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