add Avatar component

This commit is contained in:
梁灏 2017-08-15 14:52:56 +08:00
parent ececc3bb4c
commit ad22557812
2 changed files with 65 additions and 26 deletions

View file

@ -14,9 +14,13 @@
<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" shape="square"></Avatar> <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" shape="square"></Avatar>
<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" size="small" shape="square"></Avatar> <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" size="small" shape="square"></Avatar>
<br><br> <br><br>
<Avatar>Aresn</Avatar> <Avatar size="large">Leo</Avatar>
<Avatar>U</Avatar> <Avatar size="large">A</Avatar>
<Avatar>Tomserm</Avatar> <Avatar size="default">A</Avatar>
<Avatar size="small">A</Avatar>
<Avatar size="large">Tomserm</Avatar>
<Avatar size="large">{{ name }}</Avatar>
{{ name }}
<br><br> <br><br>
<Badge dot> <Badge dot>
<Avatar icon="person" shape="square"></Avatar> <Avatar icon="person" shape="square"></Avatar>
@ -24,10 +28,20 @@
<Badge :count="3"> <Badge :count="3">
<Avatar icon="person" shape="square"></Avatar> <Avatar icon="person" shape="square"></Avatar>
</Badge> </Badge>
<Button @click="change">change</Button>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
data () {
return {
name: 'Aresn'
}
},
methods: {
change () {
this.name = 'Tomserm'
}
}
} }
</script> </script>

View file

@ -1,3 +1,10 @@
<template>
<span :class="classes">
<img :src="src" v-if="src">
<Icon :type="icon" v-else-if="icon"></Icon>
<span ref="children" :class="[prefixCls + '-string']" :style="childrenStyle" v-else><slot></slot></span>
</span>
</template>
<script> <script>
import Icon from '../icon'; import Icon from '../icon';
import { oneOf } from '../../utils/assist'; import { oneOf } from '../../utils/assist';
@ -6,6 +13,7 @@
export default { export default {
name: 'Avatar', name: 'Avatar',
components: { Icon },
props: { props: {
shape: { shape: {
validator (value) { validator (value) {
@ -26,6 +34,13 @@
type: String type: String
} }
}, },
data () {
return {
prefixCls: prefixCls,
scale: 1,
isSlotShow: false
};
},
computed: { computed: {
classes () { classes () {
return [ return [
@ -37,32 +52,42 @@
[`${prefixCls}-icon`]: !!this.icon [`${prefixCls}-icon`]: !!this.icon
} }
]; ];
},
childrenStyle () {
let style = {};
if (this.isSlotShow) {
style = {
msTransform: `scale(${this.scale})`,
WebkitTransform: `scale(${this.scale})`,
transform: `scale(${this.scale})`,
position: 'absolute',
display: 'inline-block',
left: `calc(50% - ${Math.round(this.$refs.children.offsetWidth / 2)}px)`
};
}
return style;
} }
}, },
render (h) { methods: {
let innerNode = ''; setScale () {
this.isSlotShow = !this.src && !this.icon;
if (this.src) { if (this.$slots.default) {
innerNode = h('img', { const childrenWidth = this.$refs.children.offsetWidth;
attrs: { const avatarWidth = this.$el.getBoundingClientRect().width;
src: this.src // add 4px gap for each side to get better performance
if (avatarWidth - 8 < childrenWidth) {
this.scale = (avatarWidth - 8) / childrenWidth;
} else {
this.scale = 1;
} }
}); }
} else if (this.icon) {
innerNode = h(Icon, {
props: {
type: this.icon
}
});
} else if (this.$slots.default) {
innerNode = h('span', {
'class': `${prefixCls}-string`
}, this.$slots.default);
} }
},
return h('span', { mounted () {
'class': this.classes this.setScale();
}, [innerNode]); },
updated () {
this.setScale();
} }
}; };
</script> </script>