提交 b56b7031 编写于 作者: fxy060608's avatar fxy060608

feat: add useEvent

上级 6047d964
import { Ref, SetupContext } from 'vue'
type EventDetail = Record<string, any>
export function useCustomEvent(ref: Ref, emit: SetupContext['emit']) {
return (name: string, evt: Event, detail?: EventDetail) => {
emit(
name,
normalizeCustomEvent(name, evt, ref.value as HTMLElement, detail || {})
)
}
}
function normalizeDataset(el: HTMLElement) {
return el.dataset
}
function normalizeTarget(el: HTMLElement): WechatMiniprogram.Target {
const { id, tagName, offsetTop, offsetLeft } = el
return {
id,
tagName,
dataset: normalizeDataset(el),
offsetTop,
offsetLeft,
}
}
function normalizeCustomEvent(
name: string,
domEvt: Event,
el: HTMLElement,
detail: EventDetail
) {
const target = normalizeTarget(el)
const evt: WechatMiniprogram.CustomEvent = {
type: detail.type || name,
timeStamp: domEvt.timeStamp || 0,
target,
currentTarget: target,
detail,
}
return evt
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册