Skip to content

Commit

Permalink
feat: 支付宝小程序支持传递函数给三方组件
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjiyong committed Jul 11, 2022
1 parent b577956 commit 89a917a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 403 deletions.
13 changes: 12 additions & 1 deletion packages/taro-alipay/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mergeInternalComponents, mergeReconciler } from '@tarojs/shared'
import { hooks, mergeInternalComponents, mergeReconciler } from '@tarojs/shared'

import { components, hostConfig } from './runtime-utils'

Expand All @@ -12,5 +12,16 @@ Object.defineProperty(navigator, 'userAgent', {
}
})

hooks.tap('onSetThirdPartyComponentsEvent', (dom: any, name: string, value: any) => {
// 以on开头的参数成员,作为attr设置,最终给原生三方组件使用
dom.setAttribute(name, value)
dom.props[name] = value
})

hooks.tap('bindFunctionPropertyToPage', (Current:{page:any},dom: any, name: string, value: any) => {
// 如果是三方组件的函数,需要绑定到页面上进行处理
Current.page[`${dom.sid}${name}`] = value
})

mergeReconciler(hostConfig)
mergeInternalComponents(components)
3 changes: 2 additions & 1 deletion packages/taro-alipay/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class Template extends RecursiveTemplate {
} else if (attr.startsWith('bind')) {
return str + `${attr}="eh" `
} else if (attr.startsWith('on')) {
return str + `${attr}="eh" `
// 普通函数传递,使用页面上的函数实例
return str + `${attr}="{{ i.sid }}${toCamelCase(attr)}" `
}

return str + `${attr}="{{ i.${toCamelCase(attr)} }}" `
Expand Down
13 changes: 8 additions & 5 deletions packages/taro-react/src/props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormElement, Style, TaroElement } from '@tarojs/runtime'
import { capitalize, internalComponents, isFunction, isNumber, isObject, isString, toCamelCase } from '@tarojs/shared'
import { capitalize, hooks, internalComponents, isFunction, isNumber, isObject, isString, toCamelCase } from '@tarojs/shared'

export type Props = Record<string, unknown>

Expand Down Expand Up @@ -30,6 +30,7 @@ export function updateProps (dom: TaroElement, oldProps: Props, newProps: Props)
// const handlers = el!.__handlers[e.type]
// handlers[0](e)
// }
const isInternalComponents = (dom: TaroElement) => capitalize(toCamelCase(dom.tagName.toLowerCase())) in internalComponents

function setEvent (dom: TaroElement, name: string, value: unknown, oldValue?: unknown) {
const isCapture = name.endsWith('Capture')
Expand All @@ -38,9 +39,7 @@ function setEvent (dom: TaroElement, name: string, value: unknown, oldValue?: un
eventName = eventName.slice(0, -7)
}

const compName = capitalize(toCamelCase(dom.tagName.toLowerCase()))

if (eventName === 'click' && compName in internalComponents) {
if (eventName === 'click' && isInternalComponents(dom)) {
eventName = 'tap'
}

Expand Down Expand Up @@ -110,7 +109,11 @@ function setProperty (dom: TaroElement, name: string, value: unknown, oldValue?:
}
}
} else if (isEventName(name)) {
setEvent(dom, name, value, oldValue)
if (hooks.isExist('onSetThirdPartyComponentsEvent') && !isInternalComponents(dom)) {
hooks.call('onSetThirdPartyComponentsEvent', dom, name, value)
} else {
setEvent(dom, name, value, oldValue)
}
} else if (name === 'dangerouslySetInnerHTML') {
const newHtml = (value as DangerouslySetInnerHTML)?.__html ?? ''
const oldHtml = (oldValue as DangerouslySetInnerHTML)?.__html ?? ''
Expand Down
10 changes: 9 additions & 1 deletion packages/taro-runtime/src/dom/element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EMPTY_OBJ, hooks, isArray, isFunction, isObject, isString, isUndefined, Shortcuts, toCamelCase, warn } from '@tarojs/shared'
import { capitalize, EMPTY_OBJ, hooks, internalComponents, isArray, isFunction, isObject, isString, isUndefined, Shortcuts, toCamelCase, warn } from '@tarojs/shared'

import {
CATCH_VIEW,
Expand All @@ -12,6 +12,7 @@ import {
STYLE,
VIEW
} from '../constants'
import { Current } from '../current'
import { MutationObserver, MutationRecordType } from '../dom-external/mutation-observer'
import type { Attributes, Func } from '../interface'
import { extend, getComponentsAlias, isElement, isHasExtractProp, shortcutAttr } from '../utils'
Expand Down Expand Up @@ -161,6 +162,13 @@ export class TaroElement extends TaroNode {
eventSource.set(value, this)
break
default:
// eslint-disable-next-line no-case-declarations
const isInternalComponents = (dom: TaroElement) => capitalize(toCamelCase(dom.tagName.toLowerCase())) in internalComponents

if (isFunction(value) && !isInternalComponents(this) && hooks.isExist('bindFunctionPropertyToPage')) {
hooks.call('bindFunctionPropertyToPage', Current, this, qualifiedName, value)
}

this.props[qualifiedName] = value as string

if (qualifiedName.startsWith('data-')) {
Expand Down
Loading

0 comments on commit 89a917a

Please sign in to comment.