Skip to content
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

fix(runtime): 修复阻止跨代组件事件冒泡失败的问题,fix #8757 #8866

Merged
merged 2 commits into from
Mar 12, 2021
Merged
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
4 changes: 2 additions & 2 deletions packages/taro-runtime/src/dom/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EMPTY_OBJ } from '@tarojs/shared'
import { document } from '../bom/document'
import { TaroElement } from './element'
import { CurrentReconciler } from '../reconciler'
import { isParentBinded } from '../utils'

interface EventOptions {
bubbles: boolean;
Expand Down Expand Up @@ -107,9 +108,8 @@ export function eventHandler (event: MpEvent) {
}
if (typeof CurrentReconciler.batchedEventUpdates === 'function') {
const type = event.type
const isParentBinded = node.parentElement?.__handlers[type]?.length

if (!isParentBinded || (type === 'touchmove' && !!node.props.catchMove)) {
if (!isParentBinded(node, type) || (type === 'touchmove' && !!node.props.catchMove)) {
// 最上层组件统一 batchUpdate
CurrentReconciler.batchedEventUpdates(() => {
if (eventsBatch[type]) {
Expand Down
17 changes: 17 additions & 0 deletions packages/taro-runtime/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ export function isHasExtractProp (el: TaroElement): boolean {
})
return Boolean(res)
}

/**
* 往上寻找组件树直到 root,寻找是否有祖先组件绑定了同类型的事件
* @param node 当前组件
* @param type 事件类型
*/
export function isParentBinded (node: TaroElement | null, type: string): boolean {
let res = false
while (node?.parentElement && node.parentElement._path !== 'root') {
if (node.parentElement.__handlers[type]?.length) {
res = true
break
}
node = node.parentElement
}
return res
}