FloatButtonGroup.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { resolveDirective as _resolveDirective, withDirectives as _withDirectives, createVNode as _createVNode, vShow as _vShow, Fragment as _Fragment } from "vue";
  3. import { defineComponent, ref, computed, watch, onBeforeUnmount } from 'vue';
  4. import CloseOutlined from "@ant-design/icons-vue/es/icons/CloseOutlined";
  5. import FileTextOutlined from "@ant-design/icons-vue/es/icons/FileTextOutlined";
  6. import classNames from '../_util/classNames';
  7. import { getTransitionProps, Transition } from '../_util/transition';
  8. import FloatButton, { floatButtonPrefixCls } from './FloatButton';
  9. import useConfigInject from '../config-provider/hooks/useConfigInject';
  10. import { useProvideFloatButtonGroupContext } from './context';
  11. import { findDOMNode, initDefaultProps } from '../_util/props-util';
  12. import { floatButtonGroupProps } from './interface';
  13. import canUseDom from '../_util/canUseDom';
  14. // CSSINJS
  15. import useStyle from './style';
  16. import useMergedState from '../_util/hooks/useMergedState';
  17. const FloatButtonGroup = defineComponent({
  18. compatConfig: {
  19. MODE: 3
  20. },
  21. name: 'AFloatButtonGroup',
  22. inheritAttrs: false,
  23. props: initDefaultProps(floatButtonGroupProps(), {
  24. type: 'default',
  25. shape: 'circle'
  26. }),
  27. setup(props, _ref) {
  28. let {
  29. attrs,
  30. slots,
  31. emit
  32. } = _ref;
  33. const {
  34. prefixCls,
  35. direction
  36. } = useConfigInject(floatButtonPrefixCls, props);
  37. // style
  38. const [wrapSSR, hashId] = useStyle(prefixCls);
  39. const [open, setOpen] = useMergedState(false, {
  40. value: computed(() => props.open)
  41. });
  42. const floatButtonGroupRef = ref(null);
  43. const floatButtonRef = ref(null);
  44. useProvideFloatButtonGroupContext({
  45. shape: computed(() => props.shape)
  46. });
  47. const hoverTypeAction = {
  48. onMouseenter() {
  49. var _a;
  50. setOpen(true);
  51. emit('update:open', true);
  52. (_a = props.onOpenChange) === null || _a === void 0 ? void 0 : _a.call(props, true);
  53. },
  54. onMouseleave() {
  55. var _a;
  56. setOpen(false);
  57. emit('update:open', false);
  58. (_a = props.onOpenChange) === null || _a === void 0 ? void 0 : _a.call(props, false);
  59. }
  60. };
  61. const hoverAction = computed(() => {
  62. return props.trigger === 'hover' ? hoverTypeAction : {};
  63. });
  64. const handleOpenChange = () => {
  65. var _a;
  66. const nextOpen = !open.value;
  67. emit('update:open', nextOpen);
  68. (_a = props.onOpenChange) === null || _a === void 0 ? void 0 : _a.call(props, nextOpen);
  69. setOpen(nextOpen);
  70. };
  71. const onClick = e => {
  72. var _a, _b, _c;
  73. if ((_a = floatButtonGroupRef.value) === null || _a === void 0 ? void 0 : _a.contains(e.target)) {
  74. if ((_b = findDOMNode(floatButtonRef.value)) === null || _b === void 0 ? void 0 : _b.contains(e.target)) {
  75. handleOpenChange();
  76. }
  77. return;
  78. }
  79. setOpen(false);
  80. emit('update:open', false);
  81. (_c = props.onOpenChange) === null || _c === void 0 ? void 0 : _c.call(props, false);
  82. };
  83. watch(computed(() => props.trigger), value => {
  84. if (!canUseDom()) {
  85. return;
  86. }
  87. document.removeEventListener('click', onClick);
  88. if (value === 'click') {
  89. document.addEventListener('click', onClick);
  90. }
  91. }, {
  92. immediate: true
  93. });
  94. onBeforeUnmount(() => {
  95. document.removeEventListener('click', onClick);
  96. });
  97. return () => {
  98. var _a;
  99. const {
  100. shape = 'circle',
  101. type = 'default',
  102. tooltip,
  103. description,
  104. trigger
  105. } = props;
  106. const groupPrefixCls = `${prefixCls.value}-group`;
  107. const groupCls = classNames(groupPrefixCls, hashId.value, attrs.class, {
  108. [`${groupPrefixCls}-rtl`]: direction.value === 'rtl',
  109. [`${groupPrefixCls}-${shape}`]: shape,
  110. [`${groupPrefixCls}-${shape}-shadow`]: !trigger
  111. });
  112. const wrapperCls = classNames(hashId.value, `${groupPrefixCls}-wrap`);
  113. const transitionProps = getTransitionProps(`${groupPrefixCls}-wrap`);
  114. return wrapSSR(_createVNode("div", _objectSpread(_objectSpread({
  115. "ref": floatButtonGroupRef
  116. }, attrs), {}, {
  117. "class": groupCls
  118. }, hoverAction.value), [trigger && ['click', 'hover'].includes(trigger) ? _createVNode(_Fragment, null, [_createVNode(Transition, transitionProps, {
  119. default: () => [_withDirectives(_createVNode("div", {
  120. "class": wrapperCls
  121. }, [slots.default && slots.default()]), [[_vShow, open.value]])]
  122. }), _createVNode(FloatButton, {
  123. "ref": floatButtonRef,
  124. "type": type,
  125. "shape": shape,
  126. "tooltip": tooltip,
  127. "description": description
  128. }, {
  129. icon: () => {
  130. var _a, _b;
  131. return open.value ? ((_a = slots.closeIcon) === null || _a === void 0 ? void 0 : _a.call(slots)) || _createVNode(CloseOutlined, null, null) : ((_b = slots.icon) === null || _b === void 0 ? void 0 : _b.call(slots)) || _createVNode(FileTextOutlined, null, null);
  132. },
  133. tooltip: slots.tooltip,
  134. description: slots.description
  135. })]) : (_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots)]));
  136. };
  137. }
  138. });
  139. export default FloatButtonGroup;