GridItem.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { computed, defineComponent, mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
  2. import { BORDER, extend, addUnit, numericProp, createNamespace } from "../utils/index.mjs";
  3. import { GRID_KEY } from "../grid/Grid.mjs";
  4. import { useParent } from "@vant/use";
  5. import { useRoute, routeProps } from "../composables/use-route.mjs";
  6. import { Icon } from "../icon/index.mjs";
  7. import { Badge } from "../badge/index.mjs";
  8. const [name, bem] = createNamespace("grid-item");
  9. const gridItemProps = extend({}, routeProps, {
  10. dot: Boolean,
  11. text: String,
  12. icon: String,
  13. badge: numericProp,
  14. iconColor: String,
  15. iconPrefix: String,
  16. badgeProps: Object
  17. });
  18. var stdin_default = defineComponent({
  19. name,
  20. props: gridItemProps,
  21. setup(props, {
  22. slots
  23. }) {
  24. const {
  25. parent,
  26. index
  27. } = useParent(GRID_KEY);
  28. const route = useRoute();
  29. if (!parent) {
  30. if (process.env.NODE_ENV !== "production") {
  31. console.error("[Vant] <GridItem> must be a child component of <Grid>.");
  32. }
  33. return;
  34. }
  35. const rootStyle = computed(() => {
  36. const {
  37. square,
  38. gutter,
  39. columnNum
  40. } = parent.props;
  41. const percent = `${100 / +columnNum}%`;
  42. const style = {
  43. flexBasis: percent
  44. };
  45. if (square) {
  46. style.paddingTop = percent;
  47. } else if (gutter) {
  48. const gutterValue = addUnit(gutter);
  49. style.paddingRight = gutterValue;
  50. if (index.value >= +columnNum) {
  51. style.marginTop = gutterValue;
  52. }
  53. }
  54. return style;
  55. });
  56. const contentStyle = computed(() => {
  57. const {
  58. square,
  59. gutter
  60. } = parent.props;
  61. if (square && gutter) {
  62. const gutterValue = addUnit(gutter);
  63. return {
  64. right: gutterValue,
  65. bottom: gutterValue,
  66. height: "auto"
  67. };
  68. }
  69. });
  70. const renderIcon = () => {
  71. if (slots.icon) {
  72. return _createVNode(Badge, _mergeProps({
  73. "dot": props.dot,
  74. "content": props.badge
  75. }, props.badgeProps), {
  76. default: slots.icon
  77. });
  78. }
  79. if (props.icon) {
  80. return _createVNode(Icon, {
  81. "dot": props.dot,
  82. "name": props.icon,
  83. "size": parent.props.iconSize,
  84. "badge": props.badge,
  85. "class": bem("icon"),
  86. "color": props.iconColor,
  87. "badgeProps": props.badgeProps,
  88. "classPrefix": props.iconPrefix
  89. }, null);
  90. }
  91. };
  92. const renderText = () => {
  93. if (slots.text) {
  94. return slots.text();
  95. }
  96. if (props.text) {
  97. return _createVNode("span", {
  98. "class": bem("text")
  99. }, [props.text]);
  100. }
  101. };
  102. const renderContent = () => {
  103. if (slots.default) {
  104. return slots.default();
  105. }
  106. return [renderIcon(), renderText()];
  107. };
  108. return () => {
  109. const {
  110. center,
  111. border,
  112. square,
  113. gutter,
  114. reverse,
  115. direction,
  116. clickable
  117. } = parent.props;
  118. const classes = [bem("content", [direction, {
  119. center,
  120. square,
  121. reverse,
  122. clickable,
  123. surround: border && gutter
  124. }]), {
  125. [BORDER]: border
  126. }];
  127. return _createVNode("div", {
  128. "class": [bem({
  129. square
  130. })],
  131. "style": rootStyle.value
  132. }, [_createVNode("div", {
  133. "role": clickable ? "button" : void 0,
  134. "class": classes,
  135. "style": contentStyle.value,
  136. "tabindex": clickable ? 0 : void 0,
  137. "onClick": route
  138. }, [renderContent()])]);
  139. };
  140. }
  141. });
  142. export {
  143. stdin_default as default,
  144. gridItemProps
  145. };