Skip to content

Commit eb2182a

Browse files
authored
Update alert.js
1 parent cfc0f71 commit eb2182a

File tree

1 file changed

+72
-39
lines changed

1 file changed

+72
-39
lines changed

src/components/alert/alert.js

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import BButtonClose from '../button/button-close'
22
import { getComponentConfig } from '../../utils/config'
3+
import { requestAF } from '../../utils/dom'
34

45
const NAME = 'BAlert'
56

@@ -36,62 +37,58 @@ export default {
3637
data() {
3738
return {
3839
countDownTimerId: null,
39-
dismissed: false
40-
}
41-
},
42-
computed: {
43-
classObject() {
44-
return ['alert', this.alertVariant, this.dismissible ? 'alert-dismissible' : '']
45-
},
46-
alertVariant() {
47-
const variant = this.variant
48-
return `alert-${variant}`
49-
},
50-
localShow() {
51-
return !this.dismissed && (this.countDownTimerId || this.show)
40+
dismissed: false,
41+
localShow: this.show,
42+
showClass: this.fade && this.show
5243
}
5344
},
5445
watch: {
55-
show() {
56-
this.showChanged()
46+
show(newVal) {
47+
this.showChanged(newVal)
48+
},
49+
dismissed(newVal) {
50+
if (newVal) {
51+
this.localShow = false
52+
this.$emit('dismissed')
53+
}
5754
}
5855
},
5956
mounted() {
60-
this.showChanged()
57+
this.showChanged(this.show)
6158
},
6259
destroyed /* istanbul ignore next */() {
6360
this.clearCounter()
6461
},
6562
methods: {
6663
dismiss() {
6764
this.clearCounter()
68-
this.dismissed = true
69-
this.$emit('dismissed')
70-
this.$emit('input', false)
7165
if (typeof this.show === 'number') {
7266
this.$emit('dismiss-count-down', 0)
7367
this.$emit('input', 0)
7468
} else {
7569
this.$emit('input', false)
7670
}
71+
this.dismissed = true
7772
},
7873
clearCounter() {
7974
if (this.countDownTimerId) {
8075
clearInterval(this.countDownTimerId)
8176
this.countDownTimerId = null
8277
}
8378
},
84-
showChanged() {
79+
showChanged(show) {
8580
// Reset counter status
8681
this.clearCounter()
8782
// Reset dismiss status
8883
this.dismissed = false
84+
// Set localShow state
85+
this.localShow = Boolean(show)
8986
// No timer for boolean values
90-
if (this.show === true || this.show === false || this.show === null || this.show === 0) {
87+
if (show === true || show === false || show === null || show === 0) {
9188
return
9289
}
9390
// Start counter (ensure we have an integer value)
94-
let dismissCountDown = parseInt(this.show, 10) || 1
91+
let dismissCountDown = parseInt(show, 10) || 1
9592
this.countDownTimerId = setInterval(() => {
9693
if (dismissCountDown < 1) {
9794
this.dismiss()
@@ -101,30 +98,66 @@ export default {
10198
this.$emit('dismiss-count-down', dismissCountDown)
10299
this.$emit('input', dismissCountDown)
103100
}, 1000)
101+
},
102+
onBeforeEnter() {
103+
if (this.fade) {
104+
// Add show class one frame after inserted, to make transitions work
105+
requestAF(() => {
106+
this.showClass = true
107+
})
108+
}
109+
},
110+
onBeforeLeave() /* istanbul ignore next: does not appear to be called in vue-test-utils */ {
111+
this.showClass = false
104112
}
105113
},
106114
render(h) {
107-
if (!this.localShow) {
108-
// If not showing, render placeholder
109-
return h(false)
110-
}
111-
let dismissBtn = h(false)
112-
if (this.dismissible) {
113-
// Add dismiss button
114-
dismissBtn = h(
115-
'b-button-close',
116-
{ attrs: { 'aria-label': this.dismissLabel }, on: { click: this.dismiss } },
117-
[this.$slots.dismiss]
115+
const $slots = this.$slots
116+
let $alert = h(false)
117+
if (this.localShow) {
118+
let $dismissBtn = h(false)
119+
if (this.dismissible) {
120+
$dismissBtn = h(
121+
'b-button-close',
122+
{ attrs: { 'aria-label': this.dismissLabel }, on: { click: this.dismiss } },
123+
[$slots.dismiss]
124+
)
125+
}
126+
$alert = h(
127+
'div',
128+
{
129+
staticClass: 'alert',
130+
class: {
131+
fade: this.fade,
132+
show: this.showClass,
133+
'alert-dismissible': this.dismissible,
134+
[`alert-${this.variant}`]: this.variant
135+
},
136+
attrs: { role: 'alert', 'aria-live': 'polite', 'aria-atomic': true }
137+
},
138+
[$dismissBtn, $slots.default]
118139
)
140+
$alert = [$alert]
119141
}
120-
const alert = h(
121-
'div',
142+
return h(
143+
'transition',
122144
{
123-
class: this.classObject,
124-
attrs: { role: 'alert', 'aria-live': 'polite', 'aria-atomic': true }
145+
props: {
146+
mode: 'out-in',
147+
// Disable use of built-in transition classes
148+
'enter-class': '',
149+
'enter-active-class': '',
150+
'enter-to-class': '',
151+
'leave-class': 'show',
152+
'leave-active-class': '',
153+
'leave-to-class': ''
154+
},
155+
on: {
156+
beforeEnter: this.onBeforeEnter,
157+
beforeLeave: this.onBeforeLeave
158+
}
125159
},
126-
[dismissBtn, this.$slots.default]
160+
$alert
127161
)
128-
return !this.fade ? alert : h('transition', { props: { name: 'fade', appear: true } }, [alert])
129162
}
130163
}

0 commit comments

Comments
 (0)