forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
147 lines (130 loc) · 3.31 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { use, isDef } from '../utils';
import { inherit } from '../utils/functional';
import Tag from '../tag';
// Types
import { CreateElement, RenderContext } from 'vue/types';
import { DefaultSlots, ScopedSlot } from '../utils/use/sfc';
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<CardProps>
) {
const { thumb } = props;
const showThumb = slots.thumb || thumb;
const showTag = slots.tag || props.tag;
const showNum = slots.num || isDef(props.num);
const showPrice = slots.price || isDef(props.price);
const showOriginPrice = slots['origin-price'] || isDef(props.originPrice);
const Thumb = showThumb && (
<a href={props.thumbLink} class={bem('thumb')}>
{slots.thumb ? (
slots.thumb()
) : props.lazyLoad ? (
<img class={bem('img')} vLazy={thumb} />
) : (
<img class={bem('img')} src={thumb} />
)}
{showTag && (
<div class={bem('tag')}>
{slots.tag ? (
slots.tag()
) : (
<Tag mark type="danger">
{props.tag}
</Tag>
)}
</div>
)}
</a>
);
const Title = slots.title
? slots.title()
: props.title && <div class={bem('title')}>{props.title}</div>;
const Desc = slots.desc
? slots.desc()
: props.desc && (
<div class={[bem('desc'), 'van-ellipsis']}>{props.desc}</div>
);
const Price = showPrice && (
<div class={bem('price')}>
{slots.price ? slots.price() : `${props.currency} ${props.price}`}
</div>
);
const OriginPrice = showOriginPrice && (
<div class={bem('origin-price')}>
{slots['origin-price']
? slots['origin-price']()
: `${props.currency} ${props.originPrice}`}
</div>
);
const Num = showNum && (
<div class={bem('num')}>{slots.num ? slots.num() : `x ${props.num}`}</div>
);
const Footer = slots.footer && (
<div class={bem('footer')}>{slots.footer()}</div>
);
return (
<div class={bem()} {...inherit(ctx, true)}>
<div class={bem('header')}>
{Thumb}
<div class={bem('content', { centered: props.centered })}>
{Title}
{Desc}
{slots.tags && slots.tags()}
<div class="van-card__bottom">
{Price}
{OriginPrice}
{Num}
</div>
</div>
</div>
{Footer}
</div>
);
}
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<CardProps, CardEvents>(Card);