[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

View File

@ -7,7 +7,7 @@ import Popup from '../popup';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
import { PopupMixinProps } from '../mixins/popup/type';
export type ActionSheetItem = {

View File

@ -7,7 +7,7 @@ import Radio from '../radio';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type AddressItemData = {
id: string | number;

View File

@ -6,7 +6,7 @@ import AddressItem, { AddressItemData } from './Item';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { ScopedSlot, DefaultSlots } from '../utils/use/sfc';
import { ScopedSlot, DefaultSlots } from '../utils/types';
export type AddressListProps = {
value?: string | number;

View File

@ -5,7 +5,7 @@ import Loading, { LoadingType } from '../loading';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type ButtonType = 'default' | 'primary' | 'info' | 'warning' | 'danger';

View File

@ -4,7 +4,7 @@ import Tag from '../tag';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots, ScopedSlot } from '../utils/use/sfc';
import { DefaultSlots, ScopedSlot } from '../utils/types';
export type CardProps = {
tag?: string;

View File

@ -3,7 +3,7 @@ import { inherit } from '../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type CellGroupProps = {
title?: string;

View File

@ -6,7 +6,7 @@ import Icon from '../icon';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { ScopedSlot, DefaultSlots } from '../utils/use/sfc';
import { ScopedSlot, DefaultSlots } from '../utils/types';
import { Mods } from '../utils/use/bem';
export type CellProps = RouteProps &

View File

@ -4,7 +4,7 @@ import Cell from '../cell';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
const [sfc, bem, t] = use('contact-card');

View File

@ -9,7 +9,7 @@ import RadioGroup from '../radio-group';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type ContactListItem = {
id: string | number;

View File

@ -4,7 +4,7 @@ import Cell from '../cell';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
import { Coupon } from '../coupon/shared';
export type CouponCellProps = {

View File

@ -5,7 +5,7 @@ import { functionalRoute, routeProps, RouteProps } from '../utils/router';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type GoodsActionButtonProps = RouteProps & {
text?: string;

View File

@ -5,7 +5,7 @@ import { functionalRoute, routeProps, RouteProps } from '../utils/router';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type GoodsActionIconProps = RouteProps & {
icon: string;

View File

@ -3,7 +3,7 @@ import { inherit } from '../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type GoodsActionProps = {
safeAreaInsetBottom?: boolean;

View File

@ -5,7 +5,7 @@ import { isSrc } from '../utils/validate/src';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type IconProps = {
tag: keyof HTMLElementTagNameMap | string;

View File

@ -3,7 +3,7 @@ import { inherit } from '../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type InfoProps = {
info?: string | number;

View File

@ -3,7 +3,7 @@ import { inherit } from '../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type LoadingType = 'circular' | 'spinner';

View File

@ -4,7 +4,7 @@ import Icon from '../icon';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { ScopedSlot, DefaultSlots } from '../utils/use/sfc';
import { ScopedSlot, DefaultSlots } from '../utils/types';
export type NavBarProps = {
title?: string;

View File

@ -6,7 +6,7 @@ import Popup from '../popup';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
import { PopupMixinProps } from '../mixins/popup/type';
export type NotifyProps = PopupMixinProps & {

View File

@ -4,7 +4,7 @@ import { preventDefault } from '../utils/event';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type OverlayProps = {
zIndex?: number;

View File

@ -5,7 +5,7 @@ import { inherit } from '../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { ScopedSlot, DefaultSlots } from '../utils/use/sfc';
import { ScopedSlot, DefaultSlots } from '../utils/types';
export type PanelProps = {
icon?: string;

View File

@ -3,7 +3,7 @@ import { emit, inherit } from '../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type PasswordInputProps = {
mask: boolean;

View File

@ -6,7 +6,7 @@ import Icon from '../icon';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
const [sfc, bem] = use('rate');

View File

@ -5,7 +5,7 @@ import Field from '../field';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots, ScopedSlot } from '../utils/use/sfc';
import { DefaultSlots, ScopedSlot } from '../utils/types';
const [sfc, bem, t] = use('search');

View File

@ -3,7 +3,7 @@ import { inherit } from '../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type SkeletonProps = {
row: number;

View File

@ -4,7 +4,7 @@ import Button from '../../button';
// Types
import Vue, { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../../utils/use/sfc';
import { DefaultSlots } from '../../utils/types';
export type SkuActionsProps = {
buyText?: string;

View File

@ -4,7 +4,7 @@ import Icon from '../../icon';
// Types
import Vue, { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../../utils/use/sfc';
import { DefaultSlots } from '../../utils/types';
import { SkuData, SkuGoodsData, SelectedSkuData } from '../type';
export type SkuHeaderProps = {

View File

@ -3,7 +3,7 @@ import { inherit } from '../../utils/functional';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../../utils/use/sfc';
import { DefaultSlots } from '../../utils/types';
import { SkuTreeItemData } from '../type';
export type SkuRowProps = {

View File

@ -5,7 +5,7 @@ import Icon from '../icon';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { ScopedSlot, DefaultSlots } from '../utils/use/sfc';
import { ScopedSlot, DefaultSlots } from '../utils/types';
export type SubmitBarProps = {
tip?: string;

View File

@ -6,7 +6,7 @@ import { switchProps, SharedSwitchProps } from '../switch/shared';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type SwitchCellProps = SharedSwitchProps & {
size: string;

View File

@ -6,7 +6,7 @@ import Loading from '../loading';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type SwitchEvents = {
onChange?(checked: boolean): void;

View File

@ -4,7 +4,7 @@ import { RED, BLUE, GREEN, GRAY_DARK } from '../utils/color';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type TagType = 'primary' | 'success' | 'danger';

View File

@ -4,7 +4,7 @@ import Icon from '../icon';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots } from '../utils/use/sfc';
import { DefaultSlots } from '../utils/types';
export type TreeSelectItem = {
text: string;

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);
});

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;
}

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;

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
packages/utils/types.ts Normal file
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;
};

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;
};
}

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;
};
};
}

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>;
};
}