2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-06-05 13:29:55 +08:00
|
|
|
import { SpaceContext } from './context';
|
2020-08-25 16:43:52 +08:00
|
|
|
|
|
|
|
export interface ItemProps {
|
|
|
|
className: string;
|
|
|
|
children: React.ReactNode;
|
|
|
|
index: number;
|
|
|
|
direction?: 'horizontal' | 'vertical';
|
|
|
|
marginDirection: 'marginLeft' | 'marginRight';
|
2023-04-28 16:09:14 +08:00
|
|
|
split?: React.ReactNode;
|
2020-11-27 13:40:40 +08:00
|
|
|
wrap?: boolean;
|
2023-05-31 13:43:38 +08:00
|
|
|
style?: React.CSSProperties;
|
2020-08-25 16:43:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function Item({
|
|
|
|
className,
|
|
|
|
direction,
|
|
|
|
index,
|
|
|
|
marginDirection,
|
|
|
|
children,
|
2020-10-08 18:41:40 +08:00
|
|
|
split,
|
2020-11-27 13:40:40 +08:00
|
|
|
wrap,
|
2023-05-31 13:43:38 +08:00
|
|
|
style: customStyle,
|
2020-08-25 16:43:52 +08:00
|
|
|
}: ItemProps) {
|
2022-06-22 14:57:09 +08:00
|
|
|
const { horizontalSize, verticalSize, latestIndex, supportFlexGap } =
|
|
|
|
React.useContext(SpaceContext);
|
2020-11-27 13:40:40 +08:00
|
|
|
|
|
|
|
let style: React.CSSProperties = {};
|
|
|
|
|
2021-05-24 16:24:00 +08:00
|
|
|
if (!supportFlexGap) {
|
|
|
|
if (direction === 'vertical') {
|
|
|
|
if (index < latestIndex) {
|
|
|
|
style = { marginBottom: horizontalSize / (split ? 2 : 1) };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
style = {
|
|
|
|
...(index < latestIndex && { [marginDirection]: horizontalSize / (split ? 2 : 1) }),
|
|
|
|
...(wrap && { paddingBottom: verticalSize }),
|
|
|
|
};
|
2020-11-27 13:40:40 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 16:43:52 +08:00
|
|
|
|
|
|
|
if (children === null || children === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-10-08 18:41:40 +08:00
|
|
|
<>
|
2023-05-31 13:43:38 +08:00
|
|
|
<div className={className} style={{ ...style, ...customStyle }}>
|
2020-10-08 18:41:40 +08:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
{index < latestIndex && split && (
|
|
|
|
<span className={`${className}-split`} style={style}>
|
|
|
|
{split}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</>
|
2020-08-25 16:43:52 +08:00
|
|
|
);
|
|
|
|
}
|