index.js 5.0 KB

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