From ef38458a3862e6b0e200e7040757c2bc25c730a5 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 5 Feb 2019 12:04:34 +0800 Subject: [PATCH] [bugfix] Functional component inherit context (#2686) --- packages/utils/use/bem.js | 4 +++- packages/utils/use/sfc.js | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/utils/use/bem.js b/packages/utils/use/bem.js index 12451dc6f..1fc709fbd 100644 --- a/packages/utils/use/bem.js +++ b/packages/utils/use/bem.js @@ -22,10 +22,12 @@ const prefix = (name, mods) => { } const ret = {}; - mods && + if (mods) { Object.keys(mods).forEach(key => { ret[name + MODS + key] = mods[key]; }); + } + return ret; }; diff --git a/packages/utils/use/sfc.js b/packages/utils/use/sfc.js index 5c7a57368..593ac8bd4 100644 --- a/packages/utils/use/sfc.js +++ b/packages/utils/use/sfc.js @@ -30,15 +30,21 @@ function install(Vue) { Vue.component(camelize(`-${name}`), this); } +const inheritKey = ['style', 'class', 'nativeOn', 'directives', 'staticClass', 'staticStyle']; +const mapInheritKey = { nativeOn: 'on' }; + function functional(sfc) { const { render } = sfc; sfc.functional = true; - sfc.render = (h, context) => - render(h, context, { - style: context.data.style, - class: context.data.class, - staticClass: context.data.staticClass - }); + sfc.render = (h, context) => { + const inherit = inheritKey.reduce((obj, key) => { + if (context.data[key]) { + obj[mapInheritKey[key] || key] = context.data[key]; + } + return obj; + }, {}); + return render(h, context, inherit); + }; } export default name => (sfc, isFunctional) => {