-
Notifications
You must be signed in to change notification settings - Fork 307
feat(tooltip): use the template syntax and rewrite the tooltip component. #3449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shenjunjian
wants to merge
9
commits into
dev
Choose a base branch
from
shen/new-tooltip-template
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
518bbbc
feat(tooltip): 初步用模板重构tooltip
shenjunjian 45d814d
fix(tooltip): 还原所有属性的功能,核验所有示例
shenjunjian 9dfb5d9
fix(tooltip): 适配 vue2的场景
shenjunjian be4044f
fix(tooltip): fix 检视意见
shenjunjian c5c7e3e
fix(tooltip): 修复 e2e 测试
shenjunjian 038b77d
fix(tooltip): 删除多余的代码
shenjunjian a632412
fix(tooltip): 新增适配以前的api, state 等等
shenjunjian 8a425a0
fix(tooltip): 修复e2e 用例
shenjunjian d1dab21
fix(tooltip): fix
shenjunjian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export const handleRefEvent = | ||
({ props, api }) => | ||
(type: string) => { | ||
if (props.manual) return | ||
|
||
if (type === 'mouseenter') { | ||
api.cancelDelayHide() | ||
api.delayShow() | ||
} else if (type === 'mouseleave') { | ||
api.cancelDelayShow() | ||
api.delayHide() | ||
} | ||
} | ||
|
||
export const handlePopEvent = | ||
({ props, api }) => | ||
(type: string) => { | ||
if (props.manual) return | ||
if (!props.enterable) return | ||
|
||
if (type === 'mouseenter') { | ||
api.cancelDelayHide() | ||
} else if (type === 'mouseleave') { | ||
api.delayHide() | ||
} | ||
} | ||
|
||
const isDomScroll = (el: HTMLElement) => { | ||
if (!el) return false | ||
|
||
const { clientWidth, scrollWidth } = el | ||
return clientWidth < scrollWidth | ||
} | ||
export const toggleShow = | ||
({ state, props, emit, api }) => | ||
(isShow: boolean) => { | ||
// 智能识别模式 | ||
if (props.visible === 'auto') { | ||
if (!isDomScroll(state.referenceElm) && !isDomScroll(state.referenceElm.firstElementChild)) { | ||
return | ||
} | ||
} | ||
state.showPopper = isShow | ||
if (props.manual) { | ||
emit('update:modelValue', isShow) | ||
} | ||
|
||
// 自动隐藏: 如果显示,且要自动隐藏,则延时后关闭 | ||
if (!props.manual && props.hideAfter && isShow) { | ||
api.delayHideAfter() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { handlePopEvent, handleRefEvent, toggleShow } from './new-index' | ||
import { userPopper, useTimer } from '@opentiny/vue-hooks' | ||
import { guid } from '@opentiny/utils' | ||
|
||
export const api = [ | ||
'state', | ||
'handlePopEvent', | ||
'handleRefEvent', | ||
'show', | ||
'hide', | ||
'updatePopper', | ||
'setExpectedState', | ||
'debounceClose', | ||
'handleClosePopper', | ||
'handleShowPopper', | ||
'doDestroy' | ||
] | ||
|
||
export const renderless = ( | ||
props, | ||
{ ref, watch, toRefs, toRef, reactive, onBeforeUnmount, onDeactivated, onMounted, onUnmounted, inject }, | ||
{ vm, emit, slots, nextTick, parent } | ||
) => { | ||
const api = {} as any | ||
const popperVmRef = {} | ||
const { showPopper, updatePopper, popperElm, referenceElm, currentPlacement, doDestroy } = userPopper({ | ||
emit, | ||
props, | ||
nextTick, | ||
toRefs, | ||
reactive, | ||
parent: parent.$parent, | ||
vm, | ||
slots, | ||
onBeforeUnmount, | ||
onDeactivated, | ||
watch, | ||
popperVmRef | ||
} as any) | ||
|
||
showPopper.value = false // 初始为false | ||
const state = reactive({ | ||
showPopper, | ||
popperElm, | ||
referenceElm, | ||
// 适配以前用法 | ||
currentPlacement, | ||
tooltipId: guid('tiny-tooltip-', 4), | ||
showContent: inject('showContent', null), | ||
tipsMaxWidth: inject('tips-max-width', null) | ||
}) | ||
|
||
const useTimerFn = useTimer({ onUnmounted, ref }) | ||
const toggleShowFn = toggleShow({ state, props, emit, api }) | ||
const { start: delayShow, clear: cancelDelayShow } = useTimerFn(() => toggleShowFn(true), toRef(props, 'openDelay')) | ||
const { start: delayHide, clear: cancelDelayHide } = useTimerFn(() => toggleShowFn(false), toRef(props, 'closeDelay')) | ||
const { start: delayHideAfter } = useTimerFn(() => toggleShowFn(false), toRef(props, 'hideAfter')) | ||
|
||
Object.assign(api, { | ||
state, | ||
delayShow, | ||
cancelDelayShow, | ||
delayHide, | ||
cancelDelayHide, | ||
delayHideAfter, | ||
handlePopEvent: handlePopEvent({ props, api }), | ||
handleRefEvent: handleRefEvent({ props, api }), | ||
// 适配以前用法 | ||
show: delayShow, | ||
hide: delayHide, | ||
updatePopper, | ||
setExpectedState: (value) => { | ||
state.showPopper = value | ||
}, | ||
debounceClose: delayHide, | ||
handleClosePopper: () => (state.showPopper = false), | ||
handleShowPopper: () => (state.showPopper = true), | ||
doDestroy: () => {} | ||
}) | ||
watch( | ||
() => props.modelValue, | ||
(val) => { | ||
if (props.manual) { | ||
val ? delayShow() : delayHide() | ||
} | ||
} | ||
) | ||
|
||
onMounted(() => { | ||
state.popperElm = vm.$refs.popperRef | ||
state.referenceElm = vm.$refs.referenceRef | ||
// 初始显示 | ||
if (props.manual && props.modelValue) { | ||
nextTick(() => (state.showPopper = true)) | ||
} | ||
}) | ||
|
||
// 历史遗留 | ||
vm.$on('tooltip-update', (el?: HTMLElement) => { | ||
if (el) state.popperElm = el | ||
if (props.modelValue) updatePopper() | ||
}) | ||
onUnmounted(() => vm.$off('tooltip-update')) | ||
|
||
return api | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** 延时触发的通用定时器。 setTimeout/ debounce 的地方均由该函数代替。 | ||
* 比如按钮禁用, 1秒后修改disable为false | ||
* 1、 防止连续触发 | ||
* 2、 组件卸载时,自动取消 | ||
* @example | ||
* const {start: resetDisabled } = useTimer(()=> state.disabled=false, 1000) | ||
* resetDisabled(); | ||
* | ||
* const {start: debounceQuery } = useTimer((page)=> grid.query(page), 500) | ||
* debounceQuery(1); | ||
* debounceQuery(2); // 仅请求第2页 | ||
*/ | ||
export const useTimer = | ||
({ onUnmounted, ref }) => | ||
(cb: (...args: any[]) => void, delay: any) => { | ||
let timerId = 0 | ||
const $delay = ref(delay) | ||
|
||
function start(...args: any[]) { | ||
clear() | ||
timerId = setTimeout(() => { | ||
cb(...args) | ||
timerId = 0 | ||
}, $delay.value) | ||
} | ||
function clear() { | ||
if (timerId) { | ||
clearTimeout(timerId) | ||
timerId = 0 | ||
} | ||
} | ||
|
||
onUnmounted(() => clear()) | ||
|
||
return { start, clear, delay: $delay } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
{ | ||
"name": "@opentiny/vue-tooltip", | ||
"type": "module", | ||
"version": "3.23.0", | ||
"description": "", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"main": "lib/index.js", | ||
"module": "index.ts", | ||
"sideEffects": false, | ||
"type": "module", | ||
"devDependencies": { | ||
"@opentiny-internal/vue-test-utils": "workspace:*", | ||
"vitest": "catalog:" | ||
}, | ||
"scripts": { | ||
"build": "pnpm -w build:ui $npm_package_name", | ||
"//postversion": "pnpm build" | ||
}, | ||
"dependencies": { | ||
"@opentiny/vue-renderless": "workspace:~", | ||
"@opentiny/utils": "workspace:~", | ||
"@opentiny/vue-common": "workspace:~", | ||
"@opentiny/vue-directive": "workspace:~", | ||
"@opentiny/vue-renderless": "workspace:~", | ||
"@opentiny/vue-theme": "workspace:~" | ||
}, | ||
"license": "MIT" | ||
"devDependencies": { | ||
"@opentiny-internal/vue-test-utils": "workspace:*", | ||
"vitest": "catalog:" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Simplified tooltip interaction with direct event dispatching.
Using
dispatchEvent('mouseenter')
directly on the content locator is a good approach for testing hover interaction. However, there's a significant amount of commented-out code that should be addressed.Please either remove the commented-out code (lines 26-30) or add a comment explaining why it's retained. Commented-out code can lead to confusion for future maintainers.
📝 Committable suggestion
🤖 Prompt for AI Agents