Skip to content

Commit 1521058

Browse files
committed
refactor: AvatarList. closed #839 #840
1 parent 4723fc5 commit 1521058

File tree

7 files changed

+105
-149
lines changed

7 files changed

+105
-149
lines changed

src/components/AvatarList/Item.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import PropTypes from 'ant-design-vue/es/_util/vue-types'
2+
import { Tooltip, Avatar } from 'ant-design-vue'
3+
import { getSlotOptions } from 'ant-design-vue/lib/_util/props-util'
4+
import { warning } from 'ant-design-vue/lib/vc-util/warning'
5+
6+
export const AvatarListItemProps = {
7+
tips: PropTypes.string.def(null),
8+
src: PropTypes.string.def('')
9+
}
10+
11+
const Item = {
12+
__ANT_AVATAR_CHILDREN: true,
13+
name: 'AvatarListItem',
14+
props: AvatarListItemProps,
15+
created () {
16+
warning(getSlotOptions(this.$parent).__ANT_AVATAR_LIST, 'AvatarListItem must be a subcomponent of AvatarList')
17+
},
18+
render () {
19+
const AvatarDom = <Avatar size={this.$parent.size} src={this.src} />
20+
return this.tips && <Tooltip title={this.tips}>{AvatarDom}</Tooltip> || <AvatarDom />
21+
}
22+
}
23+
24+
export default Item

src/components/AvatarList/Item.vue

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/components/AvatarList/List.jsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import './index.less'
2+
3+
import PropTypes from 'ant-design-vue/es/_util/vue-types'
4+
import Avatar from 'ant-design-vue/es/avatar'
5+
import Item from './Item.jsx'
6+
import { filterEmpty } from '@/components/_util/util'
7+
8+
/**
9+
* size: `number`、 `large`、`small`、`default` 默认值: default
10+
* maxLength: number
11+
* excessItemsStyle: CSSProperties
12+
*/
13+
const AvatarListProps = {
14+
prefixCls: PropTypes.string.def('ant-pro-avatar-list'),
15+
size: {
16+
validator: val => {
17+
return typeof val === 'number' || ['small', 'large', 'default'].includes(val)
18+
},
19+
default: 'default'
20+
},
21+
maxLength: PropTypes.number.def(0),
22+
excessItemsStyle: PropTypes.object.def({
23+
color: '#f56a00',
24+
backgroundColor: '#fde3cf'
25+
})
26+
}
27+
28+
const AvatarList = {
29+
__ANT_AVATAR_LIST: true,
30+
Item,
31+
name: 'AvatarList',
32+
props: AvatarListProps,
33+
render (h) {
34+
const { prefixCls, size } = this.$props
35+
const className = {
36+
[`${prefixCls}`]: true,
37+
[`${size}`]: true
38+
}
39+
const items = filterEmpty(this.$slots.default)
40+
const itemsDom = items && items.length ? <ul class={`${prefixCls}-items`}>{this.getItems(items)}</ul> : null
41+
42+
return (
43+
<div class={className}>
44+
{itemsDom}
45+
</div>
46+
)
47+
},
48+
methods: {
49+
getItems (items) {
50+
const className = {
51+
[`${this.prefixCls}-item`]: true,
52+
[`${this.size}`]: true
53+
}
54+
const totalSize = items.length
55+
56+
if (this.maxLength > 0) {
57+
items = items.slice(0, this.maxLength)
58+
items.push((<Avatar size={this.size} style={this.excessItemsStyle}>{`+${totalSize - this.maxLength}`}</Avatar>))
59+
}
60+
return items.map((item) => (
61+
<li class={className}>{item}</li>
62+
))
63+
}
64+
}
65+
}
66+
67+
AvatarList.install = function (Vue) {
68+
Vue.component(AvatarList.name, AvatarList)
69+
Vue.component(AvatarList.Item.name, AvatarList.Item)
70+
}
71+
72+
export default AvatarList

src/components/AvatarList/List.vue

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/components/AvatarList/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import AvatarList from './List'
2-
import './index.less'
2+
import Item from './Item'
3+
4+
export {
5+
AvatarList,
6+
Item as AvatarListItem
7+
}
38

49
export default AvatarList

src/components/AvatarList/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
```javascript
1111
import AvatarList from '@/components/AvatarList'
12-
const AvatarListItem = AvatarList.AvatarItem
12+
const AvatarListItem = AvatarList.Item
1313

1414
export default {
1515
components: {

src/views/list/search/Projects.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<div class="cardItemContent">
6565
<span>{{ item.updatedAt | fromNow }}</span>
6666
<div class="avatarList">
67-
<avatar-list size="mini">
67+
<avatar-list size="small" :max-length="2">
6868
<avatar-list-item
6969
v-for="(member, i) in item.members"
7070
:key="`${item.id}-avatar-${i}`"
@@ -85,7 +85,7 @@
8585
import moment from 'moment'
8686
import { TagSelect, StandardFormRow, Ellipsis, AvatarList } from '@/components'
8787
const TagSelectOption = TagSelect.Option
88-
const AvatarListItem = AvatarList.AvatarItem
88+
const AvatarListItem = AvatarList.Item
8989
9090
export default {
9191
components: {

0 commit comments

Comments
 (0)