UNPKG

4.57 kBJavaScriptView Raw
1"use client";
2
3var __rest = this && this.__rest || function (s, e) {
4 var t = {};
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
6 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
8 }
9 return t;
10};
11import * as React from 'react';
12import classNames from 'classnames';
13import toArray from "rc-util/es/Children/toArray";
14import { isPresetSize, isValidGapNumber } from '../_util/gapSize';
15import { ConfigContext } from '../config-provider';
16import Compact from './Compact';
17import { SpaceContextProvider } from './context';
18import Item from './Item';
19import useStyle from './style';
20export { SpaceContext } from './context';
21const Space = /*#__PURE__*/React.forwardRef((props, ref) => {
22 var _a, _b;
23 const {
24 getPrefixCls,
25 space,
26 direction: directionConfig
27 } = React.useContext(ConfigContext);
28 const {
29 size = (space === null || space === void 0 ? void 0 : space.size) || 'small',
30 align,
31 className,
32 rootClassName,
33 children,
34 direction = 'horizontal',
35 prefixCls: customizePrefixCls,
36 split,
37 style,
38 wrap = false,
39 classNames: customClassNames,
40 styles
41 } = props,
42 otherProps = __rest(props, ["size", "align", "className", "rootClassName", "children", "direction", "prefixCls", "split", "style", "wrap", "classNames", "styles"]);
43 const [horizontalSize, verticalSize] = Array.isArray(size) ? size : [size, size];
44 const isPresetVerticalSize = isPresetSize(verticalSize);
45 const isPresetHorizontalSize = isPresetSize(horizontalSize);
46 const isValidVerticalSize = isValidGapNumber(verticalSize);
47 const isValidHorizontalSize = isValidGapNumber(horizontalSize);
48 const childNodes = toArray(children, {
49 keepEmpty: true
50 });
51 const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
52 const prefixCls = getPrefixCls('space', customizePrefixCls);
53 const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
54 const cls = classNames(prefixCls, space === null || space === void 0 ? void 0 : space.className, hashId, `${prefixCls}-${direction}`, {
55 [`${prefixCls}-rtl`]: directionConfig === 'rtl',
56 [`${prefixCls}-align-${mergedAlign}`]: mergedAlign,
57 [`${prefixCls}-gap-row-${verticalSize}`]: isPresetVerticalSize,
58 [`${prefixCls}-gap-col-${horizontalSize}`]: isPresetHorizontalSize
59 }, className, rootClassName, cssVarCls);
60 const itemClassName = classNames(`${prefixCls}-item`, (_a = customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.item) !== null && _a !== void 0 ? _a : (_b = space === null || space === void 0 ? void 0 : space.classNames) === null || _b === void 0 ? void 0 : _b.item);
61 // Calculate latest one
62 let latestIndex = 0;
63 const nodes = childNodes.map((child, i) => {
64 var _a, _b;
65 if (child !== null && child !== undefined) {
66 latestIndex = i;
67 }
68 const key = child && child.key || `${itemClassName}-${i}`;
69 return /*#__PURE__*/React.createElement(Item, {
70 className: itemClassName,
71 key: key,
72 index: i,
73 split: split,
74 style: (_a = styles === null || styles === void 0 ? void 0 : styles.item) !== null && _a !== void 0 ? _a : (_b = space === null || space === void 0 ? void 0 : space.styles) === null || _b === void 0 ? void 0 : _b.item
75 }, child);
76 });
77 const spaceContext = React.useMemo(() => ({
78 latestIndex
79 }), [latestIndex]);
80 // =========================== Render ===========================
81 if (childNodes.length === 0) {
82 return null;
83 }
84 const gapStyle = {};
85 if (wrap) {
86 gapStyle.flexWrap = 'wrap';
87 }
88 if (!isPresetHorizontalSize && isValidHorizontalSize) {
89 gapStyle.columnGap = horizontalSize;
90 }
91 if (!isPresetVerticalSize && isValidVerticalSize) {
92 gapStyle.rowGap = verticalSize;
93 }
94 return wrapCSSVar( /*#__PURE__*/React.createElement("div", Object.assign({
95 ref: ref,
96 className: cls,
97 style: Object.assign(Object.assign(Object.assign({}, gapStyle), space === null || space === void 0 ? void 0 : space.style), style)
98 }, otherProps), /*#__PURE__*/React.createElement(SpaceContextProvider, {
99 value: spaceContext
100 }, nodes)));
101});
102if (process.env.NODE_ENV !== 'production') {
103 Space.displayName = 'Space';
104}
105const CompoundedSpace = Space;
106CompoundedSpace.Compact = Compact;
107export default CompoundedSpace;
\No newline at end of file