$emit()支持禁止事件冒泡

pull/1/head 1.9.4
yutent 2023-05-08 18:43:04 +08:00
parent 134e465dd0
commit ecd46ba23d
4 changed files with 12 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@bd/core", "name": "@bd/core",
"version": "1.9.3", "version": "1.9.4",
"type": "module", "type": "module",
"description": "百搭UI组件库的核心", "description": "百搭UI组件库的核心",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -136,9 +136,7 @@ function getTemplateHtml(strings, type) {
let htmlResult = let htmlResult =
html2 + (strings[len] || '<?>') + (type === SVG_RESULT ? '</svg>' : '') html2 + (strings[len] || '<?>') + (type === SVG_RESULT ? '</svg>' : '')
if (!Array.isArray(strings) || !strings.hasOwnProperty('raw')) { if (!Array.isArray(strings) || !strings.hasOwnProperty('raw')) {
let message = 'invalid template strings array' throw new Error('invalid html ast')
throw new Error(message)
} }
return [htmlResult, attrNames] return [htmlResult, attrNames]
} }

View File

@ -337,7 +337,7 @@ export class Component extends HTMLElement {
unbind(this, type, callback, options) unbind(this, type, callback, options)
} }
$emit(type, data = {}) { $emit(type, data = {}, stop = false) {
return fire(this, type, data) return fire(this, type, data, stop)
} }
} }

View File

@ -185,9 +185,15 @@ export function clearOutsideClick(fn = noop) {
unbind(document, 'mousedown', fn) unbind(document, 'mousedown', fn)
} }
export function fire(el, name = 'click', data = {}) { /**
* @param el <DOM> 节点
* @param name <String> 自定义的事件名
* @param data <Object> 要合并进event对象的参数
* @param stop <Boolean> 是否禁止事件冒泡
*/
export function fire(el, name = 'click', data = {}, stop) {
let ev = document.createEvent('Events') let ev = document.createEvent('Events')
ev.initEvent(name, true, true) ev.initEvent(name, !stop, true)
Object.assign(ev, data) Object.assign(ev, data)
el.dispatchEvent(ev) el.dispatchEvent(ev)
} }