forked from N3-components/N3-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n3Alert.vue
executable file
·128 lines (120 loc) · 2.59 KB
/
n3Alert.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<template>
<div
v-show="show"
:class="classObj"
transition="fade"
:style="{width:width}">
<n3-icon class="{{prefixCls}}-alert-icon" :type="iconType" ></n3-icon>
<button v-if="dismissable" type="button" class="{{prefixCls}}-close"
@click="_handleClose" >
<span>×</span>
</button>
<div v-if="description" class="{{prefixCls}}-alert-content">
<h4>{{message}}</h4>
<p><slot></slot></p>
</div>
<span v-else>
<slot></slot>
</span>
</div>
</template>
<script>
import type from './utils/type'
import n3Icon from './n3Icon'
export default {
props: {
type: {
type: String,
default: 'default'
},
dismissable: {
type: Boolean,
default: false
},
show: {
type: Boolean,
default: true,
twoWay: true
},
duration: {
type: Number,
default: 0
},
width: {
type: String
},
small: {
type: Boolean,
default: false
},
placement: {
type: String,
default: ''
},
message: {
type: String
},
description: {
type: Boolean,
default: false
},
onClose: {
type: Function
},
prefixCls: {
type: String,
default: 'n3'
}
},
methods: {
_handleClose (e) {
this.show = false
if (type.isFunction(this.onClose)) {
this.onClose()
}
}
},
watch: {
show (val) {
if (val) {
if (this.placement === 'top' || this.placement === 'bottom') {
this.$el.style.marginLeft = -1 * (this.$el.offsetWidth / 2) + 'px'
} else if (this.placement === 'center') {
this.$el.style.marginLeft = -1 * (this.$el.offsetWidth / 2) + 'px'
this.$el.style.marginTop = -1 * (this.$el.offsetHeight / 2) + 'px'
}
}
if (this._timeout) clearTimeout(this._timeout)
if (val && !!this.duration) {
this._timeout = setTimeout(() => {
this.show = false
}, this.duration)
}
}
},
computed: {
iconType () {
let {type} = this
let map = {
success: 'check-circle-o',
danger: 'times-circle-o',
warning: 'exclamation-circle',
info: 'info-circle'
}
return map[type]
},
classObj () {
let {prefixCls, type, placement, small} = this
let klass = {}
klass[prefixCls + '-alert-small'] = small
klass[prefixCls + '-alert'] = true
klass[prefixCls + '-alert-' + type] = true
klass[prefixCls + '-' + placement] = true
return klass
}
},
components: {
n3Icon
}
}
</script>