Compact.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import toArray from "rc-util/es/Children/toArray";
  13. import { ConfigContext } from '../config-provider';
  14. import useSize from '../config-provider/hooks/useSize';
  15. import useStyle from './style';
  16. export const SpaceCompactItemContext = /*#__PURE__*/React.createContext(null);
  17. export const useCompactItemContext = (prefixCls, direction) => {
  18. const compactItemContext = React.useContext(SpaceCompactItemContext);
  19. const compactItemClassnames = React.useMemo(() => {
  20. if (!compactItemContext) {
  21. return '';
  22. }
  23. const {
  24. compactDirection,
  25. isFirstItem,
  26. isLastItem
  27. } = compactItemContext;
  28. const separator = compactDirection === 'vertical' ? '-vertical-' : '-';
  29. return classNames(`${prefixCls}-compact${separator}item`, {
  30. [`${prefixCls}-compact${separator}first-item`]: isFirstItem,
  31. [`${prefixCls}-compact${separator}last-item`]: isLastItem,
  32. [`${prefixCls}-compact${separator}item-rtl`]: direction === 'rtl'
  33. });
  34. }, [prefixCls, direction, compactItemContext]);
  35. return {
  36. compactSize: compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.compactSize,
  37. compactDirection: compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.compactDirection,
  38. compactItemClassnames
  39. };
  40. };
  41. export const NoCompactStyle = props => {
  42. const {
  43. children
  44. } = props;
  45. return /*#__PURE__*/React.createElement(SpaceCompactItemContext.Provider, {
  46. value: null
  47. }, children);
  48. };
  49. const CompactItem = props => {
  50. const {
  51. children
  52. } = props,
  53. others = __rest(props, ["children"]);
  54. return /*#__PURE__*/React.createElement(SpaceCompactItemContext.Provider, {
  55. value: React.useMemo(() => others, [others])
  56. }, children);
  57. };
  58. const Compact = props => {
  59. const {
  60. getPrefixCls,
  61. direction: directionConfig
  62. } = React.useContext(ConfigContext);
  63. const {
  64. size,
  65. direction,
  66. block,
  67. prefixCls: customizePrefixCls,
  68. className,
  69. rootClassName,
  70. children
  71. } = props,
  72. restProps = __rest(props, ["size", "direction", "block", "prefixCls", "className", "rootClassName", "children"]);
  73. const mergedSize = useSize(ctx => size !== null && size !== void 0 ? size : ctx);
  74. const prefixCls = getPrefixCls('space-compact', customizePrefixCls);
  75. const [wrapCSSVar, hashId] = useStyle(prefixCls);
  76. const clx = classNames(prefixCls, hashId, {
  77. [`${prefixCls}-rtl`]: directionConfig === 'rtl',
  78. [`${prefixCls}-block`]: block,
  79. [`${prefixCls}-vertical`]: direction === 'vertical'
  80. }, className, rootClassName);
  81. const compactItemContext = React.useContext(SpaceCompactItemContext);
  82. const childNodes = toArray(children);
  83. const nodes = React.useMemo(() => childNodes.map((child, i) => {
  84. const key = (child === null || child === void 0 ? void 0 : child.key) || `${prefixCls}-item-${i}`;
  85. return /*#__PURE__*/React.createElement(CompactItem, {
  86. key: key,
  87. compactSize: mergedSize,
  88. compactDirection: direction,
  89. isFirstItem: i === 0 && (!compactItemContext || (compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.isFirstItem)),
  90. isLastItem: i === childNodes.length - 1 && (!compactItemContext || (compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.isLastItem))
  91. }, child);
  92. }), [size, childNodes, compactItemContext]);
  93. // =========================== Render ===========================
  94. if (childNodes.length === 0) {
  95. return null;
  96. }
  97. return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({
  98. className: clx
  99. }, restProps), nodes));
  100. };
  101. export default Compact;