2020-04-01 17:38:03 +08:00
|
|
|
import classNames from 'classnames';
|
2020-08-28 11:29:37 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-05-11 12:00:02 +08:00
|
|
|
import useFlexGapSupport from '../_util/hooks/useFlexGapSupport';
|
2020-10-08 18:41:40 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
2022-10-18 16:23:10 +08:00
|
|
|
import Compact from './Compact';
|
2023-01-20 11:03:50 +08:00
|
|
|
import Item from './Item';
|
2020-08-25 16:43:52 +08:00
|
|
|
|
2023-06-05 13:29:55 +08:00
|
|
|
import { SpaceContextProvider } from './context';
|
2022-05-04 22:00:39 +08:00
|
|
|
import useStyle from './style';
|
2020-08-25 16:43:52 +08:00
|
|
|
|
2023-06-05 13:29:55 +08:00
|
|
|
export type { SpaceContext } from './context';
|
2020-11-27 13:40:40 +08:00
|
|
|
|
|
|
|
export type SpaceSize = SizeType | number;
|
2020-03-22 11:38:02 +08:00
|
|
|
|
2021-05-19 17:44:13 +08:00
|
|
|
export interface SpaceProps extends React.HTMLAttributes<HTMLDivElement> {
|
2020-03-22 11:38:02 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2020-03-22 11:38:02 +08:00
|
|
|
style?: React.CSSProperties;
|
2020-11-27 13:40:40 +08:00
|
|
|
size?: SpaceSize | [SpaceSize, SpaceSize];
|
2020-03-22 11:38:02 +08:00
|
|
|
direction?: 'horizontal' | 'vertical';
|
2020-04-16 12:28:04 +08:00
|
|
|
// No `stretch` since many components do not support that.
|
|
|
|
align?: 'start' | 'end' | 'center' | 'baseline';
|
2020-10-08 18:41:40 +08:00
|
|
|
split?: React.ReactNode;
|
2020-11-27 13:40:40 +08:00
|
|
|
wrap?: boolean;
|
2023-05-31 13:43:38 +08:00
|
|
|
classNames?: { item: string };
|
|
|
|
styles?: { item: React.CSSProperties };
|
2020-11-27 13:40:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const spaceSize = {
|
|
|
|
small: 8,
|
|
|
|
middle: 16,
|
|
|
|
large: 24,
|
|
|
|
};
|
|
|
|
|
|
|
|
function getNumberSize(size: SpaceSize) {
|
|
|
|
return typeof size === 'string' ? spaceSize[size] : size || 0;
|
2020-03-22 11:38:02 +08:00
|
|
|
}
|
|
|
|
|
2023-05-11 12:00:02 +08:00
|
|
|
const Space = React.forwardRef<HTMLDivElement, SpaceProps>((props, ref) => {
|
2020-10-08 18:41:40 +08:00
|
|
|
const { getPrefixCls, space, direction: directionConfig } = React.useContext(ConfigContext);
|
2020-03-22 11:38:02 +08:00
|
|
|
|
|
|
|
const {
|
|
|
|
size = space?.size || 'small',
|
2020-04-16 12:28:04 +08:00
|
|
|
align,
|
2020-03-22 11:38:02 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-03-22 11:38:02 +08:00
|
|
|
children,
|
|
|
|
direction = 'horizontal',
|
|
|
|
prefixCls: customizePrefixCls,
|
2020-10-08 18:41:40 +08:00
|
|
|
split,
|
2020-11-27 13:40:40 +08:00
|
|
|
style,
|
|
|
|
wrap = false,
|
2023-05-31 13:43:38 +08:00
|
|
|
classNames: customClassNames,
|
|
|
|
styles,
|
2020-03-22 11:38:02 +08:00
|
|
|
...otherProps
|
|
|
|
} = props;
|
|
|
|
|
2021-05-24 16:24:00 +08:00
|
|
|
const supportFlexGap = useFlexGapSupport();
|
|
|
|
|
2020-11-27 13:40:40 +08:00
|
|
|
const [horizontalSize, verticalSize] = React.useMemo(
|
|
|
|
() =>
|
2022-11-19 13:47:33 +08:00
|
|
|
((Array.isArray(size) ? size : [size, size]) as [SpaceSize, SpaceSize]).map((item) =>
|
2020-11-27 13:40:40 +08:00
|
|
|
getNumberSize(item),
|
|
|
|
),
|
|
|
|
[size],
|
|
|
|
);
|
|
|
|
|
2020-08-28 11:29:37 +08:00
|
|
|
const childNodes = toArray(children, { keepEmpty: true });
|
2020-03-22 11:38:02 +08:00
|
|
|
|
2020-04-16 12:28:04 +08:00
|
|
|
const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
|
2020-03-22 11:38:02 +08:00
|
|
|
const prefixCls = getPrefixCls('space', customizePrefixCls);
|
2022-05-04 22:00:39 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2020-04-01 17:38:03 +08:00
|
|
|
const cn = classNames(
|
|
|
|
prefixCls,
|
2023-06-19 19:30:16 +08:00
|
|
|
space?.className,
|
2022-05-04 22:00:39 +08:00
|
|
|
hashId,
|
2020-04-01 17:38:03 +08:00
|
|
|
`${prefixCls}-${direction}`,
|
2020-04-16 12:28:04 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: directionConfig === 'rtl',
|
|
|
|
[`${prefixCls}-align-${mergedAlign}`]: mergedAlign,
|
|
|
|
},
|
2023-06-19 19:30:16 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-04-01 17:38:03 +08:00
|
|
|
);
|
2020-03-22 11:38:02 +08:00
|
|
|
|
2023-06-02 15:03:29 +08:00
|
|
|
const itemClassName = classNames(
|
|
|
|
`${prefixCls}-item`,
|
|
|
|
customClassNames?.item ?? space?.classNames?.item,
|
|
|
|
);
|
2020-03-22 11:38:02 +08:00
|
|
|
|
2020-04-01 17:38:03 +08:00
|
|
|
const marginDirection = directionConfig === 'rtl' ? 'marginLeft' : 'marginRight';
|
|
|
|
|
2020-08-25 16:43:52 +08:00
|
|
|
// Calculate latest one
|
|
|
|
let latestIndex = 0;
|
2020-08-28 11:29:37 +08:00
|
|
|
const nodes = childNodes.map((child, i) => {
|
2020-08-25 16:43:52 +08:00
|
|
|
if (child !== null && child !== undefined) {
|
|
|
|
latestIndex = i;
|
|
|
|
}
|
|
|
|
|
2022-05-06 12:03:15 +08:00
|
|
|
const key = (child && child.key) || `${itemClassName}-${i}`;
|
2022-01-10 18:11:06 +08:00
|
|
|
|
2020-08-25 16:43:52 +08:00
|
|
|
return (
|
|
|
|
<Item
|
|
|
|
className={itemClassName}
|
2022-05-05 13:46:52 +08:00
|
|
|
key={key}
|
2020-08-25 16:43:52 +08:00
|
|
|
direction={direction}
|
|
|
|
index={i}
|
|
|
|
marginDirection={marginDirection}
|
2020-10-08 18:41:40 +08:00
|
|
|
split={split}
|
2020-11-27 13:40:40 +08:00
|
|
|
wrap={wrap}
|
2023-06-02 15:03:29 +08:00
|
|
|
style={styles?.item ?? space?.styles?.item}
|
2020-08-25 16:43:52 +08:00
|
|
|
>
|
|
|
|
{child}
|
|
|
|
</Item>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-05-24 16:24:00 +08:00
|
|
|
const spaceContext = React.useMemo(
|
|
|
|
() => ({ horizontalSize, verticalSize, latestIndex, supportFlexGap }),
|
|
|
|
[horizontalSize, verticalSize, latestIndex, supportFlexGap],
|
|
|
|
);
|
|
|
|
|
|
|
|
// =========================== Render ===========================
|
|
|
|
if (childNodes.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gapStyle: React.CSSProperties = {};
|
2021-06-16 13:34:22 +08:00
|
|
|
|
|
|
|
if (wrap) {
|
|
|
|
gapStyle.flexWrap = 'wrap';
|
|
|
|
|
|
|
|
// Patch for gap not support
|
|
|
|
if (!supportFlexGap) {
|
|
|
|
gapStyle.marginBottom = -verticalSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:24:00 +08:00
|
|
|
if (supportFlexGap) {
|
|
|
|
gapStyle.columnGap = horizontalSize;
|
|
|
|
gapStyle.rowGap = verticalSize;
|
|
|
|
}
|
|
|
|
|
2022-05-04 22:00:39 +08:00
|
|
|
return wrapSSR(
|
2020-11-27 13:40:40 +08:00
|
|
|
<div
|
2023-05-11 12:00:02 +08:00
|
|
|
ref={ref}
|
2020-11-27 13:40:40 +08:00
|
|
|
className={cn}
|
|
|
|
style={{
|
2021-05-24 16:24:00 +08:00
|
|
|
...gapStyle,
|
2023-06-02 15:03:29 +08:00
|
|
|
...space?.style,
|
2020-11-27 13:40:40 +08:00
|
|
|
...style,
|
|
|
|
}}
|
|
|
|
{...otherProps}
|
|
|
|
>
|
2023-06-05 13:29:55 +08:00
|
|
|
<SpaceContextProvider value={spaceContext}>{nodes}</SpaceContextProvider>
|
2022-05-04 22:00:39 +08:00
|
|
|
</div>,
|
2020-03-22 11:38:02 +08:00
|
|
|
);
|
2023-05-11 12:00:02 +08:00
|
|
|
});
|
2020-03-22 11:38:02 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Space.displayName = 'Space';
|
|
|
|
}
|
|
|
|
|
2023-05-11 12:00:02 +08:00
|
|
|
type CompoundedComponent = React.ForwardRefExoticComponent<
|
|
|
|
SpaceProps & React.RefAttributes<HTMLDivElement>
|
|
|
|
> & {
|
2022-10-18 16:23:10 +08:00
|
|
|
Compact: typeof Compact;
|
2022-11-19 16:56:23 +08:00
|
|
|
};
|
2022-10-18 16:23:10 +08:00
|
|
|
|
|
|
|
const CompoundedSpace = Space as CompoundedComponent;
|
2023-01-08 21:30:41 +08:00
|
|
|
|
2022-10-18 16:23:10 +08:00
|
|
|
CompoundedSpace.Compact = Compact;
|
|
|
|
|
|
|
|
export default CompoundedSpace;
|