GridItem.mjs 3.6 KB

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