index.js
4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.SpaceContext = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
var _configProvider = require("../config-provider");
var _Item = _interopRequireDefault(require("./Item"));
var __rest = void 0 && (void 0).__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
var SpaceContext = /*#__PURE__*/React.createContext({
latestIndex: 0,
horizontalSize: 0,
verticalSize: 0
});
exports.SpaceContext = SpaceContext;
var spaceSize = {
small: 8,
middle: 16,
large: 24
};
function getNumberSize(size) {
return typeof size === 'string' ? spaceSize[size] : size || 0;
}
var Space = function Space(props) {
var _classNames;
var _React$useContext = React.useContext(_configProvider.ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
space = _React$useContext.space,
directionConfig = _React$useContext.direction;
var _props$size = props.size,
size = _props$size === void 0 ? (space === null || space === void 0 ? void 0 : space.size) || 'small' : _props$size,
align = props.align,
className = props.className,
children = props.children,
_props$direction = props.direction,
direction = _props$direction === void 0 ? 'horizontal' : _props$direction,
customizePrefixCls = props.prefixCls,
split = props.split,
style = props.style,
_props$wrap = props.wrap,
wrap = _props$wrap === void 0 ? false : _props$wrap,
otherProps = __rest(props, ["size", "align", "className", "children", "direction", "prefixCls", "split", "style", "wrap"]);
var _React$useMemo = React.useMemo(function () {
return (Array.isArray(size) ? size : [size, size]).map(function (item) {
return getNumberSize(item);
});
}, [size]),
_React$useMemo2 = (0, _slicedToArray2["default"])(_React$useMemo, 2),
horizontalSize = _React$useMemo2[0],
verticalSize = _React$useMemo2[1];
var childNodes = (0, _toArray["default"])(children, {
keepEmpty: true
});
if (childNodes.length === 0) {
return null;
}
var mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
var prefixCls = getPrefixCls('space', customizePrefixCls);
var cn = (0, _classnames["default"])(prefixCls, "".concat(prefixCls, "-").concat(direction), (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-rtl"), directionConfig === 'rtl'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-align-").concat(mergedAlign), mergedAlign), _classNames), className);
var itemClassName = "".concat(prefixCls, "-item");
var marginDirection = directionConfig === 'rtl' ? 'marginLeft' : 'marginRight'; // Calculate latest one
var latestIndex = 0;
var nodes = childNodes.map(function (child, i) {
if (child !== null && child !== undefined) {
latestIndex = i;
}
/* eslint-disable react/no-array-index-key */
return /*#__PURE__*/React.createElement(_Item["default"], {
className: itemClassName,
key: "".concat(itemClassName, "-").concat(i),
direction: direction,
index: i,
marginDirection: marginDirection,
split: split,
wrap: wrap
}, child);
/* eslint-enable */
});
return /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({
className: cn,
style: (0, _extends2["default"])((0, _extends2["default"])({}, wrap && {
flexWrap: 'wrap',
marginBottom: -verticalSize
}), style)
}, otherProps), /*#__PURE__*/React.createElement(SpaceContext.Provider, {
value: {
horizontalSize: horizontalSize,
verticalSize: verticalSize,
latestIndex: latestIndex
}
}, nodes));
};
var _default = Space;
exports["default"] = _default;