Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {
Children,
type ComponentProps,
type FC,
Fragment,
type PropsWithChildren,
type ReactElement,
type ReactNode,
cloneElement,
isValidElement,
useMemo,
Expand Down Expand Up @@ -35,6 +37,38 @@ const classNameGenerator = tv({
base: 'smarthr-ui-DefinitionList shr-my-[initial]',
})

// children のうち DefinitionListItem にだけ maxColumns / termStyleType を注入して返す
function childrenToItems(
children: ReactNode,
extra: Pick<ItemType, 'maxColumns' | 'termStyleType'>,
): ReactNode[] {
const out: ReactNode[] = []

const walk = (nodes: ReactNode) => {
Children.forEach(nodes, (child) => {
if (!isValidElement(child)) {
out.push(child)
return
}

if (child.type === Fragment) {
walk(child.props.children) // Fragment は再帰的に展開
return
}

// DefinitionListItem にだけ追加 props を注入
if (child.type === DefinitionListItem) {
out.push(cloneElement(child, extra))
} else {
out.push(child) // 他の要素はそのまま
}
})
}

walk(children)
return out
}

export const DefinitionList: FC<Props & ElementProps> = ({
items,
maxColumns,
Expand All @@ -55,15 +89,7 @@ export const DefinitionList: FC<Props & ElementProps> = ({
termStyleType={termStyleType}
/>
))}
{Children.map(
children,
(child) =>
isValidElement(child) &&
cloneElement(child as ReactElement, {
maxColumns,
termStyleType,
}),
)}
{childrenToItems(children, { maxColumns, termStyleType })}
</Cluster>
)
}
Loading