mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-15 14:03:36 +08:00
feat(ReText): 新增ReText组件,支持自动省略显示Tooltip功能, 支持多行省略, 高可复用性 (#898)
* feat(ReText): 新增ReText组件 - 基于El-Text, 增加自动省略显示Tooltip功能, 高可复用性
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
@import "tippy.js/themes/light.css";
|
||||
@import "cropperjs/dist/cropper.css";
|
||||
|
||||
.re-circled {
|
||||
|
||||
7
src/components/ReText/index.ts
Normal file
7
src/components/ReText/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import reText from "./src/index.vue";
|
||||
import { withInstall } from "@pureadmin/utils";
|
||||
|
||||
/** 支持`Tooltip`提示的文本省略组件 */
|
||||
export const ReText = withInstall(reText);
|
||||
|
||||
export default ReText;
|
||||
62
src/components/ReText/src/index.vue
Normal file
62
src/components/ReText/src/index.vue
Normal file
@@ -0,0 +1,62 @@
|
||||
<script lang="ts" setup>
|
||||
import { h, onMounted, ref, useSlots } from "vue";
|
||||
import { useTippy, type TippyOptions } from "vue-tippy";
|
||||
|
||||
const props = defineProps({
|
||||
// 行数
|
||||
lineClamp: {
|
||||
type: [String, Number]
|
||||
},
|
||||
tippyProps: {
|
||||
type: Object as PropType<TippyOptions>,
|
||||
default: () => ({})
|
||||
}
|
||||
});
|
||||
|
||||
const $slots = useSlots();
|
||||
|
||||
const textRef = ref();
|
||||
const tippyFunc = ref();
|
||||
|
||||
const isTextEllipsis = (el: HTMLElement) => {
|
||||
if (!props.lineClamp) {
|
||||
// 单行省略判断
|
||||
return el.scrollWidth > el.clientWidth;
|
||||
} else {
|
||||
// 多行省略判断
|
||||
return el.scrollHeight > el.clientHeight;
|
||||
}
|
||||
};
|
||||
|
||||
const getTippyProps = () => ({
|
||||
content: h($slots.content || $slots.default),
|
||||
...props.tippyProps
|
||||
});
|
||||
|
||||
function handleHover(event: MouseEvent) {
|
||||
if (isTextEllipsis(event.target as HTMLElement)) {
|
||||
tippyFunc.value.setProps(getTippyProps());
|
||||
tippyFunc.value.enable();
|
||||
} else {
|
||||
tippyFunc.value.disable();
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
tippyFunc.value = useTippy(textRef.value?.$el, getTippyProps());
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-text
|
||||
v-bind="{
|
||||
truncated: !lineClamp,
|
||||
lineClamp,
|
||||
...$attrs
|
||||
}"
|
||||
ref="textRef"
|
||||
@mouseover.self="handleHover"
|
||||
>
|
||||
<slot />
|
||||
</el-text>
|
||||
</template>
|
||||
Reference in New Issue
Block a user