[improvement] functional inherit context (#2716)

This commit is contained in:
neverland
2019-02-11 20:46:46 +08:00
committed by GitHub
parent 9947ab00e8
commit 0bf4a9e799
9 changed files with 104 additions and 85 deletions
+33
View File
@@ -0,0 +1,33 @@
import { RenderContext, VNodeData } from 'vue';
type ObjectIndex = {
[key: string]: any;
};
type Context = RenderContext & { data: VNodeData & ObjectIndex };
type InheritContext = Partial<VNodeData> & ObjectIndex;
const inheritKey = [
'style',
'class',
'attrs',
'nativeOn',
'directives',
'staticClass',
'staticStyle'
];
const mapInheritKey: ObjectIndex = { nativeOn: 'on' };
export function inheritContext(context: Context): InheritContext {
return inheritKey.reduce(
(obj, key) => {
if (context.data[key]) {
obj[mapInheritKey[key] || key] = context.data[key];
}
return obj;
},
{} as InheritContext
);
}