index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. "use client";
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  5. Object.defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. Object.defineProperty(exports, "SpaceContext", {
  9. enumerable: true,
  10. get: function () {
  11. return _context2.SpaceContext;
  12. }
  13. });
  14. exports.default = void 0;
  15. var React = _interopRequireWildcard(require("react"));
  16. var _classnames = _interopRequireDefault(require("classnames"));
  17. var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
  18. var _gapSize = require("../_util/gapSize");
  19. var _context = require("../config-provider/context");
  20. var _Compact = _interopRequireDefault(require("./Compact"));
  21. var _context2 = require("./context");
  22. var _Item = _interopRequireDefault(require("./Item"));
  23. var _style = _interopRequireDefault(require("./style"));
  24. var __rest = void 0 && (void 0).__rest || function (s, e) {
  25. var t = {};
  26. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  27. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  28. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  29. }
  30. return t;
  31. };
  32. const InternalSpace = /*#__PURE__*/React.forwardRef((props, ref) => {
  33. var _a;
  34. const {
  35. getPrefixCls,
  36. direction: directionConfig,
  37. size: contextSize,
  38. className: contextClassName,
  39. style: contextStyle,
  40. classNames: contextClassNames,
  41. styles: contextStyles
  42. } = (0, _context.useComponentConfig)('space');
  43. const {
  44. size = contextSize !== null && contextSize !== void 0 ? contextSize : 'small',
  45. align,
  46. className,
  47. rootClassName,
  48. children,
  49. direction = 'horizontal',
  50. prefixCls: customizePrefixCls,
  51. split,
  52. style,
  53. wrap = false,
  54. classNames: customClassNames,
  55. styles
  56. } = props,
  57. otherProps = __rest(props, ["size", "align", "className", "rootClassName", "children", "direction", "prefixCls", "split", "style", "wrap", "classNames", "styles"]);
  58. const [horizontalSize, verticalSize] = Array.isArray(size) ? size : [size, size];
  59. const isPresetVerticalSize = (0, _gapSize.isPresetSize)(verticalSize);
  60. const isPresetHorizontalSize = (0, _gapSize.isPresetSize)(horizontalSize);
  61. const isValidVerticalSize = (0, _gapSize.isValidGapNumber)(verticalSize);
  62. const isValidHorizontalSize = (0, _gapSize.isValidGapNumber)(horizontalSize);
  63. const childNodes = (0, _toArray.default)(children, {
  64. keepEmpty: true
  65. });
  66. const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
  67. const prefixCls = getPrefixCls('space', customizePrefixCls);
  68. const [wrapCSSVar, hashId, cssVarCls] = (0, _style.default)(prefixCls);
  69. const cls = (0, _classnames.default)(prefixCls, contextClassName, hashId, `${prefixCls}-${direction}`, {
  70. [`${prefixCls}-rtl`]: directionConfig === 'rtl',
  71. [`${prefixCls}-align-${mergedAlign}`]: mergedAlign,
  72. [`${prefixCls}-gap-row-${verticalSize}`]: isPresetVerticalSize,
  73. [`${prefixCls}-gap-col-${horizontalSize}`]: isPresetHorizontalSize
  74. }, className, rootClassName, cssVarCls);
  75. const itemClassName = (0, _classnames.default)(`${prefixCls}-item`, (_a = customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.item) !== null && _a !== void 0 ? _a : contextClassNames.item);
  76. // Calculate latest one
  77. let latestIndex = 0;
  78. const nodes = childNodes.map((child, i) => {
  79. var _a;
  80. if (child !== null && child !== undefined) {
  81. latestIndex = i;
  82. }
  83. const key = (child === null || child === void 0 ? void 0 : child.key) || `${itemClassName}-${i}`;
  84. return /*#__PURE__*/React.createElement(_Item.default, {
  85. className: itemClassName,
  86. key: key,
  87. index: i,
  88. split: split,
  89. style: (_a = styles === null || styles === void 0 ? void 0 : styles.item) !== null && _a !== void 0 ? _a : contextStyles.item
  90. }, child);
  91. });
  92. const spaceContext = React.useMemo(() => ({
  93. latestIndex
  94. }), [latestIndex]);
  95. // =========================== Render ===========================
  96. if (childNodes.length === 0) {
  97. return null;
  98. }
  99. const gapStyle = {};
  100. if (wrap) {
  101. gapStyle.flexWrap = 'wrap';
  102. }
  103. if (!isPresetHorizontalSize && isValidHorizontalSize) {
  104. gapStyle.columnGap = horizontalSize;
  105. }
  106. if (!isPresetVerticalSize && isValidVerticalSize) {
  107. gapStyle.rowGap = verticalSize;
  108. }
  109. return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({
  110. ref: ref,
  111. className: cls,
  112. style: Object.assign(Object.assign(Object.assign({}, gapStyle), contextStyle), style)
  113. }, otherProps), /*#__PURE__*/React.createElement(_context2.SpaceContextProvider, {
  114. value: spaceContext
  115. }, nodes)));
  116. });
  117. const Space = InternalSpace;
  118. Space.Compact = _Compact.default;
  119. if (process.env.NODE_ENV !== 'production') {
  120. Space.displayName = 'Space';
  121. }
  122. var _default = exports.default = Space;