j-component 1.4.1 → 1.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -159,6 +159,28 @@ comp.dispatchEvent('customevent', {
159
159
  })
160
160
  ```
161
161
 
162
+ ##### addEventListener(eventName, handler, useCapture)
163
+
164
+ 用于外部监听组件触发的事件。
165
+
166
+ ```js
167
+ comp.addEventListener('customevent', evt => {
168
+ console.log(evt)
169
+ })
170
+ ```
171
+
172
+ ##### removeEventListener(eventName, handler, useCapture)
173
+
174
+ 用于外部取消监听组件触发的事件。
175
+
176
+ ```js
177
+ const handler = evt => {
178
+ console.log(evt)
179
+ comp.removeEventListener('customevent', handler)
180
+ }
181
+ comp.addEventListener('customevent', handler)
182
+ ```
183
+
162
184
  ##### triggerLifeTime(lifeTime, args)
163
185
 
164
186
  触发组件实例的生命周期钩子。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-component",
3
- "version": "1.4.1",
3
+ "version": "1.4.5",
4
4
  "description": "miniprogram custom component framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -42,11 +42,11 @@ class ComponentManager {
42
42
  // 支持内置 behavior
43
43
  if (item === 'wx://component-export') {
44
44
  return global.wxComponentExport
45
- }
46
- if (item === 'wx://form-field') {
45
+ } else if (item === 'wx://form-field') {
47
46
  return global.wxFormField
48
- }
49
- if (item === 'wx://form-field-button') {
47
+ } else if (item === 'wx://form-field-group') {
48
+ return global.wxFormFieldGroup
49
+ } else if (item === 'wx://form-field-button') {
50
50
  return global.wxFormFieldButton
51
51
  }
52
52
 
package/src/index.js CHANGED
@@ -54,6 +54,10 @@ global.wxFormField = module.exports.behavior({
54
54
  }
55
55
  })
56
56
 
57
+ global.wxFormFieldGroup = module.exports.behavior({
58
+ is: 'wx://form-field-group',
59
+ })
60
+
57
61
  global.wxFormFieldButton = module.exports.behavior({
58
62
  is: 'wx://form-field-button',
59
63
  listeners: {
@@ -176,6 +176,22 @@ class Component {
176
176
  }
177
177
  }
178
178
 
179
+ /**
180
+ * 监听组件事件
181
+ */
182
+ addEventListener(eventName, handler, capture = false) {
183
+ if (typeof capture === 'object') capture = !!capture.capture
184
+ this._exparserNode.addListener(eventName, handler, {capture})
185
+ }
186
+
187
+ /**
188
+ * 取消监听组件事件
189
+ */
190
+ removeEventListener(eventName, handler, capture = false) {
191
+ if (typeof capture === 'object') capture = !!capture.capture
192
+ this._exparserNode.removeListener(eventName, handler, {capture})
193
+ }
194
+
179
195
  /**
180
196
  * 选取第一个符合的子组件节点
181
197
  */
@@ -264,7 +280,7 @@ class RootComponent extends Component {
264
280
  }
265
281
 
266
282
  get dom() {
267
- return this._exparserNode.$$
283
+ return _.getDom(this._exparserNode)
268
284
  }
269
285
 
270
286
  /**
@@ -204,7 +204,7 @@ class VirtualNode {
204
204
  if (this.children && this.children.length) {
205
205
  if (this.type === CONSTANT.TYPE_FOR) {
206
206
  // 检查 for 语句
207
- const list = expr.calcExpression(statement.for, data)
207
+ const list = expr.calcExpression(statement.for, data) || []
208
208
  options.extra = options.extra || {}
209
209
 
210
210
  for (let i = 0, len = list.length; i < len; i++) {
package/src/tool/utils.js CHANGED
@@ -261,6 +261,25 @@ function relativeToAbsolute(basePath, relativePath) {
261
261
  return pathList.join('/')
262
262
  }
263
263
 
264
+ /**
265
+ * 获取 exparser 节点对应的 dom 节点
266
+ */
267
+ function getDom(exparserNode) {
268
+ let dom = exparserNode.$$
269
+ if (!dom) {
270
+ dom = document.createElement('virtual')
271
+ const fragment = document.createDocumentFragment()
272
+ const shadowRoot = exparserNode.shadowRoot
273
+ const childNodes = shadowRoot && shadowRoot.childNodes
274
+ if (childNodes && childNodes.length) {
275
+ childNodes.forEach(child => fragment.appendChild(getDom(child)))
276
+ }
277
+ dom.appendChild(fragment)
278
+ }
279
+
280
+ return dom
281
+ }
282
+
264
283
  module.exports = {
265
284
  getId,
266
285
  copy,
@@ -277,4 +296,5 @@ module.exports = {
277
296
  parseEvent,
278
297
  normalizeAbsolute,
279
298
  relativeToAbsolute,
299
+ getDom,
280
300
  }
package/CHANGELOG.md DELETED
@@ -1,72 +0,0 @@
1
- # 更新日志
2
-
3
- ## 1.4.1
4
-
5
- * triggerLifeTime 接口支持参数
6
- * 支持 triggerPageLifeTime 接口
7
-
8
- ## 1.4.0
9
-
10
- * 更新基础库 exparser 到 2.15.0
11
- * 修复 observers 在组件 init 之前就会被调一次的问题
12
-
13
- ## 1.3.3
14
-
15
- * 修复 behavior 创建没有调用 callDefinitionFilter 的问题
16
-
17
- ## 1.3.2
18
-
19
- * 修复 properties 的 type 值转换问题
20
-
21
- ## 1.3.1
22
-
23
- * 随机 id 生成使用 Math.random
24
-
25
- ## 1.3.0
26
-
27
- * 更新基础库 exparser 到 2.11.2
28
- * 支持 virtual host 特性
29
-
30
- ## 1.2.3
31
-
32
- * toJSON 方法将会在 root 组件上返回 `<main/>`
33
- * 修复 wxml 属性简写导致 diff 后属性重复的问题
34
- * 修复部分场景 diff 失败问题
35
-
36
- ## 1.2.2
37
-
38
- * 支持 relations
39
- * 支持了 mutated 事件监听
40
- * 修复无法传递驼峰参数
41
- * 异步事件处理改为微任务
42
-
43
- ## 1.2.1
44
-
45
- * 支持 Component.prototype.toJSON
46
- * 添加 typescript 类型声明
47
-
48
- ## 1.2.0
49
-
50
- * 支持内置 behavior wx://component-export
51
- * 更新 exparser 版本为 2.10.4
52
-
53
- ## 1.1.11
54
-
55
- * 支持内置 behavior wx://form-field-button
56
-
57
- ## 1.1.10
58
-
59
- * 修复节点属性值为 falsely 值被强制转为空字符串的问题
60
-
61
- ## 1.1.9
62
-
63
- * dispatchEvent 接口支持触发自定义事件
64
-
65
- ## 1.1.8
66
-
67
- * 修复 diff 时没有处理父节点为根节点的情况
68
-
69
- ## 1.1.7
70
-
71
- * 废弃注册组件时对初始 data 进行深拷贝的逻辑。
72
- * 修复组件 dispatchEvent 方法中自定义事件传入 detail,组件函数接收不到 detail 的问题(#7)。