Commit d71d2977 by Sendya

fix: avatar list

parent a4ebcc5b
<template> <template>
<li :class="[prefixCls, size]"> <tooltip v-if="tips !== ''">
<slot> <template slot="title">{{ tips }}</template>
<tooltip> <avatar :size="avatarSize" :src="src" />
<template slot="title">{{ tips }}</template> </tooltip>
<avatar :size="size !== 'mini' && size || 20" :src="src" /> <avatar v-else :size="avatarSize" :src="src" />
</tooltip>
</slot>
</li>
</template> </template>
<script> <script>
...@@ -20,10 +17,6 @@ ...@@ -20,10 +17,6 @@
Tooltip Tooltip
}, },
props: { props: {
prefixCls: {
type: String,
default: 'ant-pro-avatar-list-item'
},
tips: { tips: {
type: String, type: String,
default: '', default: '',
...@@ -39,6 +32,11 @@ ...@@ -39,6 +32,11 @@
size: this.$parent.size size: this.$parent.size
} }
}, },
computed: {
avatarSize () {
return this.size !== 'mini' && this.size || 20
}
},
watch: { watch: {
'$parent.size' (val) { '$parent.size' (val) {
this.size = val this.size = val
......
<!--
<template> <template>
<div :class="[prefixCls]"> <div :class="[prefixCls]">
<ul> <ul>
<slot></slot> <slot></slot>
<template v-if="maxLength > 0 && slotsSize > maxLength"> <template v-for="item in filterEmpty($slots.default).slice(0, 3)"></template>
<template v-if="maxLength > 0 && filterEmpty($slots.default).length > maxLength">
<avatar-item :size="size"> <avatar-item :size="size">
<avatar :size="size !== 'mini' && size || 20" :style="excessItemsStyle">{{ `+${maxLength}` }}</avatar> <avatar :size="size !== 'mini' && size || 20" :style="excessItemsStyle">{{ `+${maxLength}` }}</avatar>
</avatar-item> </avatar-item>
...@@ -10,10 +14,12 @@ ...@@ -10,10 +14,12 @@
</ul> </ul>
</div> </div>
</template> </template>
-->
<script> <script>
import Avatar from 'ant-design-vue/es/avatar' import Avatar from 'ant-design-vue/es/avatar'
import AvatarItem from './Item' import AvatarItem from './Item'
import { filterEmpty } from '@/components/_util/util'
export default { export default {
AvatarItem, AvatarItem,
...@@ -56,24 +62,39 @@ ...@@ -56,24 +62,39 @@
} }
}, },
data () { data () {
return { return {}
slotsSize: 0
}
},
created () {
this.slotsSize = this.$slots.default.length
this.splitSlots()
}, },
methods: { methods: {
splitSlots () { getItems(items) {
if (this.maxLength !== 0 && this.slotsSize > this.maxLength) { const classString = {
this.$slots.default = this.$slots.default.slice(0, this.maxLength) [`${this.prefixCls}-item`]: true,
[`${this.size}`]: true
}
if (this.maxLength > 0) {
items = items.slice(0, this.maxLength)
items.push((<Avatar size={ this.size } style={ this.excessItemsStyle }>{`+${this.maxLength}`}</Avatar>))
} }
const itemList = items.map((item) => (
<li class={ classString }>{ item }</li>
))
return itemList
} }
},
render () {
const { prefixCls, size } = this.$props
const classString = {
[`${prefixCls}`]: true,
[`${size}`]: true,
}
const items = filterEmpty(this.$slots.default)
const itemsDom = items && items.length ? <ul class={`${prefixCls}-items`}>{ this.getItems(items) }</ul> : null
return (
<div class={ classString }>
{ itemsDom }
</div>
)
} }
} }
</script> </script>
\ No newline at end of file
<style lang="less" scoped>
</style>
\ No newline at end of file
<template>
<span>
{{ lastTime | format }}
</span>
</template>
<script>
function fixedZero(val) {
return val * 1 < 10 ? `0${val}` : val;
}
export default {
name: "CountDown",
props: {
format: {
type: Function,
default: undefined
},
target: {
type: [Date, Number],
required: true,
},
onEnd: {
type: Function,
default: () => {
}
}
},
data() {
return {
dateTime: '0',
originTargetTime: 0,
lastTime: 0,
timer: 0,
interval: 1000
}
},
filters: {
format(time) {
const hours = 60 * 60 * 1000;
const minutes = 60 * 1000;
const h = Math.floor(time / hours);
const m = Math.floor((time - h * hours) / minutes);
const s = Math.floor((time - h * hours - m * minutes) / 1000);
return `${fixedZero(h)}:${fixedZero(m)}:${fixedZero(s)}`
}
},
created() {
this.initTime()
this.tick()
},
methods: {
initTime() {
let lastTime = 0;
let targetTime = 0;
this.originTargetTime = this.target
try {
if (Object.prototype.toString.call(this.target) === '[object Date]') {
targetTime = this.target
} else {
targetTime = new Date(this.target).getTime()
}
} catch (e) {
throw new Error('invalid target prop')
}
lastTime = targetTime - new Date().getTime();
this.lastTime = lastTime < 0 ? 0 : lastTime
},
tick() {
const {onEnd} = this
this.timer = setTimeout(() => {
if (this.lastTime < this.interval) {
clearTimeout(this.timer)
this.lastTime = 0
if (typeof onEnd === 'function') {
onEnd();
}
} else {
this.lastTime -= this.interval
this.tick()
}
}, this.interval)
}
},
beforeUpdate () {
if (this.originTargetTime !== this.target) {
this.initTime()
}
},
beforeDestroy() {
clearTimeout(this.timer)
}
}
</script>
<style scoped>
</style>
\ No newline at end of file
import CountDown from './CountDown'
export default CountDown
\ No newline at end of file
/**
* components util
*/
/**
* 清理空值,对象
* @param children
* @returns {*[]}
*/
export function filterEmpty (children = []) {
return children.filter(c => c.tag || (c.text && c.text.trim() !== ''))
}
\ No newline at end of file
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" /> <avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" /> <avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" /> <avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
</avatar-list> </avatar-list>
<a-divider type="vertical" style="margin: 0 16px" /> <a-divider type="vertical" style="margin: 0 16px" />
...@@ -64,6 +65,16 @@ ...@@ -64,6 +65,16 @@
<avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" /> <avatar-list-item tips="Niko" src="https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png" />
</avatar-list> </avatar-list>
</a-card> </a-card>
<a-divider> CountDown </a-divider>
<a-card>
<count-down
style="font-size: 2rem"
:target="new Date().getTime() + 3000"
:on-end="onEndHandle">
</count-down>
</a-card>
</div> </div>
</template> </template>
...@@ -72,15 +83,27 @@ ...@@ -72,15 +83,27 @@
import Trend from '@/components/Trend' import Trend from '@/components/Trend'
import AvatarList from '@/components/AvatarList' import AvatarList from '@/components/AvatarList'
import CountDown from '@/components/CountDown/CountDown'
const AvatarListItem = AvatarList.AvatarItem const AvatarListItem = AvatarList.AvatarItem
export default { export default {
name: 'Home', name: 'Home',
components: { components: {
CountDown,
Trend, Trend,
AvatarList, AvatarList,
AvatarListItem AvatarListItem
},
data () {
return {
targetTime: new Date().getTime() + 3900000
}
},
methods: {
onEndHandle () {
this.$message.success('CountDown callback!!!')
}
} }
} }
</script> </script>
...@@ -89,10 +112,11 @@ ...@@ -89,10 +112,11 @@
.home { .home {
width: 900px; width: 900px;
margin: 0 auto; margin: 0 auto;
padding: 25px 0;
} }
.home > .banner { .home > .banner {
text-align: center; text-align: center;
padding-top: 25px; padding: 25px 0;
margin: 25px 0; margin: 25px 0;
} }
</style> </style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment