Skip to content

Commit 45d814d

Browse files
committed
fix(tooltip): 还原所有属性的功能,核验所有示例
1 parent 518bbbc commit 45d814d

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

packages/renderless/src/tooltip/new-index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,33 @@ export const handlePopEvent =
1616
({ props, api }) =>
1717
(type: string) => {
1818
if (props.manual) return
19+
if (!props.enterable) return
1920

2021
if (type === 'mouseenter') {
2122
api.cancelDelayHide()
23+
} else if (type === 'mouseleave') {
24+
api.delayHide()
2225
}
2326
}
2427

2528
export const toggleShow =
26-
({ state, props, emit }) =>
29+
({ state, props, emit, api }) =>
2730
(isShow: boolean) => {
31+
// 智能识别模式
32+
if (props.visible === 'auto' && state.referenceElm) {
33+
const { clientWidth, scrollWidth } = state.referenceElm.firstElementChild
34+
if (scrollWidth <= clientWidth) {
35+
return
36+
}
37+
}
38+
2839
state.showPopper = isShow
2940
if (props.manual) {
30-
emit('update:show', isShow)
41+
emit('update:modelValue', isShow)
42+
}
43+
44+
// 自动隐藏: 如果显示,且要自动隐藏,则延时后关闭
45+
if (!props.manual && props.hideAfter && isShow) {
46+
api.delayHideAfter()
3147
}
3248
}

packages/renderless/src/tooltip/new-vue.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
/**
2-
* Copyright (c) 2022 - present TinyVue Authors.
3-
* Copyright (c) 2022 - present Huawei Cloud Computing Technologies Co., Ltd.
4-
*
5-
* Use of this source code is governed by an MIT-style license.
6-
*
7-
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8-
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9-
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10-
*
11-
*/
12-
131
import { handlePopEvent, handleRefEvent, toggleShow } from './new-index'
142
import { userPopper, useTimer } from '@opentiny/vue-hooks'
3+
import { guid } from '@opentiny/utils'
154

165
export const api = ['state', 'handlePopEvent', 'handleRefEvent']
176

