index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  4. import { Fragment as _Fragment, createVNode as _createVNode } from "vue";
  5. import { defineComponent, computed, ref, watch, Fragment } from 'vue';
  6. import PropTypes from '../_util/vue-types';
  7. import { filterEmpty } from '../_util/props-util';
  8. import { tuple, withInstall } from '../_util/type';
  9. import useConfigInject from '../_util/hooks/useConfigInject';
  10. import useFlexGapSupport from '../_util/hooks/useFlexGapSupport';
  11. import classNames from '../_util/classNames';
  12. var spaceSize = {
  13. small: 8,
  14. middle: 16,
  15. large: 24
  16. };
  17. export var spaceProps = function spaceProps() {
  18. return {
  19. prefixCls: String,
  20. size: {
  21. type: [String, Number, Array]
  22. },
  23. direction: PropTypes.oneOf(tuple('horizontal', 'vertical')).def('horizontal'),
  24. align: PropTypes.oneOf(tuple('start', 'end', 'center', 'baseline')),
  25. wrap: {
  26. type: Boolean,
  27. default: undefined
  28. }
  29. };
  30. };
  31. function getNumberSize(size) {
  32. return typeof size === 'string' ? spaceSize[size] : size || 0;
  33. }
  34. var Space = defineComponent({
  35. compatConfig: {
  36. MODE: 3
  37. },
  38. name: 'ASpace',
  39. props: spaceProps(),
  40. slots: ['split'],
  41. setup: function setup(props, _ref) {
  42. var slots = _ref.slots;
  43. var _useConfigInject = useConfigInject('space', props),
  44. prefixCls = _useConfigInject.prefixCls,
  45. space = _useConfigInject.space,
  46. directionConfig = _useConfigInject.direction;
  47. var supportFlexGap = useFlexGapSupport();
  48. var size = computed(function () {
  49. var _ref2, _props$size, _space$value;
  50. return (_ref2 = (_props$size = props.size) !== null && _props$size !== void 0 ? _props$size : (_space$value = space.value) === null || _space$value === void 0 ? void 0 : _space$value.size) !== null && _ref2 !== void 0 ? _ref2 : 'small';
  51. });
  52. var horizontalSize = ref();
  53. var verticalSize = ref();
  54. watch(size, function () {
  55. var _map = (Array.isArray(size.value) ? size.value : [size.value, size.value]).map(function (item) {
  56. return getNumberSize(item);
  57. });
  58. var _map2 = _slicedToArray(_map, 2);
  59. horizontalSize.value = _map2[0];
  60. verticalSize.value = _map2[1];
  61. }, {
  62. immediate: true
  63. });
  64. var mergedAlign = computed(function () {
  65. return props.align === undefined && props.direction === 'horizontal' ? 'center' : props.align;
  66. });
  67. var cn = computed(function () {
  68. var _classNames;
  69. return classNames(prefixCls.value, "".concat(prefixCls.value, "-").concat(props.direction), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls.value, "-rtl"), directionConfig.value === 'rtl'), _defineProperty(_classNames, "".concat(prefixCls.value, "-align-").concat(mergedAlign.value), mergedAlign.value), _classNames));
  70. });
  71. var marginDirection = computed(function () {
  72. return directionConfig.value === 'rtl' ? 'marginLeft' : 'marginRight';
  73. });
  74. var style = computed(function () {
  75. var gapStyle = {};
  76. if (supportFlexGap.value) {
  77. gapStyle.columnGap = "".concat(horizontalSize.value, "px");
  78. gapStyle.rowGap = "".concat(verticalSize.value, "px");
  79. }
  80. return _objectSpread(_objectSpread({}, gapStyle), props.wrap && {
  81. flexWrap: 'wrap',
  82. marginBottom: "".concat(-verticalSize.value, "px")
  83. });
  84. });
  85. return function () {
  86. var _slots$default, _slots$split;
  87. var wrap = props.wrap,
  88. _props$direction = props.direction,
  89. direction = _props$direction === void 0 ? 'horizontal' : _props$direction;
  90. var children = (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots);
  91. var items = filterEmpty(children);
  92. var len = items.length;
  93. if (len === 0) {
  94. return null;
  95. }
  96. var split = (_slots$split = slots.split) === null || _slots$split === void 0 ? void 0 : _slots$split.call(slots);
  97. var itemClassName = "".concat(prefixCls.value, "-item");
  98. var horizontalSizeVal = horizontalSize.value;
  99. var latestIndex = len - 1;
  100. return _createVNode("div", {
  101. "class": cn.value,
  102. "style": style.value
  103. }, [items.map(function (child, index) {
  104. var originIndex = children.indexOf(child);
  105. var itemStyle = {};
  106. if (!supportFlexGap.value) {
  107. if (direction === 'vertical') {
  108. if (index < latestIndex) {
  109. itemStyle = {
  110. marginBottom: "".concat(horizontalSizeVal / (split ? 2 : 1), "px")
  111. };
  112. }
  113. } else {
  114. itemStyle = _objectSpread(_objectSpread({}, index < latestIndex && _defineProperty({}, marginDirection.value, "".concat(horizontalSizeVal / (split ? 2 : 1), "px"))), wrap && {
  115. paddingBottom: "".concat(verticalSize.value, "px")
  116. });
  117. }
  118. }
  119. return _createVNode(_Fragment, {
  120. "key": originIndex
  121. }, [_createVNode("div", {
  122. "class": itemClassName,
  123. "style": itemStyle
  124. }, [child]), index < latestIndex && split && _createVNode("span", {
  125. "class": "".concat(itemClassName, "-split"),
  126. "style": itemStyle
  127. }, [split])]);
  128. })]);
  129. };
  130. }
  131. });
  132. export default withInstall(Space);