blob: e81fc37979e9e1a5d9c9cbbc4e1abd2a2e888eba [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view class="u-alert-tips" v-if="show" :class="[
3 !show ? 'u-close-alert-tips': '',
4 type ? 'u-alert-tips--bg--' + type + '-light' : '',
5 type ? 'u-alert-tips--border--' + type + '-disabled' : '',
6 ]" :style="{
7 backgroundColor: bgColor,
8 borderColor: borderColor
9 }">
10 <view class="u-icon-wrap">
11 <u-icon v-if="showIcon" :name="uIcon" :size="description ? 40 : 32" class="u-icon" :color="uIconType" :custom-style="iconStyle"></u-icon>
12 </view>
13 <view class="u-alert-content" @tap.stop="click">
14 <view class="u-alert-title" :style="[uTitleStyle]">
15 {{title}}
16 </view>
17 <view v-if="description" class="u-alert-desc" :style="[descStyle]">
18 {{description}}
19 </view>
20 </view>
21 <view class="u-icon-wrap">
22 <u-icon @click="close" v-if="closeAble && !closeText" hoverClass="u-type-error-hover-color" name="close" color="#c0c4cc"
23 :size="22" class="u-close-icon" :style="{
24 top: description ? '18rpx' : '24rpx'
25 }"></u-icon>
26 </view>
27 <text v-if="closeAble && closeText" class="u-close-text" :style="{
28 top: description ? '18rpx' : '24rpx'
29 }">{{closeText}}</text>
30 </view>
31</template>
32
33<script>
34 /**
35 * alertTips 警告提示
36 * @description 警告提示,展现需要关注的信息
37 * @tutorial https://uviewui.com/components/alertTips.html
38 * @property {String} title 显示的标题文字
39 * @property {String} description 辅助性文字,颜色比title浅一点,字号也小一点,可选
40 * @property {String} type 关闭按钮(默认为叉号icon图标)
41 * @property {String} icon 图标名称
42 * @property {Object} icon-style 图标的样式,对象形式
43 * @property {Object} title-style 标题的样式,对象形式
44 * @property {Object} desc-style 描述的样式,对象形式
45 * @property {String} close-able 用文字替代关闭图标,close-able为true时有效
46 * @property {Boolean} show-icon 是否显示左边的辅助图标
47 * @property {Boolean} show 显示或隐藏组件
48 * @event {Function} click 点击组件时触发
49 * @event {Function} close 点击关闭按钮时触发
50 */
51 export default {
52 name: 'u-alert-tips',
53 props: {
54 // 显示文字
55 title: {
56 type: String,
57 default: ''
58 },
59 // 主题,success/warning/info/error
60 type: {
61 type: String,
62 default: 'warning'
63 },
64 // 辅助性文字
65 description: {
66 type: String,
67 default: ''
68 },
69 // 是否可关闭
70 closeAble: {
71 type: Boolean,
72 default: false
73 },
74 // 关闭按钮自定义文本
75 closeText: {
76 type: String,
77 default: ''
78 },
79 // 是否显示图标
80 showIcon: {
81 type: Boolean,
82 default: false
83 },
84 // 文字颜色,如果定义了color值,icon会失效
85 color: {
86 type: String,
87 default: ''
88 },
89 // 背景颜色
90 bgColor: {
91 type: String,
92 default: ''
93 },
94 // 边框颜色
95 borderColor: {
96 type: String,
97 default: ''
98 },
99 // 是否显示
100 show: {
101 type: Boolean,
102 default: true
103 },
104 // 左边显示的icon
105 icon: {
106 type: String,
107 default: ''
108 },
109 // icon的样式
110 iconStyle: {
111 type: Object,
112 default() {
113 return {}
114 }
115 },
116 // 标题的样式
117 titleStyle: {
118 type: Object,
119 default() {
120 return {}
121 }
122 },
123 // 描述文字的样式
124 descStyle: {
125 type: Object,
126 default() {
127 return {}
128 }
129 },
130 },
131 data() {
132 return {
133 }
134 },
135 computed: {
136 uTitleStyle() {
137 let style = {};
138 // 如果有描述文字的话,标题进行加粗
139 style.fontWeight = this.description ? 500 : 'normal';
140 // 将用户传入样式对象和style合并,传入的优先级比style高,同属性会被覆盖
141 return this.$u.deepMerge(style, this.titleStyle);
142 },
143 uIcon() {
144 // 如果有设置icon名称就使用,否则根据type主题,推定一个默认的图标
145 return this.icon ? this.icon : this.$u.type2icon(this.type);
146 },
147 uIconType() {
148 // 如果有设置图标的样式,优先使用,没有的话,则用type的样式
149 return Object.keys(this.iconStyle).length ? '' : this.type;
150 }
151 },
152 methods: {
153 // 点击内容
154 click() {
155 this.$emit('click');
156 },
157 // 点击关闭按钮
158 close() {
159 this.$emit('close');
160 }
161 }
162 }
163</script>
164
165<style lang="scss" scoped>
166 @import "../../libs/css/style.components.scss";
167
168 .u-alert-tips {
169 @include vue-flex;
170 align-items: center;
171 padding: 16rpx 30rpx;
172 border-radius: 8rpx;
173 position: relative;
174 transition: all 0.3s linear;
175 border: 1px solid #fff;
176
177 &--bg--primary-light {
178 background-color: $u-type-primary-light;
179 }
180
181 &--bg--info-light {
182 background-color: $u-type-info-light;
183 }
184
185 &--bg--success-light {
186 background-color: $u-type-success-light;
187 }
188
189 &--bg--warning-light {
190 background-color: $u-type-warning-light;
191 }
192
193 &--bg--error-light {
194 background-color: $u-type-error-light;
195 }
196
197 &--border--primary-disabled {
198 border-color: $u-type-primary-disabled;
199 }
200
201 &--border--success-disabled {
202 border-color: $u-type-success-disabled;
203 }
204
205 &--border--error-disabled {
206 border-color: $u-type-error-disabled;
207 }
208
209 &--border--warning-disabled {
210 border-color: $u-type-warning-disabled;
211 }
212
213 &--border--info-disabled {
214 border-color: $u-type-info-disabled;
215 }
216 }
217
218 .u-close-alert-tips {
219 opacity: 0;
220 visibility: hidden;
221 }
222
223 .u-icon {
224 margin-right: 16rpx;
225 }
226
227 .u-alert-title {
228 font-size: 28rpx;
229 color: $u-main-color;
230 }
231
232 .u-alert-desc {
233 font-size: 26rpx;
234 text-align: left;
235 color: $u-content-color;
236 }
237
238 .u-close-icon {
239 position: absolute;
240 top: 20rpx;
241 right: 20rpx;
242 }
243
244 .u-close-hover {
245 color: red;
246 }
247
248 .u-close-text {
249 font-size: 24rpx;
250 color: $u-tips-color;
251 position: absolute;
252 top: 20rpx;
253 right: 20rpx;
254 line-height: 1;
255 }
256</style>