blob: e85b133ae4623769ddfeb4e739a1e22460d2f632 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view v-if="show" class="u-badge" :class="[
3 isDot ? 'u-badge-dot' : '',
4 size == 'mini' ? 'u-badge-mini' : '',
5 type ? 'u-badge--bg--' + type : ''
6 ]" :style="[{
7 top: offset[0] + 'rpx',
8 right: offset[1] + 'rpx',
9 fontSize: fontSize + 'rpx',
10 position: absolute ? 'absolute' : 'static',
11 color: color,
12 backgroundColor: bgColor
13 }, boxStyle]"
14 >
15 {{showText}}
16 </view>
17</template>
18
19<script>
20 /**
21 * badge 角标
22 * @description 本组件一般用于展示头像的地方,如个人中心,或者评论列表页的用户头像展示等场所。
23 * @tutorial https://www.uviewui.com/components/badge.html
24 * @property {String Number} count 展示的数字,大于 overflowCount 时显示为 ${overflowCount}+,为0且show-zero为false时隐藏
25 * @property {Boolean} is-dot 不展示数字,只有一个小点(默认false)
26 * @property {Boolean} absolute 组件是否绝对定位,为true时,offset参数才有效(默认true)
27 * @property {String Number} overflow-count 展示封顶的数字值(默认99)
28 * @property {String} type 使用预设的背景颜色(默认error)
29 * @property {Boolean} show-zero 当数值为 0 时,是否展示 Badge(默认false)
30 * @property {String} size Badge的尺寸,设为mini会得到小一号的Badge(默认default)
31 * @property {Array} offset 设置badge的位置偏移,格式为 [x, y],也即设置的为top和right的值,单位rpx。absolute为true时有效(默认[20, 20])
32 * @property {String} color 字体颜色(默认#ffffff)
33 * @property {String} bgColor 背景颜色,优先级比type高,如设置,type参数会失效
34 * @property {Boolean} is-center 组件中心点是否和父组件右上角重合,优先级比offset高,如设置,offset参数会失效(默认false)
35 * @example <u-badge type="error" count="7"></u-badge>
36 */
37 export default {
38 name: 'u-badge',
39 props: {
40 // primary,warning,success,error,info
41 type: {
42 type: String,
43 default: 'error'
44 },
45 // default, mini
46 size: {
47 type: String,
48 default: 'default'
49 },
50 //是否是圆点
51 isDot: {
52 type: Boolean,
53 default: false
54 },
55 // 显示的数值内容
56 count: {
57 type: [Number, String],
58 },
59 // 展示封顶的数字值
60 overflowCount: {
61 type: Number,
62 default: 99
63 },
64 // 当数值为 0 时,是否展示 Badge
65 showZero: {
66 type: Boolean,
67 default: false
68 },
69 // 位置偏移
70 offset: {
71 type: Array,
72 default: () => {
73 return [20, 20]
74 }
75 },
76 // 是否开启绝对定位,开启了offset才会起作用
77 absolute: {
78 type: Boolean,
79 default: true
80 },
81 // 字体大小
82 fontSize: {
83 type: [String, Number],
84 default: '24'
85 },
86 // 字体演示
87 color: {
88 type: String,
89 default: '#ffffff'
90 },
91 // badge的背景颜色
92 bgColor: {
93 type: String,
94 default: ''
95 },
96 // 是否让badge组件的中心点和父组件右上角重合,配置的话,offset将会失效
97 isCenter: {
98 type: Boolean,
99 default: false
100 }
101 },
102 computed: {
103 // 是否将badge中心与父组件右上角重合
104 boxStyle() {
105 let style = {};
106 if(this.isCenter) {
107 style.top = 0;
108 style.right = 0;
109 // Y轴-50%,意味着badge向上移动了badge自身高度一半,X轴50%,意味着向右移动了自身宽度一半
110 style.transform = "translateY(-50%) translateX(50%)";
111 } else {
112 style.top = this.offset[0] + 'rpx';
113 style.right = this.offset[1] + 'rpx';
114 style.transform = "translateY(0) translateX(0)";
115 }
116 // 如果尺寸为mini,后接上scal()
117 if(this.size == 'mini') {
118 style.transform = style.transform + " scale(0.8)";
119 }
120 return style;
121 },
122 // isDot类型时,不显示文字
123 showText() {
124 if(this.isDot) return '';
125 else {
126 if(this.count > this.overflowCount) return `${this.overflowCount}+`;
127 else return this.count;
128 }
129 },
130 // 是否显示组件
131 show() {
132 // 如果count的值为0,并且showZero设置为false,不显示组件
133 if(this.count == 0 && this.showZero == false) return false;
134 else return true;
135 }
136 }
137 }
138</script>
139
140<style lang="scss" scoped>
141 @import "../../libs/css/style.components.scss";
142
143 .u-badge {
144 /* #ifndef APP-NVUE */
145 display: inline-flex;
146 /* #endif */
147 justify-content: center;
148 align-items: center;
149 line-height: 24rpx;
150 padding: 4rpx 8rpx;
151 border-radius: 100rpx;
152 z-index: 9;
153
154 &--bg--primary {
155 background-color: $u-type-primary;
156 }
157
158 &--bg--error {
159 background-color: $u-type-error;
160 }
161
162 &--bg--success {
163 background-color: $u-type-success;
164 }
165
166 &--bg--info {
167 background-color: $u-type-info;
168 }
169
170 &--bg--warning {
171 background-color: $u-type-warning;
172 }
173 }
174
175 .u-badge-dot {
176 height: 16rpx;
177 width: 16rpx;
178 border-radius: 100rpx;
179 line-height: 1;
180 }
181
182 .u-badge-mini {
183 transform: scale(0.8);
184 transform-origin: center center;
185 }
186
187 // .u-primary {
188 // background: $u-type-primary;
189 // color: #fff;
190 // }
191
192 // .u-error {
193 // background: $u-type-error;
194 // color: #fff;
195 // }
196
197 // .u-warning {
198 // background: $u-type-warning;
199 // color: #fff;
200 // }
201
202 // .u-success {
203 // background: $u-type-success;
204 // color: #fff;
205 // }
206
207 // .u-black {
208 // background: #585858;
209 // color: #fff;
210 // }
211
212 .u-info {
213 background-color: $u-type-info;
214 color: #fff;
215 }
216</style>