From 607410a64f9ac8ed483e872cac18d6e9405fbdbb Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Mon, 17 Aug 2020 11:53:37 +0800 Subject: [PATCH] feat: migrate Card component --- components.js | 1 + src/card/index.js | 183 ++++++++++++++++++++++++++++++++++++++ src/card/index.tsx | 216 --------------------------------------------- vant.config.js | 16 ++-- 4 files changed, 192 insertions(+), 224 deletions(-) create mode 100644 src/card/index.js delete mode 100644 src/card/index.tsx diff --git a/components.js b/components.js index 2b50e7e54..a836a1984 100644 --- a/components.js +++ b/components.js @@ -69,4 +69,5 @@ module.exports = [ 'index-anchor', 'address-list', 'area', + 'card', ]; diff --git a/src/card/index.js b/src/card/index.js new file mode 100644 index 000000000..1b673fee5 --- /dev/null +++ b/src/card/index.js @@ -0,0 +1,183 @@ +// Utils +import { createNamespace, isDef } from '../utils'; + +// Components +import Tag from '../tag'; +import Image from '../image'; + +const [createComponent, bem] = createNamespace('card'); + +export default createComponent({ + 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: '¥', + }, + }, + + emits: ['click-thumb'], + + setup(props, { slots, emit }) { + return () => { + 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 || slots.bottom; + + function onThumbClick(event) { + emit('click-thumb', event); + } + + 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 PriceContent() { + const priceArr = props.price.toString().split('.'); + return ( +
+ {props.currency} + {priceArr[0]}. + {priceArr[1]} +
+ ); + } + + function Price() { + if (showPrice) { + return ( +
+ {slots.price ? slots.price() : PriceContent()} +
+ ); + } + } + + 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?.()} +
+ {showBottom && ( +
+ {slots['price-top']?.()} + {Price()} + {OriginPrice()} + {Num()} + {slots.bottom?.()} +
+ )} +
+
+ {Footer()} +
+ ); + }; + }, +}); diff --git a/src/card/index.tsx b/src/card/index.tsx deleted file mode 100644 index bc024b103..000000000 --- a/src/card/index.tsx +++ /dev/null @@ -1,216 +0,0 @@ -// Utils -import { createNamespace, isDef } from '../utils'; -import { emit, inherit } from '../utils/functional'; - -// Components -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; - bottom?: ScopedSlot; - footer?: ScopedSlot; - 'origin-price'?: ScopedSlot; - 'price-top'?: ScopedSlot; -}; - -export type CardEvents = { - onClick?(event: Event): void; -}; - -const [createComponent, bem] = createNamespace('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 || slots.bottom; - - function onThumbClick(event: MouseEvent) { - emit(ctx, 'click-thumb', event); - } - - 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 PriceContent() { - const priceArr = props.price!.toString().split('.'); - return ( -
- {props.currency} - {priceArr[0]}. - {priceArr[1]} -
- ); - } - - function Price() { - if (showPrice) { - return ( -
- {slots.price ? slots.price() : PriceContent()} -
- ); - } - } - - 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?.()} -
- {showBottom && ( -
- {slots['price-top']?.()} - {Price()} - {OriginPrice()} - {Num()} - {slots.bottom?.()} -
- )} -
-
- {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 createComponent(Card); diff --git a/vant.config.js b/vant.config.js index 85cdfc04d..569e74d63 100644 --- a/vant.config.js +++ b/vant.config.js @@ -335,10 +335,10 @@ module.exports = { path: 'area', title: 'Area 省市区选择', }, - // { - // path: 'card', - // title: 'Card 商品卡片', - // }, + { + path: 'card', + title: 'Card 商品卡片', + }, // { // path: 'contact-card', // title: 'Contact 联系人', @@ -669,10 +669,10 @@ module.exports = { path: 'area', title: 'Area', }, - // { - // path: 'card', - // title: 'Card', - // }, + { + path: 'card', + title: 'Card', + }, // { // path: 'contact-card', // title: 'Contact',