Skip to content
Open
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Children,
type ComponentProps,
type FC,
Fragment,
type PropsWithChildren,
type ReactElement,
cloneElement,
Expand Down Expand Up @@ -55,15 +56,21 @@ export const DefinitionList: FC<Props & ElementProps> = ({
termStyleType={termStyleType}
/>
))}
{Children.map(
children,
(child) =>
isValidElement(child) &&
cloneElement(child as ReactElement, {
maxColumns,
termStyleType,
}),
)}
{Children.toArray(children)
.flatMap((child) => {
if (isValidElement(child) && child.type === Fragment) {
return Children.toArray(child.props.children)
}
return child
})
.map((child) =>
isValidElement(child)
? cloneElement(child as ReactElement, {
maxColumns,
termStyleType,
})
: child,
)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flatMap -> mapで配列を2回転している & 内容でisValidElementのチェックが2重でかかっている & Fragmentが複数ネストしている場合に対応できていないので以下の感じはどうっすかね?
(動作確認自体は未対応ですが...)

const childrenToItems = (children: ReactNode) => (
  Children.toArray(children)
    .reduce((prev, child) => {
      if (isValidElement(child)) {
        if (child.type === Fragment) {
          prev = prev.concat(childrenToItems(child.props.children))
        }

        prev.push(cloneElement(child as ReactElement, {
          maxColumns,
          termStyleType,
        }))
      } else {
        prev.push(child)
      }

      return prev
    }, [] as ReactElement)
)

...

{childrenToItems(children)}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AtsushiM
2回転とisValidElementの二重チェックを避けつつもう少し厳格にしました!
e1110ef

</Cluster>
)
}
Loading