@@ -21,7 +10,7 @@ export const renderless = (
2110
{ vm, emit, slots, nextTick, parent }
2211
) => {
2312
const api = {} as any
24-
13+
const popperVmRef = {}
2514
const { showPopper, updatePopper, popperElm, referenceElm, doDestroy, popperJS } = userPopper({
2615
emit,
2716
props,
@@ -33,32 +22,51 @@ export const renderless = (
3322
slots,
3423
onBeforeUnmount,
3524
onDeactivated,
36-
watch
25+
watch,
26+
popperVmRef
3727
} as any)
3828

3929
const state = reactive({
4030
showPopper,
4131
popperElm,
4232
referenceElm,
33+
tooltipId: guid('tiny-tooltip-', 4),
4334
showContent: inject('showContent', null),
4435
tipsMaxWidth: inject('tips-max-width', null)
4536
})
37+
state.showPopper = false // 初始为false
4638

4739
const { start: delayShow, clear: cancelDelayShow } = useTimer(() => api.toggleShow(true), toRef(props, 'openDelay'))
4840
const { start: delayHide, clear: cancelDelayHide } = useTimer(() => api.toggleShow(false), toRef(props, 'closeDelay'))
41+
const { start: delayHideAfter } = useTimer(() => api.toggleShow(false), toRef(props, 'hideAfter'))
4942

5043
Object.assign(api, {
5144
state,
5245
delayShow,
5346
cancelDelayShow,
5447
delayHide,
5548
cancelDelayHide,
49+
delayHideAfter,
5650
handlePopEvent: handlePopEvent({ props, api }),
5751
handleRefEvent: handleRefEvent({ props, api }),
58-
toggleShow: toggleShow({ state, props, emit })
52+
toggleShow: toggleShow({ state, props, emit, api })
53+
})
54+
watch(
55+
() => props.modelValue,
56+
(val) => {
57+
if (props.manual) {
58+
val ? delayShow() : delayHide()
59+
}
60+
}
61+
)
62+
onMounted(() => {
63+
state.popperElm = vm.$refs.popperRef
64+
state.referenceElm = vm.$refs.referenceRef
65+
// 初始显示
66+
if (props.manual && props.modelValue) {
67+
state.showPopper = true
68+
}
5969
})
60-
61-
onMounted(() => {})
6270

6371
vm.$on('tooltip-update', updatePopper())
6472

packages/vue/src/tooltip/src/tooltip.vue

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
<template>
2-
<span
2+
<div
33
ref="referenceRef"
44
class="tiny-tooltip"
55
v-bind="$attrs"
6+
style="display: inline-block"
7+
:tabindex="tabindex"
8+
:aria-describeby="state.tooltipId"
69
@mouseenter="handleRefEvent('mouseenter')"
710
@mouseleave="handleRefEvent('mouseleave')"
811
>
912
<slot></slot>
10-
</span>
13+
</div>
1114
<Transition :name="transition">
1215
<div
1316
ref="popperRef"
1417
v-show="!disabled && state.showPopper"
18+
:id="state.tooltipId"
1519
class="tiny-tooltip tiny-tooltip__popper"
16-
:class="['is-' + (type || effect), popperClass, state.showContent ? 'tiny-tooltip__show-tips' : '']"
20+
:class="['is-' + (type || effect || 'dark'), popperClass, state.showContent ? 'tiny-tooltip__show-tips' : '']"
1721
:style="{ ['max-width']: state.tipsMaxWidth }"
1822
@mouseenter="handlePopEvent('mouseenter')"
1923
@mouseleave="handlePopEvent('mouseleave')"
2024
>
2125
<slot name="content">
2226
<template v-if="renderContent">
23-
<render-content-node />
27+
<render-content-node :renderContent="renderContent" :content="content" />
2428
</template>
2529
<template v-else>
26-
<span v-if="!pre" class="tiny-tooltip__content-wrapper" :style="{ ['max-height']: contentMaxHeight }">
30+
<div v-if="!pre" class="tiny-tooltip__content-wrapper" :style="{ ['max-height']: contentMaxHeight }">
2731
{{ content }}
28-
</span>
32+
</div>
2933
<pre v-else>{{ content }}</pre>
3034
</template>
3135
</slot>
@@ -43,8 +47,9 @@ export default defineComponent({
4347
componentName: 'Tooltip',
4448
components: {
4549
RenderContentNode: {
50+
props: ['renderContent', 'content'],
4651
render() {
47-
return this.$parent.renderContent(h, this.$parent.content)
52+
return this.renderContent(h, this.content)
4853
}
4954
}
5055
},
@@ -55,21 +60,22 @@ export default defineComponent({
5560
default: () => 'always',
5661
validator: (value: string) => ['always', 'auto'].includes(value)
5762
},
58-
// 原来未暴露的属性
63+
// 原来未暴露的属性, 自动传入vue-popper
5964
adjustArrow: {
6065
type: Boolean,
6166
default: () => false
6267
},
68+
// 自动传入vue-popper
6369
appendToBody: {
6470
type: Boolean,
6571
default: () => true
6672
},
67-
// 原来未暴露的属性
73+
// 原来未暴露的属性, 自动传入vue-popper
6874
arrowOffset: {
6975
type: Number,
7076
default: () => 0
7177
},
72-
// 原来未暴露的属性
78+
// 原来未暴露的属性, 未入 vue-popper, 可能bug
7379
boundariesPadding: {
7480
type: Number,
7581
default: () => 5
@@ -94,6 +100,7 @@ export default defineComponent({
94100
},
95101
manual: { type: Boolean },
96102
modelValue: { type: Boolean },
103+
// 自动传入vue-popper
97104
offset: {
98105
default: () => 0
99106
},

0 commit comments

Comments
 (0)