2020-08-25 16:43:52 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { LastIndexContext } from '.';
|
|
|
|
import { SizeType } from '../config-provider/SizeContext';
|
|
|
|
|
|
|
|
const spaceSize = {
|
|
|
|
small: 8,
|
|
|
|
middle: 16,
|
|
|
|
large: 24,
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface ItemProps {
|
|
|
|
className: string;
|
|
|
|
children: React.ReactNode;
|
|
|
|
index: number;
|
|
|
|
direction?: 'horizontal' | 'vertical';
|
|
|
|
size?: SizeType | number;
|
|
|
|
marginDirection: 'marginLeft' | 'marginRight';
|
2020-10-08 18:41:40 +08:00
|
|
|
split?: string | React.ReactNode;
|
2020-08-25 16:43:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function Item({
|
|
|
|
className,
|
|
|
|
direction,
|
|
|
|
index,
|
|
|
|
size,
|
|
|
|
marginDirection,
|
|
|
|
children,
|
2020-10-08 18:41:40 +08:00
|
|
|
split,
|
2020-08-25 16:43:52 +08:00
|
|
|
}: ItemProps) {
|
|
|
|
const latestIndex = React.useContext(LastIndexContext);
|
|
|
|
|
|
|
|
if (children === null || children === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:41:40 +08:00
|
|
|
const style =
|
|
|
|
index >= latestIndex
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
[direction === 'vertical' ? 'marginBottom' : marginDirection]:
|
|
|
|
((typeof size === 'string' ? spaceSize[size] : size) ?? 0) / (split ? 2 : 1),
|
|
|
|
};
|
|
|
|
|
2020-08-25 16:43:52 +08:00
|
|
|
return (
|
2020-10-08 18:41:40 +08:00
|
|
|
<>
|
|
|
|
<div className={className} style={style}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
{index < latestIndex && split && (
|
|
|
|
<span className={`${className}-split`} style={style}>
|
|
|
|
{split}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</>
|
2020-08-25 16:43:52 +08:00
|
|
|
);
|
|
|
|
}
|