[improvement] organize type definitions

This commit is contained in:
陈嘉涵
2019-05-13 19:07:04 +08:00
parent 96b1d8f81d
commit 7755b1738e
40 changed files with 111 additions and 121 deletions
+3 -6
View File
@@ -1,13 +1,10 @@
/* eslint-disable no-use-before-define */
import { isDef, isObj } from '.';
import { ObjectIndex } from './types';
const { hasOwnProperty } = Object.prototype;
type Object = {
[key: string]: any;
}
function assignKey(to: Object, from: Object, key: string) {
function assignKey(to: ObjectIndex, from: ObjectIndex, key: string) {
const val = from[key];
if (!isDef(val)) {
@@ -21,7 +18,7 @@ function assignKey(to: Object, from: Object, key: string) {
}
}
export function deepAssign(to: Object, from: Object) {
export function deepAssign(to: ObjectIndex, from: ObjectIndex): ObjectIndex {
Object.keys(from).forEach(key => {
assignKey(to, from, key);
});
+2
View File
@@ -4,8 +4,10 @@ export function deepClone(obj: object): object {
if (Array.isArray(obj)) {
return obj.map(item => deepClone(item));
}
if (typeof obj === 'object') {
return deepAssign({}, obj);
}
return obj;
}
+1 -2
View File
@@ -2,8 +2,7 @@
/* eslint-disable getter-return */
/* eslint-disable import/no-mutable-exports */
import { isServer } from '.';
type EventHanlder = (event?: Event) => void;
import { EventHanlder } from './types';
export let supportsPassive = false;
+2 -8
View File
@@ -1,8 +1,5 @@
import Vue, { RenderContext, VNodeData } from 'vue';
type ObjectIndex = {
[key: string]: any;
};
import { ObjectIndex } from './types';
type Context = RenderContext & { data: VNodeData & ObjectIndex };
@@ -22,10 +19,7 @@ const inheritKey = [
const mapInheritKey: ObjectIndex = { nativeOn: 'on' };
// inherit partial context, map nativeOn to on
export function inherit(
context: Context,
inheritListeners?: boolean
): InheritContext {
export function inherit(context: Context, inheritListeners?: boolean): InheritContext {
const result = inheritKey.reduce(
(obj, key) => {
if (context.data[key]) {
+34
View File
@@ -0,0 +1,34 @@
import { VNode, CreateElement, RenderContext } from 'vue';
import { InjectOptions, PropsDefinition } from 'vue/types/options';
export type EventHanlder = (eventName?: Event) => void;
export type ObjectIndex = {
[key: string]: any;
};
export type ScopedSlot<Props = any> = (props?: Props) => VNode[] | VNode | undefined;
export type DefaultSlots = {
default?: ScopedSlot;
};
export type ScopedSlots = DefaultSlots & {
[key: string]: ScopedSlot | undefined;
};
export type ModelOptions = {
prop?: string;
event?: string;
};
export type DefaultProps = Record<string, any>;
export type FunctionComponent<Props = DefaultProps, PropDefs = PropsDefinition<Props>> = {
(h: CreateElement, props: Props, slots: ScopedSlots, context: RenderContext<Props>):
| VNode
| undefined;
props?: PropDefs;
model?: ModelOptions;
inject?: InjectOptions;
};
+10 -8
View File
@@ -36,12 +36,14 @@ function prefix(name: string, mods: Mods): Mods {
return ret;
}
export const useBEM = (name: string) => (el?: Mods, mods?: Mods): Mods => {
if (el && typeof el !== 'string') {
mods = el;
el = '';
}
el = join(name, el, ELEMENT);
export function useBEM(name: string) {
return function (el?: Mods, mods?: Mods): Mods {
if (el && typeof el !== 'string') {
mods = el;
el = '';
}
el = join(name, el, ELEMENT);
return mods ? [el, prefix(el, mods)] : el;
};
return mods ? [el, prefix(el, mods)] : el;
};
}
+4 -3
View File
@@ -1,10 +1,11 @@
import { get, camelize } from '..';
import locale from '../../locale';
export const useI18N = (name: string) => {
export function useI18N(name: string) {
const prefix = camelize(name) + '.';
return (path: string, ...args: any[]): string => {
return function (path: string, ...args: any[]): string {
const message = get(locale.messages(), prefix + path) || get(locale.messages(), path);
return typeof message === 'function' ? message(...args) : message;
};
};
}
+23 -62
View File
@@ -4,52 +4,14 @@
import '../../locale';
import { camelize } from '..';
import { SlotsMixin } from '../../mixins/slots';
import Vue, {
VNode,
VueConstructor,
ComponentOptions,
CreateElement,
RenderContext
} from 'vue';
import { InjectOptions, PropsDefinition } from 'vue/types/options';
export type ScopedSlot<Props = any> = (props?: Props) => VNode[] | VNode | undefined;
export type DefaultSlots = {
default?: ScopedSlot;
};
export type ScopedSlots = DefaultSlots & {
[key: string]: ScopedSlot | undefined;
};
export type ModelOptions = {
prop?: string;
event?: string;
};
import Vue, { VNode, VueConstructor, ComponentOptions, RenderContext } from 'vue';
import { DefaultProps, FunctionComponent } from '../types';
export interface VantComponentOptions extends ComponentOptions<Vue> {
functional?: boolean;
install?: (Vue: VueConstructor) => void;
}
export type DefaultProps = Record<string, any>;
export type FunctionComponent<
Props = DefaultProps,
PropDefs = PropsDefinition<Props>
> = {
(
h: CreateElement,
props: Props,
slots: ScopedSlots,
context: RenderContext<Props>
): VNode | undefined;
props?: PropDefs;
model?: ModelOptions;
inject?: InjectOptions;
};
export type TsxBaseProps<Slots> = {
key: string | number;
// hack for jsx prop spread
@@ -105,36 +67,35 @@ export function unifySlots(context: RenderContext) {
}
// should be removed after Vue 3
function transformFunctionComponent(
pure: FunctionComponent
): VantComponentOptions {
function transformFunctionComponent(pure: FunctionComponent): VantComponentOptions {
return {
functional: true,
props: pure.props,
model: pure.model,
render: (h, context): any =>
pure(h, context.props, unifySlots(context), context)
render: (h, context): any => pure(h, context.props, unifySlots(context), context)
};
}
export const useSFC = (name: string) => <Props = DefaultProps, Events = {}, Slots = {}>(
sfc: VantComponentOptions | FunctionComponent
): TsxComponent<Props, Events, Slots> => {
if (typeof sfc === 'function') {
sfc = transformFunctionComponent(sfc);
}
export function useSFC(name: string) {
return function<Props = DefaultProps, Events = {}, Slots = {}> (
sfc: VantComponentOptions | FunctionComponent
): TsxComponent<Props, Events, Slots> {
if (typeof sfc === 'function') {
sfc = transformFunctionComponent(sfc);
}
if (!sfc.functional) {
sfc.mixins = sfc.mixins || [];
sfc.mixins.push(SlotsMixin);
}
if (!sfc.functional) {
sfc.mixins = sfc.mixins || [];
sfc.mixins.push(SlotsMixin);
}
if (sfc.props) {
defaultProps(sfc.props);
}
if (sfc.props) {
defaultProps(sfc.props);
}
sfc.name = name;
sfc.install = install;
sfc.name = name;
sfc.install = install;
return sfc as TsxComponent<Props, Events, Slots>;
};
return sfc as TsxComponent<Props, Events, Slots>;
};
}