From 1ffee155c6c384830cc1ed8120b9086cf2f254d6 Mon Sep 17 00:00:00 2001 From: Zhenfei You Date: Sat, 7 Apr 2018 12:24:23 +0800 Subject: [PATCH] feat(weex): support object syntax of class (#7930) --- src/platforms/weex/runtime/modules/class.js | 39 +++++++------- test/weex/cases/cases.spec.js | 1 + test/weex/cases/render/class.vdom.js | 19 +++++++ test/weex/cases/render/class.vue | 56 +++++++++++++++++++++ 4 files changed, 95 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