import { use, isDef } from '../utils'; import { inherit } from '../utils/functional'; import Tag from '../tag'; import Image from '../image'; // Types import { CreateElement, RenderContext } from 'vue/types'; import { DefaultSlots, ScopedSlot } from '../utils/types'; export type CardProps = { tag?: string; num?: number | string; desc?: string; thumb?: string; title?: string; price?: number | string; currency: string; centered?: boolean; lazyLoad?: boolean; thumbLink?: string; originPrice?: number | string; }; export type CardSlots = DefaultSlots & { num?: ScopedSlot; tag?: ScopedSlot; tags?: ScopedSlot; desc?: ScopedSlot; title?: ScopedSlot; thumb?: ScopedSlot; price?: ScopedSlot; footer?: ScopedSlot; 'origin-price'?: ScopedSlot; }; export type CardEvents = { onClick?(event: Event): void; }; const [sfc, bem] = use('card'); function Card( h: CreateElement, props: CardProps, slots: CardSlots, ctx: RenderContext ) { const { thumb } = props; const showNum = slots.num || isDef(props.num); const showPrice = slots.price || isDef(props.price); const showOriginPrice = slots['origin-price'] || isDef(props.originPrice); const showBottom = showNum || showPrice || showOriginPrice; function ThumbTag() { if (slots.tag || props.tag) { return (
{slots.tag ? ( slots.tag() ) : ( {props.tag} )}
); } } function Thumb() { if (slots.thumb || thumb) { return ( {slots.thumb ? ( slots.thumb() ) : ( )} {ThumbTag()} ); } } function Title() { if (slots.title) { return slots.title(); } if (props.title) { return
{props.title}
; } } function Desc() { if (slots.desc) { return slots.desc(); } if (props.desc) { return
{props.desc}
; } } function Price() { if (showPrice) { return (
{slots.price ? slots.price() : `${props.currency} ${props.price}`}
); } } function OriginPrice() { if (showOriginPrice) { const slot = slots['origin-price']; return (
{slot ? slot() : `${props.currency} ${props.originPrice}`}
); } } function Num() { if (showNum) { return
{slots.num ? slots.num() : `x ${props.num}`}
; } } function Footer() { if (slots.footer) { return
{slots.footer()}
; } } return (
{Thumb()}
{Title()} {Desc()} {slots.tags && slots.tags()} {showBottom && (
{Price()} {OriginPrice()} {Num()}
)}
{Footer()}
); } Card.props = { tag: String, desc: String, thumb: String, title: String, centered: Boolean, lazyLoad: Boolean, thumbLink: String, num: [Number, String], price: [Number, String], originPrice: [Number, String], currency: { type: String, default: '¥' } }; export default sfc(Card);