diff --git a/packages/button/index.js b/packages/button/index.js index 63df8ca67..f46cd3486 100644 --- a/packages/button/index.js +++ b/packages/button/index.js @@ -4,75 +4,72 @@ import Loading from '../loading'; const [sfc, bem] = use('button'); -export default sfc({ - functional: true, +function Button(h, props, slots, ctx) { + const { type, disabled, loading, loadingText } = props; - props: { - text: String, - block: Boolean, - plain: Boolean, - round: Boolean, - square: Boolean, - loading: Boolean, - disabled: Boolean, - nativeType: String, - loadingText: String, - bottomAction: Boolean, - tag: { - type: String, - default: 'button' - }, - type: { - type: String, - default: 'default' - }, - size: { - type: String, - default: 'normal' + const onClick = event => { + if (!loading && !disabled) { + emit(ctx, 'click', event); } + }; + + return ( + + {loading ? ( + [ + , + loadingText && {loadingText} + ] + ) : ( + + {slots.default ? slots.default() : props.text} + + )} + + ); +} + +Button.props = { + text: String, + block: Boolean, + plain: Boolean, + round: Boolean, + square: Boolean, + loading: Boolean, + disabled: Boolean, + nativeType: String, + loadingText: String, + bottomAction: Boolean, + tag: { + type: String, + default: 'button' }, - - render(h, context) { - const { props } = context; - const { type, disabled, loading, loadingText } = props; - - const onClick = event => { - if (!loading && !disabled) { - emit(context, 'click', event); - } - }; - - return ( - - {loading ? ( - [ - , - loadingText && ( - {loadingText} - ) - ] - ) : ( - {context.children || props.text} - )} - - ); + type: { + type: String, + default: 'default' + }, + size: { + type: String, + default: 'normal' } -}); +}; + +export default sfc(Button); diff --git a/packages/cell-group/index.js b/packages/cell-group/index.js index e80a2d503..f83005d53 100644 --- a/packages/cell-group/index.js +++ b/packages/cell-group/index.js @@ -2,21 +2,22 @@ import { use } from '../utils'; const [sfc, bem] = use('cell-group'); -export default sfc({ - functional: true, +function CellGroup(h, props, slots, ctx) { + return ( +
+ {slots.default && slots.default()} +
+ ); +} - props: { - border: { - type: Boolean, - default: true - } - }, - - render(h, context) { - return ( -
- {context.children} -
- ); +CellGroup.props = { + border: { + type: Boolean, + default: true } -}); +}; + +export default sfc(CellGroup); diff --git a/packages/cell/index.js b/packages/cell/index.js index 51d94b722..f302d5a67 100644 --- a/packages/cell/index.js +++ b/packages/cell/index.js @@ -1,84 +1,80 @@ import { use, isDef } from '../utils'; import { cellProps } from './shared'; -import { emit, inherit, unifySlots } from '../utils/functional'; +import { emit, inherit } from '../utils/functional'; import { routeProps, functionalRoute } from '../mixins/router'; import Icon from '../icon'; const [sfc, bem] = use('cell'); -export default sfc({ - functional: true, +function Cell(h, props, slots, ctx) { + const { icon, size, title, label, value, isLink, arrowDirection } = props; - props: { - ...cellProps, - ...routeProps, - size: String, - clickable: Boolean, - arrowDirection: String - }, + const showTitle = slots.title || isDef(title); + const showValue = slots.default || isDef(value); - render(h, context) { - const slots = unifySlots(context); - const { props } = context; - const { icon, size, title, label, value, isLink, arrowDirection } = props; + const Title = showTitle && ( +
+ {slots.title ? slots.title() : {title}} + {label &&
{label}
} +
+ ); - const showTitle = slots.title || isDef(title); - const showValue = slots.default || isDef(value); + const Value = showValue && ( +
+ {slots.default ? slots.default() : {value}} +
+ ); - const Title = showTitle && ( -
- {slots.title ? slots.title() : {title}} - {label &&
{label}
} -
+ const LeftIcon = slots.icon + ? slots.icon() + : icon && ; + + const RightIcon = slots['right-icon'] + ? slots['right-icon']() + : isLink && ( + ); - const Value = showValue && ( -
- {slots.default ? slots.default() : {value}} -
- ); + const onClick = event => { + emit(ctx, 'click', event); + functionalRoute(ctx); + }; - const LeftIcon = slots.icon - ? slots.icon() - : icon && ; + return ( +
+ {LeftIcon} + {Title} + {Value} + {RightIcon} + {slots.extra && slots.extra()} +
+ ); +} - const RightIcon = slots['right-icon'] - ? slots['right-icon']() - : isLink && ( - - ); +Cell.props = { + ...cellProps, + ...routeProps, + size: String, + clickable: Boolean, + arrowDirection: String +}; - const onClick = event => { - emit(context, 'click', event); - functionalRoute(context); - }; - - return ( -
- {LeftIcon} - {Title} - {Value} - {RightIcon} - {slots.extra && slots.extra()} -
- ); - } -}); +export default sfc(Cell); diff --git a/packages/contact-card/index.js b/packages/contact-card/index.js index 2849c0e94..1a1ff5569 100644 --- a/packages/contact-card/index.js +++ b/packages/contact-card/index.js @@ -4,46 +4,46 @@ import Cell from '../cell'; const [sfc, bem, t] = use('contact-card'); -export default sfc({ - functional: true, +function ContactCard(h, props, slots, ctx) { + const { type, editable } = props; - props: { - tel: String, - name: String, - addText: String, - editable: { - type: Boolean, - default: true - }, - type: { - type: String, - default: 'add' - } + return ( + { + if (editable) { + emit(ctx, 'click', event); + } + }} + {...inherit(ctx)} + > + {type === 'add' + ? props.addText || t('addText') + : [ +
{`${t('name')}:${props.name}`}
, +
{`${t('tel')}:${props.tel}`}
+ ]} +
+ ); +} + +ContactCard.props = { + tel: String, + name: String, + addText: String, + editable: { + type: Boolean, + default: true }, - - render(h, context) { - const { props } = context; - const { type, editable } = props; - - return ( - { - if (editable) { - emit(context, 'click', event); - } - }} - {...inherit(context)} - > - {type === 'add' - ? props.addText || t('addText') - : [
{`${t('name')}:${props.name}`}
,
{`${t('tel')}:${props.tel}`}
]} -
- ); + type: { + type: String, + default: 'add' } -}); +}; + +export default sfc(ContactCard); diff --git a/packages/contact-list/index.js b/packages/contact-list/index.js index b45d28c76..83a579b33 100644 --- a/packages/contact-list/index.js +++ b/packages/contact-list/index.js @@ -8,62 +8,58 @@ import RadioGroup from '../radio-group'; const [sfc, bem, t] = use('contact-list'); -export default sfc({ - functional: true, +function ContactList(h, props, slots, ctx) { + const List = props.list.map((item, index) => ( + ( + +
{`${item.name},${item.tel}`}
+
+ ), + 'right-icon': () => ( + { + event.stopPropagation(); + emit(ctx, 'edit', item, index); + }} + /> + ) + }} + onClick={() => { + emit(ctx, 'input', item.id); + emit(ctx, 'select', item, index); + }} + /> + )); - props: { - value: null, - list: Array, - addText: String - }, - - render(h, context) { - const { props, listeners } = context; - - const List = props.list.map((item, index) => ( - ( - -
{`${item.name},${item.tel}`}
-
- ), - 'right-icon': () => ( - { - event.stopPropagation(); - emit(context, 'edit', item, index); - }} - /> - ) - }} - onClick={() => { - emit(context, 'input', item.id); - emit(context, 'select', item, index); - }} + return ( +
+ + {List} + +
+ ); +} - return ( -
- - {List} - -
- ); - } -}); +ContactList.props = { + value: null, + list: Array, + addText: String +}; + +export default sfc(ContactList); diff --git a/packages/goods-action-big-btn/index.js b/packages/goods-action-big-btn/index.js index 231ee34f5..3cdc66538 100644 --- a/packages/goods-action-big-btn/index.js +++ b/packages/goods-action-big-btn/index.js @@ -1,43 +1,38 @@ import { use } from '../utils'; import Button from '../button'; -import { emit, inherit, unifySlots } from '../utils/functional'; +import { emit, inherit } from '../utils/functional'; import { functionalRoute, routeProps } from '../mixins/router'; const [sfc, bem] = use('goods-action-big-btn'); -export default sfc({ - functional: true, +function GoodsActionBigBtn(h, props, slots, ctx) { + const onClick = event => { + emit(ctx, 'click', event); + functionalRoute(ctx); + }; - props: { - ...routeProps, - text: String, - primary: Boolean, - loading: Boolean, - disabled: Boolean - }, + return ( + + ); +} - render(h, context) { - const { props } = context; - const slots = unifySlots(context); +GoodsActionBigBtn.props = { + ...routeProps, + text: String, + primary: Boolean, + loading: Boolean, + disabled: Boolean +}; - const onClick = event => { - emit(context, 'click', event); - functionalRoute(context); - }; - - return ( - - ); - } -}); +export default sfc(GoodsActionBigBtn); diff --git a/packages/goods-action-mini-btn/index.js b/packages/goods-action-mini-btn/index.js index 2c2a2887a..4be77a8d4 100644 --- a/packages/goods-action-mini-btn/index.js +++ b/packages/goods-action-mini-btn/index.js @@ -1,43 +1,38 @@ import { use } from '../utils'; import Icon from '../icon'; -import { emit, inherit, unifySlots } from '../utils/functional'; +import { emit, inherit } from '../utils/functional'; import { functionalRoute, routeProps } from '../mixins/router'; const [sfc, bem] = use('goods-action-mini-btn'); -export default sfc({ - functional: true, +function GoodsActionMiniBtn(h, props, slots, ctx) { + const onClick = event => { + emit(ctx, 'click', event); + functionalRoute(ctx); + }; - props: { - ...routeProps, - text: String, - icon: String, - info: [String, Number], - iconClass: String - }, + return ( +
+ + {slots.default ? slots.default() : props.text} +
+ ); +} - render(h, context) { - const { props } = context; - const slots = unifySlots(context); +GoodsActionMiniBtn.props = { + ...routeProps, + text: String, + icon: String, + info: [String, Number], + iconClass: String +}; - const onClick = event => { - emit(context, 'click', event); - functionalRoute(context); - }; - - return ( -
- - {slots.default ? slots.default() : props.text} -
- ); - } -}); +export default sfc(GoodsActionMiniBtn); diff --git a/packages/goods-action/index.js b/packages/goods-action/index.js index d201ad9a7..2ac8e95aa 100644 --- a/packages/goods-action/index.js +++ b/packages/goods-action/index.js @@ -2,14 +2,12 @@ import { use } from '../utils'; const [sfc, bem] = use('goods-action'); -export default sfc({ - functional: true, +function GoodsAction(h, props, slots, ctx) { + return ( +
+ {slots.default && slots.default()} +
+ ); +} - render(h, context) { - return ( -
- {context.children} -
- ); - } -}); +export default sfc(GoodsAction); diff --git a/packages/icon/index.js b/packages/icon/index.js index d7d7b9ec2..1777814be 100644 --- a/packages/icon/index.js +++ b/packages/icon/index.js @@ -4,40 +4,37 @@ import isSrc from '../utils/validate/src'; const [sfc] = use('icon'); -export default sfc({ - functional: true, +function Icon(h, props, slots, ctx) { + const urlIcon = isSrc(props.name); - props: { - name: String, - size: String, - color: String, - info: [String, Number], - classPrefix: { - type: String, - default: 'van-icon' - } - }, + return ( + + {ctx.default && ctx.default()} + {urlIcon && } + + + ); +} - render(h, context) { - const { props } = context; - const urlIcon = isSrc(props.name); - - return ( - - {context.children} - {urlIcon && } - - - ); +Icon.props = { + name: String, + size: String, + color: String, + info: [String, Number], + classPrefix: { + type: String, + default: 'van-icon' } -}); +}; + +export default sfc(Icon); diff --git a/packages/info/index.js b/packages/info/index.js index e98c33bad..df544617c 100644 --- a/packages/info/index.js +++ b/packages/info/index.js @@ -2,20 +2,18 @@ import { use, isDef } from '../utils'; const [sfc, bem] = use('info'); -export default sfc({ - functional: true, +function Info(h, props, slots, ctx) { + return ( + isDef(props.info) && ( +
+ {props.info} +
+ ) + ); +} - props: { - info: [String, Number] - }, +Info.props = { + info: [String, Number] +}; - render(h, { props, data }) { - return ( - isDef(props.info) && ( -
- {props.info} -
- ) - ); - } -}); +export default sfc(Info); diff --git a/packages/loading/index.js b/packages/loading/index.js index 9c038242a..44bd8c2ad 100644 --- a/packages/loading/index.js +++ b/packages/loading/index.js @@ -3,52 +3,50 @@ import { use } from '../utils'; const [sfc, bem] = use('loading'); const DEFAULT_COLOR = '#c9c9c9'; -export default sfc({ - functional: true, +function Loading(h, props, slots, ctx) { + const { color, size, type } = props; - props: { - size: String, - type: { - type: String, - default: 'circular' - }, - color: { - type: String, - default: DEFAULT_COLOR + const colorType = color === 'white' || color === 'black' ? color : ''; + + const style = { + color: color === 'black' ? DEFAULT_COLOR : color, + width: size, + height: size + }; + + const Spin = []; + if (type === 'spinner') { + for (let i = 0; i < 12; i++) { + Spin.push(); } - }, - - render(h, context) { - const { color, size, type } = context.props; - - const colorType = color === 'white' || color === 'black' ? color : ''; - - const style = { - color: color === 'black' ? DEFAULT_COLOR : color, - width: size, - height: size - }; - - const Spin = []; - if (type === 'spinner') { - for (let i = 0; i < 12; i++) { - Spin.push(); - } - } - - const Circular = type === 'circular' && ( - - - - ); - - return ( -
- - {Spin} - {Circular} - -
- ); } -}); + + const Circular = type === 'circular' && ( + + + + ); + + return ( +
+ + {Spin} + {Circular} + +
+ ); +} + +Loading.props = { + size: String, + type: { + type: String, + default: 'circular' + }, + color: { + type: String, + default: DEFAULT_COLOR + } +}; + +export default sfc(Loading); diff --git a/packages/nav-bar/index.js b/packages/nav-bar/index.js index 676c3b09c..8bde4d2c9 100644 --- a/packages/nav-bar/index.js +++ b/packages/nav-bar/index.js @@ -4,46 +4,56 @@ import Icon from '../icon'; const [sfc, bem] = use('nav-bar'); -export default sfc({ - functional: true, - - props: { - title: String, - fixed: Boolean, - leftText: String, - rightText: String, - leftArrow: Boolean, - border: { - type: Boolean, - default: true - }, - zIndex: { - type: Number, - default: 1 - } - }, - - render(h, context) { - const { props, listeners } = context; - const slots = context.slots(); - - return ( -
-
- {slots.left || [ - props.leftArrow && , - props.leftText && {props.leftText} +function NavBar(h, props, slots, ctx) { + return ( +
+
+ {slots.left + ? slots.left() + : [ + props.leftArrow && ( + + ), + props.leftText && ( + {props.leftText} + ) ]} -
-
{slots.title || props.title}
-
- {slots.right || (props.rightText && {props.rightText})} -
- ); +
+ {slots.title ? slots.title() : props.title} +
+
+ {slots.right + ? slots.right() + : props.rightText && ( + {props.rightText} + )} +
+
+ ); +} + +NavBar.props = { + title: String, + fixed: Boolean, + leftText: String, + rightText: String, + leftArrow: Boolean, + border: { + type: Boolean, + default: true + }, + zIndex: { + type: Number, + default: 1 } -}); +}; + +export default sfc(NavBar); diff --git a/packages/panel/index.js b/packages/panel/index.js index 27c18a2fc..63f37f600 100644 --- a/packages/panel/index.js +++ b/packages/panel/index.js @@ -4,35 +4,34 @@ import CellGroup from '../cell-group'; const [sfc, bem] = use('panel'); -export default sfc({ - functional: true, +function Panel(h, props, slots, ctx) { + return ( + + {slots.header ? ( + slots.header() + ) : ( + + )} +
{slots.default && slots.default()}
+ {slots.footer && ( +
{slots.footer()}
+ )} +
+ ); +} - props: { - icon: String, - desc: String, - title: String, - status: String - }, +Panel.props = { + icon: String, + desc: String, + title: String, + status: String +}; - render(h, context) { - const { props } = context; - const slots = context.slots(); - - return ( - - {slots.header || ( - - )} -
{slots.default}
- {slots.footer &&
{slots.footer}
} -
- ); - } -}); +export default sfc(Panel); diff --git a/packages/password-input/index.js b/packages/password-input/index.js index 1338e834d..4626b6c33 100644 --- a/packages/password-input/index.js +++ b/packages/password-input/index.js @@ -3,49 +3,48 @@ import { emit } from '../utils/functional'; const [sfc, bem] = use('password-input'); -export default sfc({ - functional: true, +function PasswordInput(h, props, slots, ctx) { + const info = props.errorInfo || props.info; - props: { - info: String, - errorInfo: String, - value: { - type: String, - default: '' - }, - length: { - type: Number, - default: 6 - } - }, - - render(h, context) { - const { props } = context; - const info = props.errorInfo || props.info; - - const Points = []; - for (let i = 0; i < props.length; i++) { - Points.push( -
  • - -
  • - ); - } - - return ( -
    -
      { - event.stopPropagation(); - emit(context, 'focus', event); - }} - {...context.data} - > - {Points} -
    - {info &&
    {info}
    } -
    + const Points = []; + for (let i = 0; i < props.length; i++) { + Points.push( +
  • + +
  • ); } -}); + + return ( +
    +
      { + event.stopPropagation(); + emit(ctx, 'focus', event); + }} + {...ctx.data} + > + {Points} +
    + {info && ( +
    {info}
    + )} +
    + ); +} + +PasswordInput.props = { + info: String, + errorInfo: String, + value: { + type: String, + default: '' + }, + length: { + type: Number, + default: 6 + } +}; + +export default sfc(PasswordInput); diff --git a/packages/submit-bar/index.js b/packages/submit-bar/index.js index 86a30c1cc..fbadd17b3 100644 --- a/packages/submit-bar/index.js +++ b/packages/submit-bar/index.js @@ -4,63 +4,61 @@ import Button from '../button'; const [sfc, bem, t] = use('submit-bar'); -export default sfc({ - functional: true, +function SubmitBar(h, props, slots, ctx) { + const { tip, price } = props; + const hasPrice = typeof price === 'number'; - props: { - tip: String, - label: String, - loading: Boolean, - disabled: Boolean, - buttonText: String, - price: { - type: Number, - default: null - }, - currency: { - type: String, - default: '¥' - }, - buttonType: { - type: String, - default: 'danger' - } - }, - - render(h, context) { - const { props, listeners } = context; - const { tip, price } = props; - const slots = context.slots(); - const hasPrice = typeof price === 'number'; - - return ( -
    - {slots.top} - {(slots.tip || tip) && ( -
    - {tip} - {slots.tip} -
    - )} -
    - {slots.default} -
    - {hasPrice && [ - {props.label || t('label')}, - {`${props.currency} ${(price / 100).toFixed(2)}`} - ]} -
    -
    - ); +
    + ); +} + +SubmitBar.props = { + tip: String, + label: String, + loading: Boolean, + disabled: Boolean, + buttonText: String, + price: { + type: Number, + default: null + }, + currency: { + type: String, + default: '¥' + }, + buttonType: { + type: String, + default: 'danger' } -}); +}; + +export default sfc(SubmitBar); diff --git a/packages/switch-cell/index.js b/packages/switch-cell/index.js index 8da552669..367508ef1 100644 --- a/packages/switch-cell/index.js +++ b/packages/switch-cell/index.js @@ -2,31 +2,32 @@ import { use } from '../utils'; import { inherit } from '../utils/functional'; import Cell from '../cell'; import Switch from '../switch'; -import SwitchMixin from '../mixins/switch'; +import { switchProps } from '../switch/shared'; const [sfc, bem] = use('switch-cell'); -export default sfc({ - functional: true, +function SwitchCell(h, props, slots, ctx) { + return ( + + + + ); +} - mixins: [SwitchMixin], - - props: { - title: String, - border: Boolean, - size: { - type: String, - default: '24px' - } - }, - - render(h, context) { - const { props } = context; - - return ( - - - - ); +SwitchCell.props = { + ...switchProps, + title: String, + border: Boolean, + size: { + type: String, + default: '24px' } -}); +}; + +export default sfc(SwitchCell); diff --git a/packages/switch/index.js b/packages/switch/index.js index 07815f6dd..315cbdae7 100644 --- a/packages/switch/index.js +++ b/packages/switch/index.js @@ -1,11 +1,11 @@ import { use } from '../utils'; import Loading from '../loading'; -import SwitchMixin from '../mixins/switch'; +import { switchProps } from './shared'; const [sfc, bem] = use('switch'); export default sfc({ - mixins: [SwitchMixin], + props: switchProps, methods: { onClick() { diff --git a/packages/switch/shared.ts b/packages/switch/shared.ts new file mode 100644 index 000000000..c5443f849 --- /dev/null +++ b/packages/switch/shared.ts @@ -0,0 +1,23 @@ +/** + * Common Switch Props + */ + +export const switchProps = { + value: null, + loading: Boolean, + disabled: Boolean, + activeColor: String, + inactiveColor: String, + activeValue: { + type: null, + default: true + }, + inactiveValue: { + type: null, + default: false + }, + size: { + type: String, + default: '30px' + } +}; diff --git a/packages/tag/index.js b/packages/tag/index.js index 18e5efc0b..82a703737 100644 --- a/packages/tag/index.js +++ b/packages/tag/index.js @@ -9,44 +9,41 @@ const COLOR_MAP = { success: GREEN }; -export default sfc({ - functional: true, +function Tag(h, props, slots, ctx) { + const { mark, plain, round, size } = ctx.props; - props: { - size: String, - type: String, - mark: Boolean, - color: String, - plain: Boolean, - round: Boolean, - textColor: String - }, + const color = props.color || COLOR_MAP[props.type] || GRAY_DARK; + const key = plain ? 'color' : 'backgroundColor'; + const style = { [key]: color }; - render(h, context) { - const { props } = context; - const { mark, plain, round, size } = context.props; - - const color = props.color || COLOR_MAP[props.type] || GRAY_DARK; - const key = plain ? 'color' : 'backgroundColor'; - const style = { [key]: color }; - - if (props.textColor) { - style.color = props.textColor; - } - - return ( - - {context.children} - - ); + if (props.textColor) { + style.color = props.textColor; } -}); + + return ( + + {slots.default && slots.default()} + + ); +} + +Tag.props = { + size: String, + type: String, + mark: Boolean, + color: String, + plain: Boolean, + round: Boolean, + textColor: String +}; + +export default sfc(Tag); diff --git a/packages/utils/functional.ts b/packages/utils/functional.ts index adc91613b..3242d790d 100644 --- a/packages/utils/functional.ts +++ b/packages/utils/functional.ts @@ -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; -} diff --git a/packages/utils/use/sfc.ts b/packages/utils/use/sfc.ts index ee38fd201..c1fc6be35 100644 --- a/packages/utils/use/sfc.ts +++ b/packages/utils/use/sfc.ts @@ -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 & { 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: 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; };