Skip to content

Commit f729bbf

Browse files
committed
feat: avatar list
1 parent f9c51ed commit f729bbf

File tree

5 files changed

+105
-26
lines changed

5 files changed

+105
-26
lines changed

src/components/AvatarList/Item.vue

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
11
<template>
2-
<div>
3-
item
4-
</div>
2+
<li :class="[prefixCls, size]">
3+
<slot>
4+
<tooltip>
5+
<template slot="title">{{ tips }}</template>
6+
<avatar :size="size !== 'mini' && size || 20" :src="src" />
7+
</tooltip>
8+
</slot>
9+
</li>
510
</template>
611

712
<script>
13+
import Avatar from 'ant-design-vue/es/avatar'
14+
import Tooltip from 'ant-design-vue/es/tooltip'
15+
816
export default {
917
name: "AvatarItem",
18+
components: {
19+
Avatar,
20+
Tooltip
21+
},
1022
props: {
23+
prefixCls: {
24+
type: String,
25+
default: 'ant-pro-avatar-list-item'
26+
},
1127
tips: {
1228
type: String,
1329
default: '',
1430
required: false
1531
},
1632
src: {
1733
type: String,
18-
required: true
34+
default: ''
35+
}
36+
},
37+
data () {
38+
return {
39+
size: this.$parent.size
40+
}
41+
},
42+
watch: {
43+
'$parent.size' (val) {
44+
this.size = val
1945
}
2046
}
2147
}
22-
</script>
23-
24-
<style scoped>
25-
26-
</style>
48+
</script>

src/components/AvatarList/List.vue

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,79 @@
11
<template>
2-
<div>
3-
2+
<div :class="[prefixCls]">
3+
<ul>
4+
<slot></slot>
5+
<template v-if="maxLength > 0 && slotsSize > maxLength">
6+
<avatar-item :size="size">
7+
<avatar :size="size !== 'mini' && size || 20" :style="excessItemsStyle">{{ `+${maxLength}` }}</avatar>
8+
</avatar-item>
9+
</template>
10+
</ul>
411
</div>
512
</template>
613

714
<script>
815
import Avatar from 'ant-design-vue/es/avatar'
9-
import AvatarListItem from './Item'
16+
import AvatarItem from './Item'
1017
1118
export default {
19+
AvatarItem,
1220
name: "AvatarList",
1321
components: {
1422
Avatar,
15-
AvatarListItem
23+
AvatarItem
1624
},
1725
props: {
26+
prefixCls: {
27+
type: String,
28+
default: 'ant-pro-avatar-list'
29+
},
1830
/**
1931
* 头像大小 类型: large、small 、mini, default
2032
* 默认值: default
2133
*/
2234
size: {
23-
type: String,
35+
type: [String, Number],
2436
default: 'default'
2537
},
2638
/**
2739
* 要显示的最大项目
2840
*/
2941
maxLength: {
3042
type: Number,
31-
default: 3
43+
default: 0
3244
},
3345
/**
34-
* 额外 CSS 风格
46+
* 多余的项目风格
3547
*/
3648
excessItemsStyle: {
3749
type: Object,
38-
default: () => {}
50+
default: () => {
51+
return {
52+
color: '#f56a00',
53+
backgroundColor: '#fde3cf'
54+
}
55+
}
3956
}
4057
},
4158
data () {
4259
return {
43-
60+
slotsSize: 0
61+
}
62+
},
63+
created () {
64+
this.slotsSize = this.$slots.default.length
65+
this.splitSlots()
66+
},
67+
methods: {
68+
splitSlots () {
69+
if (this.maxLength !== 0 && this.slotsSize > this.maxLength) {
70+
this.$slots.default = this.$slots.default.slice(0, this.maxLength)
71+
}
4472
}
4573
}
4674
}
4775
</script>
4876

49-
<style scoped>
77+
<style lang="less" scoped>
5078
5179
</style>

src/components/AvatarList/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import AvatarList from './List'
2+
import "./index.less"
23

34
export default AvatarList

src/components/AvatarList/index.less

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
display: inline-block;
88

99
ul {
10+
list-style: none;
1011
display: inline-block;
11-
margin-left: 8px;
12+
padding: 0;
13+
margin: 0 0 0 8px;
1214
font-size: 0;
1315
}
1416
}
@@ -23,20 +25,21 @@
2325
:global {
2426
.ant-avatar {
2527
border: 1px solid #fff;
28+
cursor: pointer;
2629
}
2730
}
2831

29-
.large {
32+
&.large {
3033
width: @avatar-size-lg;
3134
height: @avatar-size-lg;
3235
}
3336

34-
.small {
37+
&.small {
3538
width: @avatar-size-sm;
3639
height: @avatar-size-sm;
3740
}
3841

39-
.mini {
42+
&.mini {
4043
width: 20px;
4144
height: 20px;
4245

@@ -53,4 +56,5 @@
5356
}
5457
}
5558
}
56-
}
59+
}
60+

src/views/Home.vue

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,44 @@
4343

4444
</a-card>
4545

46-
<a-divider> 例子 </a-divider>
46+
<a-divider> AvatarList </a-divider>
4747

48+
<a-card>
49+
<avatar-list :max-length="3">
50+
<avatar-list-item tips="Jake" src="https://gw.alipayobjects.com/zos/rmsportal/zOsKZmFRdUtvpqCImOVY.png" />
51+
<avatar-list-item tips="Andy" src="https://gw.alipayobjects.com/zos/rmsportal/sfjbOqnsXXJgNCjCzDBL.png" />
52+
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
53+
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
54+
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
55+
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
56+
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
57+
</avatar-list>
58+
59+
<a-divider type="vertical" style="margin: 0 16px" />
60+
61+
<avatar-list size="mini">
62+
<avatar-list-item tips="Jake" src="https://gw.alipayobjects.com/zos/rmsportal/zOsKZmFRdUtvpqCImOVY.png" />
63+
<avatar-list-item tips="Andy" src="https://gw.alipayobjects.com/zos/rmsportal/sfjbOqnsXXJgNCjCzDBL.png" />
64+
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
65+
</avatar-list>
66+
</a-card>
4867
</div>
4968
</template>
5069

5170
<script>
5271
// @ is an alias to /src
5372
5473
import Trend from '@/components/Trend'
74+
import AvatarList from '@/components/AvatarList'
75+
76+
const AvatarListItem = AvatarList.AvatarItem
5577
5678
export default {
5779
name: 'Home',
5880
components: {
59-
Trend
81+
Trend,
82+
AvatarList,
83+
AvatarListItem
6084
}
6185
}
6286
</script>

0 commit comments

Comments
 (0)