Skip to content

Commit 9bb6004

Browse files
committed
Fix issue in SpacedList
1 parent edfb8e6 commit 9bb6004

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/common/ui/SpacedList.tsx

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
import React from "react"
22
import { List, Box, SxProps } from "@mui/material"
33

4-
const SpacedList = ({
5-
itemSpacing,
6-
sx,
7-
children
8-
}: {
4+
interface SpacedListProps {
95
itemSpacing: number
106
sx?: SxProps
117
children?: React.ReactNode
12-
}) => {
8+
}
9+
10+
const SpacedList = ({ itemSpacing, sx, children }: SpacedListProps) => {
11+
const childrenArray = React.Children.toArray(children)
12+
const lastIndex = childrenArray.length - 1
13+
1314
return (
14-
<List disablePadding sx={{ ...sx }}>
15-
{React.Children.map(children, (child, idx) => {
16-
const baseKey = (child as React.ReactElement)?.key ?? "idx";
17-
const key = `${String(baseKey)}-${idx}`;
18-
return (
19-
<Box
20-
key={key}
21-
sx={{
22-
marginBottom:
23-
idx < React.Children.count(children) - 1 ? itemSpacing : 0,
24-
}}
25-
>
26-
{child}
27-
</Box>
28-
);
29-
})}
15+
<List disablePadding sx={sx}>
16+
{childrenArray.map((child, idx) => (
17+
<Box
18+
key={idx}
19+
sx={{
20+
marginBottom: idx < lastIndex ? itemSpacing : 0
21+
}}
22+
>
23+
{child}
24+
</Box>
25+
))}
3026
</List>
3127
)
3228
}

0 commit comments

Comments
 (0)