Row.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { computed, defineComponent, createVNode as _createVNode } from "vue";
  2. import { truthProp, makeStringProp, createNamespace } from "../utils/index.mjs";
  3. import { useChildren } from "@vant/use";
  4. const [name, bem] = createNamespace("row");
  5. const ROW_KEY = Symbol(name);
  6. const rowProps = {
  7. tag: makeStringProp("div"),
  8. wrap: truthProp,
  9. align: String,
  10. gutter: {
  11. type: [String, Number, Array],
  12. default: 0
  13. },
  14. justify: String
  15. };
  16. var stdin_default = defineComponent({
  17. name,
  18. props: rowProps,
  19. setup(props, {
  20. slots
  21. }) {
  22. const {
  23. children,
  24. linkChildren
  25. } = useChildren(ROW_KEY);
  26. const groups = computed(() => {
  27. const groups2 = [[]];
  28. let totalSpan = 0;
  29. children.forEach((child, index) => {
  30. totalSpan += Number(child.span);
  31. if (totalSpan > 24) {
  32. groups2.push([index]);
  33. totalSpan -= 24;
  34. } else {
  35. groups2[groups2.length - 1].push(index);
  36. }
  37. });
  38. return groups2;
  39. });
  40. const spaces = computed(() => {
  41. let gutter = 0;
  42. if (Array.isArray(props.gutter)) {
  43. gutter = Number(props.gutter[0]) || 0;
  44. } else {
  45. gutter = Number(props.gutter);
  46. }
  47. const spaces2 = [];
  48. if (!gutter) {
  49. return spaces2;
  50. }
  51. groups.value.forEach((group) => {
  52. const averagePadding = gutter * (group.length - 1) / group.length;
  53. group.forEach((item, index) => {
  54. if (index === 0) {
  55. spaces2.push({
  56. right: averagePadding
  57. });
  58. } else {
  59. const left = gutter - spaces2[item - 1].right;
  60. const right = averagePadding - left;
  61. spaces2.push({
  62. left,
  63. right
  64. });
  65. }
  66. });
  67. });
  68. return spaces2;
  69. });
  70. const verticalSpaces = computed(() => {
  71. const {
  72. gutter
  73. } = props;
  74. const spaces2 = [];
  75. if (Array.isArray(gutter) && gutter.length > 1) {
  76. const bottom = Number(gutter[1]) || 0;
  77. if (bottom <= 0) {
  78. return spaces2;
  79. }
  80. groups.value.forEach((group, index) => {
  81. if (index === groups.value.length - 1) return;
  82. group.forEach(() => {
  83. spaces2.push({
  84. bottom
  85. });
  86. });
  87. });
  88. }
  89. return spaces2;
  90. });
  91. linkChildren({
  92. spaces,
  93. verticalSpaces
  94. });
  95. return () => {
  96. const {
  97. tag,
  98. wrap,
  99. align,
  100. justify
  101. } = props;
  102. return _createVNode(tag, {
  103. "class": bem({
  104. [`align-${align}`]: align,
  105. [`justify-${justify}`]: justify,
  106. nowrap: !wrap
  107. })
  108. }, {
  109. default: () => {
  110. var _a;
  111. return [(_a = slots.default) == null ? void 0 : _a.call(slots)];
  112. }
  113. });
  114. };
  115. }
  116. });
  117. export {
  118. ROW_KEY,
  119. stdin_default as default,
  120. rowProps
  121. };