index.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { createVNode as _createVNode } from "vue";
  3. import { defineComponent } from 'vue';
  4. import PropTypes from '../_util/vue-types';
  5. import { flattenChildren } from '../_util/props-util';
  6. import { withInstall } from '../_util/type';
  7. import useConfigInject from '../config-provider/hooks/useConfigInject';
  8. // CSSINJS
  9. import useStyle from './style';
  10. export const commentProps = () => ({
  11. actions: Array,
  12. /** The element to display as the comment author. */
  13. author: PropTypes.any,
  14. /** The element to display as the comment avatar - generally an antd Avatar */
  15. avatar: PropTypes.any,
  16. /** The main content of the comment */
  17. content: PropTypes.any,
  18. /** Comment prefix defaults to '.ant-comment' */
  19. prefixCls: String,
  20. /** A datetime element containing the time to be displayed */
  21. datetime: PropTypes.any
  22. });
  23. const Comment = defineComponent({
  24. compatConfig: {
  25. MODE: 3
  26. },
  27. name: 'AComment',
  28. inheritAttrs: false,
  29. props: commentProps(),
  30. slots: Object,
  31. setup(props, _ref) {
  32. let {
  33. slots,
  34. attrs
  35. } = _ref;
  36. const {
  37. prefixCls,
  38. direction
  39. } = useConfigInject('comment', props);
  40. // style
  41. const [wrapSSR, hashId] = useStyle(prefixCls);
  42. const renderNested = (prefixCls, children) => {
  43. return _createVNode("div", {
  44. "class": `${prefixCls}-nested`
  45. }, [children]);
  46. };
  47. const getAction = actions => {
  48. if (!actions || !actions.length) {
  49. return null;
  50. }
  51. const actionList = actions.map((action, index) => _createVNode("li", {
  52. "key": `action-${index}`
  53. }, [action]));
  54. return actionList;
  55. };
  56. return () => {
  57. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
  58. const pre = prefixCls.value;
  59. const actions = (_a = props.actions) !== null && _a !== void 0 ? _a : (_b = slots.actions) === null || _b === void 0 ? void 0 : _b.call(slots);
  60. const author = (_c = props.author) !== null && _c !== void 0 ? _c : (_d = slots.author) === null || _d === void 0 ? void 0 : _d.call(slots);
  61. const avatar = (_e = props.avatar) !== null && _e !== void 0 ? _e : (_f = slots.avatar) === null || _f === void 0 ? void 0 : _f.call(slots);
  62. const content = (_g = props.content) !== null && _g !== void 0 ? _g : (_h = slots.content) === null || _h === void 0 ? void 0 : _h.call(slots);
  63. const datetime = (_j = props.datetime) !== null && _j !== void 0 ? _j : (_k = slots.datetime) === null || _k === void 0 ? void 0 : _k.call(slots);
  64. const avatarDom = _createVNode("div", {
  65. "class": `${pre}-avatar`
  66. }, [typeof avatar === 'string' ? _createVNode("img", {
  67. "src": avatar,
  68. "alt": "comment-avatar"
  69. }, null) : avatar]);
  70. const actionDom = actions ? _createVNode("ul", {
  71. "class": `${pre}-actions`
  72. }, [getAction(Array.isArray(actions) ? actions : [actions])]) : null;
  73. const authorContent = _createVNode("div", {
  74. "class": `${pre}-content-author`
  75. }, [author && _createVNode("span", {
  76. "class": `${pre}-content-author-name`
  77. }, [author]), datetime && _createVNode("span", {
  78. "class": `${pre}-content-author-time`
  79. }, [datetime])]);
  80. const contentDom = _createVNode("div", {
  81. "class": `${pre}-content`
  82. }, [authorContent, _createVNode("div", {
  83. "class": `${pre}-content-detail`
  84. }, [content]), actionDom]);
  85. const comment = _createVNode("div", {
  86. "class": `${pre}-inner`
  87. }, [avatarDom, contentDom]);
  88. const children = flattenChildren((_l = slots.default) === null || _l === void 0 ? void 0 : _l.call(slots));
  89. return wrapSSR(_createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
  90. "class": [pre, {
  91. [`${pre}-rtl`]: direction.value === 'rtl'
  92. }, attrs.class, hashId.value]
  93. }), [comment, children && children.length ? renderNested(pre, children) : null]));
  94. };
  95. }
  96. });
  97. export default withInstall(Comment);