Skip to content

fix(autocomplete,search,base-select,cascader,date-panel, date-range,date-picker,dropdown,input,select,tree): The component under the shadowRoot node event is invalid. #3546

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
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/renderless/src/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export const searchEnterKey =
export const clickOutside =
({ parent, props, state }: Pick<ISearchRenderlessParams, 'parent' | 'props' | 'state'>) =>
(event: Event) => {
if (!parent.$el.contains(event.target)) {
const path = event.composedPath && event.composedPath()
if (path ? !path.includes(parent.$el) : !parent.$el.contains(event.target)) {
state.show = false
props.mini && !state.currentValue && (state.collapse = true)
}
Expand Down
25 changes: 11 additions & 14 deletions packages/vue-directive/src/clickoutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ if (!isServer) {

const createDocumentHandler = (el, binding, vnode) =>
function (mouseup = {}, mousedown = {}) {
let popperElm = vnode.context.popperElm || (vnode.context.state && vnode.context.state.popperElm)
const popperElm = vnode.context.popperElm || (vnode.context.state && vnode.context.state.popperElm)

if (
!mouseup?.target ||
!mousedown?.target ||
el.contains(mouseup.target) ||
el.contains(mousedown.target) ||
el === mouseup.target ||
(popperElm && (popperElm.contains(mouseup.target) || popperElm.contains(mousedown.target)))
) {
// 使用 event.composedPath() 来处理 Shadow DOM 场景。
// composedPath() 会返回事件的完整路径,即使事件穿过了 Shadow DOM 的边界。
// 这确保了即使 popperElm 在 Shadow DOM 外部(或组件本身在 Shadow DOM 内部),
// 我们也能准确判断点击是否发生在组件或其 popper 内部。
const mousedownPath = (mousedown.composedPath && mousedown.composedPath()) || [mousedown.target]
const mouseupPath = (mouseup.composedPath && mouseup.composedPath()) || [mouseup.target]
const isClickInEl = mousedownPath.includes(el) || mouseupPath.includes(el)
const isClickInPopper = popperElm && (mousedownPath.includes(popperElm) || mouseupPath.includes(popperElm))

if (!mousedown.target || !mouseup.target || isClickInEl || isClickInPopper) {
return
}

Expand All @@ -56,10 +58,6 @@ const createDocumentHandler = (el, binding, vnode) =>
}

/**
* v-clickoutside
* @desc 点击元素外面才会触发的事件
* @example
* 两个修饰符,mousedown、mouseup
* 当没有修饰符时,需要同时满足在目标元素外同步按下和释放鼠标才会触发回调。
* ```html
* <div v-clickoutside="handleClose"> // 在元素外部点击时触发
Expand Down Expand Up @@ -110,7 +108,6 @@ export default {
if (nodeList.length === 0 && startClick) {
startClick = null
}

delete el[nameSpace]
}
}
Loading