Skip to content

Commit

Permalink
feat: Add support for multi-level nesting in bones layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Belov committed Dec 1, 2023
1 parent 2f42d50 commit c289f36
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions packages/npm/src/hooks/useGetBones.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,33 @@ interface UseGetBonesProps {
export const useGetBones = (componentSize: IComponentSize) => {
const renderBone = useRenderBone(componentSize);

return useCallback(
({ bonesLayout, children, prefix, generalStyles }: UseGetBonesProps) => {
if (bonesLayout && bonesLayout.length > 0) {
const iterator: number[] = new Array(bonesLayout.length).fill(0);
const renderNestedBones = (bones: ICustomViewStyle[], prefix: string | number | undefined, generalStyles: IGeneralStyles) => {
return bones.map((bone, index) => {
const keyIndex = prefix ? `${prefix}_${index}` : index;

return iterator.map((_, i) => {
/* NOTE: Has nested layout with children */
if (bonesLayout[i]?.children?.length) {
const containerPrefix =
bonesLayout[i]?.key || `bone_container_${i}`;
const { children: childBones, ...layoutStyle } =
bonesLayout[i] ?? {};
const { children: childBones, ...layoutStyle } = bone;

return (
<View key={containerPrefix} style={layoutStyle}>
{childBones?.map((__, childIndex) =>
renderBone({
generalStyles,
bonesLayout: childBones,
index: childIndex,
keyIndex: prefix ? `${prefix}_${childIndex}` : childIndex,
}),
)}
</View>
);
}
if (childBones?.length) {
return (
<View key={keyIndex} style={layoutStyle}>
{renderNestedBones(childBones, keyIndex, generalStyles)}
</View>
);
}

return renderBone({
generalStyles,
bonesLayout,
index: i,
keyIndex: prefix ? `${prefix}_${i}` : i,
});
});
return renderBone({
generalStyles,
bonesLayout: bones,
index,
keyIndex,
});
});
};

return useCallback(
({ bonesLayout, children, prefix, generalStyles }: UseGetBonesProps) => {
if (bonesLayout && bonesLayout.length > 0) {
return renderNestedBones(bonesLayout, prefix, generalStyles);
}

return Children.map(children as JSX.Element[], (child, i) => {
Expand Down

0 comments on commit c289f36

Please sign in to comment.