From 566bcf93961d6c2e0bc0396f8a84799b2579295c Mon Sep 17 00:00:00 2001 From: Zhenfei You Date: Fri, 30 Mar 2018 17:02:55 +0800 Subject: [PATCH 1/2] feat(weex): support object syntax of class --- src/platforms/weex/runtime/modules/class.js | 39 ++++++++-------- test/weex/cases/cases.spec.js | 1 + test/weex/cases/render/class.vdom.js | 16 +++++++ test/weex/cases/render/class.vue | 50 +++++++++++++++++++++ 4 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 test/weex/cases/render/class.vdom.js create mode 100644 test/weex/cases/render/class.vue diff --git a/src/platforms/weex/runtime/modules/class.js b/src/platforms/weex/runtime/modules/class.js index 029b9e9ed2f..47620552346 100755 --- a/src/platforms/weex/runtime/modules/class.js +++ b/src/platforms/weex/runtime/modules/class.js @@ -1,6 +1,6 @@ /* @flow */ -import { extend } from 'shared/util' +import { extend, isObject } from 'shared/util' function updateClass (oldVnode: VNodeWithData, vnode: VNodeWithData) { const el = vnode.elm @@ -15,25 +15,8 @@ function updateClass (oldVnode: VNodeWithData, vnode: VNodeWithData) { return } - const oldClassList = [] - // unlike web, weex vnode staticClass is an Array - const oldStaticClass: any = oldData.staticClass - if (oldStaticClass) { - oldClassList.push.apply(oldClassList, oldStaticClass) - } - if (oldData.class) { - oldClassList.push.apply(oldClassList, oldData.class) - } - - const classList = [] - // unlike web, weex vnode staticClass is an Array - const staticClass: any = data.staticClass - if (staticClass) { - classList.push.apply(classList, staticClass) - } - if (data.class) { - classList.push.apply(classList, data.class) - } + const oldClassList = makeClassList(oldData) + const classList = makeClassList(data) if (typeof el.setClassList === 'function') { el.setClassList(classList) @@ -49,6 +32,22 @@ function updateClass (oldVnode: VNodeWithData, vnode: VNodeWithData) { } } +function makeClassList (data: VNodeData): Array { + const classList = [] + // unlike web, weex vnode staticClass is an Array + const staticClass: any = data.staticClass + const dataClass = data.class + if (staticClass) { + classList.push.apply(classList, staticClass) + } + if (Array.isArray(dataClass)) { + classList.push.apply(classList, dataClass) + } else if (isObject(dataClass)) { + classList.push.apply(classList, Object.keys(dataClass).filter(className => dataClass[className])) + } + return classList +} + function getStyle (oldClassList: Array, classList: Array, ctx: Component): Object { // style is a weex-only injected object // compiled from + + \ No newline at end of file From 55758729d79d4f3e93862f2c5085789e46242d81 Mon Sep 17 00:00:00 2001 From: Zhenfei You Date: Mon, 2 Apr 2018 17:51:44 +0800 Subject: [PATCH 2/2] feat(weex): add more inline statement examples in the test case --- test/weex/cases/render/class.vdom.js | 3 +++ test/weex/cases/render/class.vue | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/test/weex/cases/render/class.vdom.js b/test/weex/cases/render/class.vdom.js index f162f00e03c..7dec4ab57a9 100644 --- a/test/weex/cases/render/class.vdom.js +++ b/test/weex/cases/render/class.vdom.js @@ -12,5 +12,8 @@ }, { type: 'div', classList: ['box', 'box4'] + }, { + type: 'div', + classList: ['box', 'box5'] }] }) diff --git a/test/weex/cases/render/class.vue b/test/weex/cases/render/class.vue index 6bc1c84401a..f4443c50dd2 100644 --- a/test/weex/cases/render/class.vue +++ b/test/weex/cases/render/class.vue @@ -4,6 +4,7 @@
+
@@ -28,6 +29,10 @@ .box4 { background-color: #AAA; } + + .box5 { + background-color: #999; + }