Item.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { createVNode as _createVNode } from "vue";
  3. var __rest = this && this.__rest || function (s, e) {
  4. var t = {};
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  8. }
  9. return t;
  10. };
  11. import PropTypes from '../_util/vue-types';
  12. import classNames from '../_util/classNames';
  13. import { isStringElement, isEmptyElement, flattenChildren } from '../_util/props-util';
  14. import { Col } from '../grid';
  15. import { cloneElement } from '../_util/vnode';
  16. import { defineComponent, inject, ref } from 'vue';
  17. import ItemMeta from './ItemMeta';
  18. import useConfigInject from '../config-provider/hooks/useConfigInject';
  19. import { ListContextKey } from './contextKey';
  20. export const listItemProps = () => ({
  21. prefixCls: String,
  22. extra: PropTypes.any,
  23. actions: PropTypes.array,
  24. grid: Object,
  25. colStyle: {
  26. type: Object,
  27. default: undefined
  28. }
  29. });
  30. export default defineComponent({
  31. compatConfig: {
  32. MODE: 3
  33. },
  34. name: 'AListItem',
  35. inheritAttrs: false,
  36. Meta: ItemMeta,
  37. props: listItemProps(),
  38. slots: Object,
  39. setup(props, _ref) {
  40. let {
  41. slots,
  42. attrs
  43. } = _ref;
  44. const {
  45. itemLayout,
  46. grid
  47. } = inject(ListContextKey, {
  48. grid: ref(),
  49. itemLayout: ref()
  50. });
  51. const {
  52. prefixCls
  53. } = useConfigInject('list', props);
  54. const isItemContainsTextNodeAndNotSingular = () => {
  55. var _a;
  56. const children = ((_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots)) || [];
  57. let result;
  58. children.forEach(element => {
  59. if (isStringElement(element) && !isEmptyElement(element)) {
  60. result = true;
  61. }
  62. });
  63. return result && children.length > 1;
  64. };
  65. const isFlexMode = () => {
  66. var _a, _b;
  67. const extra = (_a = props.extra) !== null && _a !== void 0 ? _a : (_b = slots.extra) === null || _b === void 0 ? void 0 : _b.call(slots);
  68. if (itemLayout.value === 'vertical') {
  69. return !!extra;
  70. }
  71. return !isItemContainsTextNodeAndNotSingular();
  72. };
  73. return () => {
  74. var _a, _b, _c, _d, _e;
  75. const {
  76. class: className
  77. } = attrs,
  78. restAttrs = __rest(attrs, ["class"]);
  79. const pre = prefixCls.value;
  80. const extra = (_a = props.extra) !== null && _a !== void 0 ? _a : (_b = slots.extra) === null || _b === void 0 ? void 0 : _b.call(slots);
  81. const children = (_c = slots.default) === null || _c === void 0 ? void 0 : _c.call(slots);
  82. let actions = (_d = props.actions) !== null && _d !== void 0 ? _d : flattenChildren((_e = slots.actions) === null || _e === void 0 ? void 0 : _e.call(slots));
  83. actions = actions && !Array.isArray(actions) ? [actions] : actions;
  84. const actionsContent = actions && actions.length > 0 && _createVNode("ul", {
  85. "class": `${pre}-item-action`,
  86. "key": "actions"
  87. }, [actions.map((action, i) => _createVNode("li", {
  88. "key": `${pre}-item-action-${i}`
  89. }, [action, i !== actions.length - 1 && _createVNode("em", {
  90. "class": `${pre}-item-action-split`
  91. }, null)]))]);
  92. const Element = grid.value ? 'div' : 'li';
  93. const itemChildren = _createVNode(Element, _objectSpread(_objectSpread({}, restAttrs), {}, {
  94. "class": classNames(`${pre}-item`, {
  95. [`${pre}-item-no-flex`]: !isFlexMode()
  96. }, className)
  97. }), {
  98. default: () => [itemLayout.value === 'vertical' && extra ? [_createVNode("div", {
  99. "class": `${pre}-item-main`,
  100. "key": "content"
  101. }, [children, actionsContent]), _createVNode("div", {
  102. "class": `${pre}-item-extra`,
  103. "key": "extra"
  104. }, [extra])] : [children, actionsContent, cloneElement(extra, {
  105. key: 'extra'
  106. })]]
  107. });
  108. return grid.value ? _createVNode(Col, {
  109. "flex": 1,
  110. "style": props.colStyle
  111. }, {
  112. default: () => [itemChildren]
  113. }) : itemChildren;
  114. };
  115. }
  116. });