Skip to content

Commit

Permalink
fix(runtime): 修复阻止跨代组件事件冒泡失败的问题,fix #8757
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Mar 12, 2021
1 parent dbb65e0 commit b50d9f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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
}

0 comments on commit b50d9f5

Please sign in to comment.