ant-design-vue/components/float-button/FloatButtonGroup.tsx
Zev Zhu 53dc5daaaf
feat: add static-style-extract (#6713)
* docs: add ant-design-vue nuxt module

* feat: add static-style-extract

* docs: fix import defineProps and defineEmits

* feat: add static-style extract test

* fix: add ignore component

* feat:  calendar select support info.source param (#6697)

* docs: add ant-design-vue nuxt module

* feat: calendar select support info.source param

* docs: synchronous config-provider demo (#6706)

* revert: #6706

* feat: node error

* fix: Handle the output of styles in Server Side Rendering mode

* fix: fix value required error

* chore: change tests dirname

* fix: auchor warning

* chore: add tests

* chore: generate tests snap

* docs: add ssr docs

* docs: synchronous config-provider demo (#6706)

* revert: #6706

* docs: add extract ssr doc

* fix: Removing canUseDom does not have any effect.

* fix: remove range picker in picker map

* fix: watch source can only be a getter/effect function style warning

* fix: styleContext props maybe ref

* fix: remove black list component

* feat: add compChildNameMap

---------

Co-authored-by: selicens <1244620067@qq.com>
Co-authored-by: tangjinzhou <415800467@qq.com>
2023-08-06 12:28:22 +08:00

142 lines
4.5 KiB
Vue

import { defineComponent, ref, computed, watch, onBeforeUnmount } from 'vue';
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
import FileTextOutlined from '@ant-design/icons-vue/FileTextOutlined';
import classNames from '../_util/classNames';
import { getTransitionProps, Transition } from '../_util/transition';
import FloatButton, { floatButtonPrefixCls } from './FloatButton';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import { useProvideFloatButtonGroupContext } from './context';
import { findDOMNode, initDefaultProps } from '../_util/props-util';
import { floatButtonGroupProps } from './interface';
import type { FloatButtonGroupProps } from './interface';
import canUseDom from '../_util/canUseDom';
// CSSINJS
import useStyle from './style';
import useMergedState from '../_util/hooks/useMergedState';
const FloatButtonGroup = defineComponent({
compatConfig: { MODE: 3 },
name: 'AFloatButtonGroup',
inheritAttrs: false,
props: initDefaultProps(floatButtonGroupProps(), {
type: 'default',
shape: 'circle',
} as FloatButtonGroupProps),
setup(props, { attrs, slots, emit }) {
const { prefixCls, direction } = useConfigInject(floatButtonPrefixCls, props);
// style
const [wrapSSR, hashId] = useStyle(prefixCls);
const [open, setOpen] = useMergedState(false, { value: computed(() => props.open) });
const floatButtonGroupRef = ref<HTMLDivElement>(null);
const floatButtonRef = ref<HTMLButtonElement | HTMLAnchorElement>(null);
useProvideFloatButtonGroupContext({
shape: computed(() => props.shape),
});
const hoverTypeAction = {
onMouseenter() {
setOpen(true);
emit('update:open', true);
props.onOpenChange?.(true);
},
onMouseleave() {
setOpen(false);
emit('update:open', false);
props.onOpenChange?.(false);
},
};
const hoverAction = computed(() => {
return props.trigger === 'hover' ? hoverTypeAction : {};
});
const handleOpenChange = () => {
const nextOpen = !open.value;
emit('update:open', nextOpen);
props.onOpenChange?.(nextOpen);
setOpen(nextOpen);
};
const onClick = (e: MouseEvent) => {
if (floatButtonGroupRef.value?.contains(e.target as Node)) {
if (findDOMNode(floatButtonRef.value)?.contains(e.target as Node)) {
handleOpenChange();
}
return;
}
setOpen(false);
emit('update:open', false);
props.onOpenChange?.(false);
};
watch(
computed(() => props.trigger),
value => {
if (!canUseDom()) {
return;
}
document.removeEventListener('click', onClick);
if (value === 'click') {
document.addEventListener('click', onClick);
}
},
{ immediate: true },
);
onBeforeUnmount(() => {
document.removeEventListener('click', onClick);
});
return () => {
const { shape = 'circle', type = 'default', tooltip, description, trigger } = props;
const groupPrefixCls = `${prefixCls.value}-group`;
const groupCls = classNames(groupPrefixCls, hashId.value, attrs.class, {
[`${groupPrefixCls}-rtl`]: direction.value === 'rtl',
[`${groupPrefixCls}-${shape}`]: shape,
[`${groupPrefixCls}-${shape}-shadow`]: !trigger,
});
const wrapperCls = classNames(hashId.value, `${groupPrefixCls}-wrap`);
const transitionProps = getTransitionProps(`${groupPrefixCls}-wrap`);
return wrapSSR(
<div ref={floatButtonGroupRef} {...attrs} class={groupCls} {...hoverAction.value}>
{trigger && ['click', 'hover'].includes(trigger) ? (
<>
<Transition {...transitionProps}>
<div v-show={open.value} class={wrapperCls}>
{slots.default && slots.default()}
</div>
</Transition>
<FloatButton
ref={floatButtonRef}
type={type}
shape={shape}
tooltip={tooltip}
description={description}
v-slots={{
icon: () =>
open.value
? slots.closeIcon?.() || <CloseOutlined />
: slots.icon?.() || <FileTextOutlined />,
tooltip: slots.tooltip,
description: slots.description,
}}
></FloatButton>
</>
) : (
slots.default?.()
)}
</div>,
);
};
},
});
export default FloatButtonGroup;