[improvement] Functional components be just functions (#2735)

This commit is contained in:
neverland
2019-02-14 11:56:02 +08:00
committed by GitHub
parent 166397dad4
commit 5a9143c736
21 changed files with 704 additions and 674 deletions
-15
View File
@@ -1,5 +1,4 @@
import { RenderContext, VNodeData } from 'vue/types';
import { ScopedSlot } from 'vue/types/vnode';
type ObjectIndex = {
[key: string]: any;
@@ -47,17 +46,3 @@ export function emit(context: Context, eventName: string, ...args: any[]) {
}
}
}
// unify slots & scopedSlots
export function unifySlots(context: Context) {
const { scopedSlots } = context;
const slots = context.slots();
Object.keys(slots).forEach(key => {
if (!scopedSlots[key]) {
scopedSlots[key] = () => slots[key];
}
});
return scopedSlots;
}
+53 -6
View File
@@ -4,13 +4,29 @@
import '../../locale';
import { camelize } from '..';
import SlotsMixin from '../../mixins/slots';
import Vue, { VueConstructor, ComponentOptions } from 'vue';
import Vue, {
VueConstructor,
ComponentOptions,
CreateElement,
RenderContext
} from 'vue/types';
import { VNode, ScopedSlot } from 'vue/types/vnode';
type VantComponentOptions = ComponentOptions<Vue> & {
functional?: boolean;
install?: (Vue: VueConstructor) => void;
};
type VantPureComponent = {
(
h: CreateElement,
props: { [key: string]: any },
slots: { [key: string]: ScopedSlot | undefined },
context: RenderContext
): VNode;
props: any;
};
const arrayProp = {
type: Array,
default: () => []
@@ -39,15 +55,46 @@ function install(this: ComponentOptions<Vue>, Vue: VueConstructor) {
}
}
export default (name: string) => (sfc: VantComponentOptions) => {
sfc.name = name;
sfc.install = install;
sfc.mixins = sfc.mixins || [];
sfc.mixins.push(SlotsMixin);
// unify slots & scopedSlots
export function unifySlots(context: RenderContext) {
const { scopedSlots } = context;
const slots = context.slots();
Object.keys(slots).forEach(key => {
if (!scopedSlots[key]) {
scopedSlots[key] = () => slots[key];
}
});
return scopedSlots;
}
function transformPureComponent(pure: VantPureComponent): VantComponentOptions {
return {
functional: true,
props: pure.props,
render: (h, context) => pure(h, context.props, unifySlots(context), context)
};
}
export default (name: string) => (
sfc: VantComponentOptions | VantPureComponent
) => {
if (typeof sfc === 'function') {
sfc = transformPureComponent(sfc);
}
if (!sfc.functional) {
sfc.mixins = sfc.mixins || [];
sfc.mixins.push(SlotsMixin);
}
if (sfc.props) {
defaultProps(sfc.props);
}
sfc.name = name;
sfc.install = install;
return sfc;
};