From 7755b1738e07a4e6ea11ac9744274c7a7db65bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Mon, 13 May 2019 19:07:04 +0800 Subject: [PATCH] [improvement] organize type definitions --- packages/action-sheet/index.tsx | 2 +- packages/address-list/Item.tsx | 2 +- packages/address-list/index.tsx | 2 +- packages/button/index.tsx | 2 +- packages/card/index.tsx | 2 +- packages/cell-group/index.tsx | 2 +- packages/cell/index.tsx | 2 +- packages/contact-card/index.tsx | 2 +- packages/contact-list/index.tsx | 2 +- packages/coupon-cell/index.tsx | 2 +- packages/goods-action-button/index.tsx | 2 +- packages/goods-action-icon/index.tsx | 2 +- packages/goods-action/index.tsx | 2 +- packages/icon/index.tsx | 2 +- packages/info/index.tsx | 2 +- packages/loading/index.tsx | 2 +- packages/nav-bar/index.tsx | 2 +- packages/notify/Notify.tsx | 2 +- packages/overlay/index.tsx | 2 +- packages/panel/index.tsx | 2 +- packages/password-input/index.tsx | 2 +- packages/rate/index.tsx | 2 +- packages/search/index.tsx | 2 +- packages/skeleton/index.tsx | 2 +- packages/sku/components/SkuActions.tsx | 2 +- packages/sku/components/SkuHeader.tsx | 2 +- packages/sku/components/SkuRow.tsx | 2 +- packages/submit-bar/index.tsx | 2 +- packages/switch-cell/index.tsx | 2 +- packages/switch/index.tsx | 2 +- packages/tag/index.tsx | 2 +- packages/tree-select/index.tsx | 2 +- packages/utils/deep-assign.ts | 9 +-- packages/utils/deep-clone.ts | 2 + packages/utils/event.ts | 3 +- packages/utils/functional.ts | 10 +-- packages/utils/types.ts | 34 +++++++++++ packages/utils/use/bem.ts | 18 +++--- packages/utils/use/i18n.ts | 7 ++- packages/utils/use/sfc.ts | 85 +++++++------------------- 40 files changed, 111 insertions(+), 121 deletions(-) create mode 100644 packages/utils/types.ts diff --git a/packages/action-sheet/index.tsx b/packages/action-sheet/index.tsx index a087c52de..03daf8144 100644 --- a/packages/action-sheet/index.tsx +++ b/packages/action-sheet/index.tsx @@ -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 = { diff --git a/packages/address-list/Item.tsx b/packages/address-list/Item.tsx index c49c84e93..aa3e332f5 100644 --- a/packages/address-list/Item.tsx +++ b/packages/address-list/Item.tsx @@ -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; diff --git a/packages/address-list/index.tsx b/packages/address-list/index.tsx index 7b2876916..dc98e281a 100644 --- a/packages/address-list/index.tsx +++ b/packages/address-list/index.tsx @@ -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; diff --git a/packages/button/index.tsx b/packages/button/index.tsx index c62eb84f2..0e973d2ce 100644 --- a/packages/button/index.tsx +++ b/packages/button/index.tsx @@ -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'; diff --git a/packages/card/index.tsx b/packages/card/index.tsx index 6036ddb4b..0888312e7 100644 --- a/packages/card/index.tsx +++ b/packages/card/index.tsx @@ -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; diff --git a/packages/cell-group/index.tsx b/packages/cell-group/index.tsx index 8360c90b8..2c1415032 100644 --- a/packages/cell-group/index.tsx +++ b/packages/cell-group/index.tsx @@ -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; diff --git a/packages/cell/index.tsx b/packages/cell/index.tsx index 6bf923b44..ff5561ba1 100644 --- a/packages/cell/index.tsx +++ b/packages/cell/index.tsx @@ -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 & diff --git a/packages/contact-card/index.tsx b/packages/contact-card/index.tsx index 83ef53572..c813fbcf2 100644 --- a/packages/contact-card/index.tsx +++ b/packages/contact-card/index.tsx @@ -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'); diff --git a/packages/contact-list/index.tsx b/packages/contact-list/index.tsx index e580162ed..6aa66f0bc 100644 --- a/packages/contact-list/index.tsx +++ b/packages/contact-list/index.tsx @@ -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; diff --git a/packages/coupon-cell/index.tsx b/packages/coupon-cell/index.tsx index cd35898b1..1bc30e4a3 100644 --- a/packages/coupon-cell/index.tsx +++ b/packages/coupon-cell/index.tsx @@ -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 = { diff --git a/packages/goods-action-button/index.tsx b/packages/goods-action-button/index.tsx index c09aadc77..5d6514803 100644 --- a/packages/goods-action-button/index.tsx +++ b/packages/goods-action-button/index.tsx @@ -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; diff --git a/packages/goods-action-icon/index.tsx b/packages/goods-action-icon/index.tsx index bb4a6205f..dbe702fba 100644 --- a/packages/goods-action-icon/index.tsx +++ b/packages/goods-action-icon/index.tsx @@ -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; diff --git a/packages/goods-action/index.tsx b/packages/goods-action/index.tsx index 7d39baf2f..d89154b29 100644 --- a/packages/goods-action/index.tsx +++ b/packages/goods-action/index.tsx @@ -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; diff --git a/packages/icon/index.tsx b/packages/icon/index.tsx index 5fd21b490..e974473ef 100644 --- a/packages/icon/index.tsx +++ b/packages/icon/index.tsx @@ -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; diff --git a/packages/info/index.tsx b/packages/info/index.tsx index 9f9421bb9..c64f09ecf 100644 --- a/packages/info/index.tsx +++ b/packages/info/index.tsx @@ -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; diff --git a/packages/loading/index.tsx b/packages/loading/index.tsx index 639034aa4..c0e2cdb14 100644 --- a/packages/loading/index.tsx +++ b/packages/loading/index.tsx @@ -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'; diff --git a/packages/nav-bar/index.tsx b/packages/nav-bar/index.tsx index 114ebea37..080620c77 100644 --- a/packages/nav-bar/index.tsx +++ b/packages/nav-bar/index.tsx @@ -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; diff --git a/packages/notify/Notify.tsx b/packages/notify/Notify.tsx index 9dc85a2fb..59a5dd35e 100644 --- a/packages/notify/Notify.tsx +++ b/packages/notify/Notify.tsx @@ -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 & { diff --git a/packages/overlay/index.tsx b/packages/overlay/index.tsx index 85abc90a5..029a050fc 100644 --- a/packages/overlay/index.tsx +++ b/packages/overlay/index.tsx @@ -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; diff --git a/packages/panel/index.tsx b/packages/panel/index.tsx index 952a6168b..ffd4ee5e8 100644 --- a/packages/panel/index.tsx +++ b/packages/panel/index.tsx @@ -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; diff --git a/packages/password-input/index.tsx b/packages/password-input/index.tsx index d044b79be..04dd76206 100644 --- a/packages/password-input/index.tsx +++ b/packages/password-input/index.tsx @@ -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; diff --git a/packages/rate/index.tsx b/packages/rate/index.tsx index d09257cdc..3de20e596 100644 --- a/packages/rate/index.tsx +++ b/packages/rate/index.tsx @@ -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'); diff --git a/packages/search/index.tsx b/packages/search/index.tsx index 0176763ec..57727f4d8 100644 --- a/packages/search/index.tsx +++ b/packages/search/index.tsx @@ -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'); diff --git a/packages/skeleton/index.tsx b/packages/skeleton/index.tsx index d3da65cc6..7f3cd4325 100644 --- a/packages/skeleton/index.tsx +++ b/packages/skeleton/index.tsx @@ -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; diff --git a/packages/sku/components/SkuActions.tsx b/packages/sku/components/SkuActions.tsx index 4cd0655ec..7c691dafa 100644 --- a/packages/sku/components/SkuActions.tsx +++ b/packages/sku/components/SkuActions.tsx @@ -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; diff --git a/packages/sku/components/SkuHeader.tsx b/packages/sku/components/SkuHeader.tsx index 945985d4d..61c7f41fb 100644 --- a/packages/sku/components/SkuHeader.tsx +++ b/packages/sku/components/SkuHeader.tsx @@ -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 = { diff --git a/packages/sku/components/SkuRow.tsx b/packages/sku/components/SkuRow.tsx index c66ef28da..d2ebb6933 100644 --- a/packages/sku/components/SkuRow.tsx +++ b/packages/sku/components/SkuRow.tsx @@ -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 = { diff --git a/packages/submit-bar/index.tsx b/packages/submit-bar/index.tsx index 1693683ba..7794f4ff1 100644 --- a/packages/submit-bar/index.tsx +++ b/packages/submit-bar/index.tsx @@ -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; diff --git a/packages/switch-cell/index.tsx b/packages/switch-cell/index.tsx index 332a34701..b5bd54322 100644 --- a/packages/switch-cell/index.tsx +++ b/packages/switch-cell/index.tsx @@ -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; diff --git a/packages/switch/index.tsx b/packages/switch/index.tsx index 23d9abce8..0c7381934 100644 --- a/packages/switch/index.tsx +++ b/packages/switch/index.tsx @@ -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; diff --git a/packages/tag/index.tsx b/packages/tag/index.tsx index 77ec4d73f..1e6fe3d74 100644 --- a/packages/tag/index.tsx +++ b/packages/tag/index.tsx @@ -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'; diff --git a/packages/tree-select/index.tsx b/packages/tree-select/index.tsx index e22db6a4d..7d7191d12 100644 --- a/packages/tree-select/index.tsx +++ b/packages/tree-select/index.tsx @@ -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; diff --git a/packages/utils/deep-assign.ts b/packages/utils/deep-assign.ts index 2b93b1c9e..3b5f592a3 100644 --- a/packages/utils/deep-assign.ts +++ b/packages/utils/deep-assign.ts @@ -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); }); diff --git a/packages/utils/deep-clone.ts b/packages/utils/deep-clone.ts index 4c8b2c51a..aaf08dfae 100644 --- a/packages/utils/deep-clone.ts +++ b/packages/utils/deep-clone.ts @@ -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; } diff --git a/packages/utils/event.ts b/packages/utils/event.ts index b4b032343..49c023bcc 100644 --- a/packages/utils/event.ts +++ b/packages/utils/event.ts @@ -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; diff --git a/packages/utils/functional.ts b/packages/utils/functional.ts index 2ecf3e291..c3ba829ca 100644 --- a/packages/utils/functional.ts +++ b/packages/utils/functional.ts @@ -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]) { diff --git a/packages/utils/types.ts b/packages/utils/types.ts new file mode 100644 index 000000000..c6850ef39 --- /dev/null +++ b/packages/utils/types.ts @@ -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?: 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; + +export type FunctionComponent> = { + (h: CreateElement, props: Props, slots: ScopedSlots, context: RenderContext): + | VNode + | undefined; + props?: PropDefs; + model?: ModelOptions; + inject?: InjectOptions; +}; diff --git a/packages/utils/use/bem.ts b/packages/utils/use/bem.ts index 03b4d8d41..2ee97b1c0 100644 --- a/packages/utils/use/bem.ts +++ b/packages/utils/use/bem.ts @@ -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; + }; +} diff --git a/packages/utils/use/i18n.ts b/packages/utils/use/i18n.ts index aacc1fb42..4291619c8 100644 --- a/packages/utils/use/i18n.ts +++ b/packages/utils/use/i18n.ts @@ -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; }; -}; +} diff --git a/packages/utils/use/sfc.ts b/packages/utils/use/sfc.ts index 31ae5a381..fe0710370 100644 --- a/packages/utils/use/sfc.ts +++ b/packages/utils/use/sfc.ts @@ -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?: 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 { functional?: boolean; install?: (Vue: VueConstructor) => void; } -export type DefaultProps = Record; - -export type FunctionComponent< - Props = DefaultProps, - PropDefs = PropsDefinition -> = { - ( - h: CreateElement, - props: Props, - slots: ScopedSlots, - context: RenderContext - ): VNode | undefined; - props?: PropDefs; - model?: ModelOptions; - inject?: InjectOptions; -}; - export type TsxBaseProps = { 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) => ( - sfc: VantComponentOptions | FunctionComponent -): TsxComponent => { - if (typeof sfc === 'function') { - sfc = transformFunctionComponent(sfc); - } +export function useSFC(name: string) { + return function ( + sfc: VantComponentOptions | FunctionComponent + ): TsxComponent { + 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; -}; + return sfc as TsxComponent; + }; +}