Skip to content

chore(alert): code refactoring (closes #2967) #2969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bf9c10b
Update alert.js
tmorehouse Mar 31, 2019
481e2ec
Update alert.js
tmorehouse Mar 31, 2019
a8494f2
Update alert.js
tmorehouse Mar 31, 2019
0104df8
Update alert.spec.js
tmorehouse Mar 31, 2019
9e03246
Update alert.spec.js
tmorehouse Mar 31, 2019
1c1af67
Update alert.js
tmorehouse Mar 31, 2019
e89a78e
Update alert.js
tmorehouse Mar 31, 2019
88d7528
Update alert.spec.js
tmorehouse Mar 31, 2019
3048c64
Update alert.js
tmorehouse Mar 31, 2019
cae8a8b
Update alert.js
tmorehouse Mar 31, 2019
defe871
Update alert.js
tmorehouse Mar 31, 2019
bd9eb13
Update alert.js
tmorehouse Mar 31, 2019
cac4ec9
add support for countodown number (via `show` prop) as a string number
tmorehouse Mar 31, 2019
ae0ca89
Update alert.js
tmorehouse Mar 31, 2019
0c8ceb6
Update alert.spec.js
tmorehouse Mar 31, 2019
38d7009
Update alert.spec.js
tmorehouse Mar 31, 2019
82e080e
Update alert.spec.js
tmorehouse Mar 31, 2019
ba68022
Update alert.spec.js
tmorehouse Mar 31, 2019
b20b2c1
Merge branch 'dev' into tmorehouse/alert
jacobmllr95 Mar 31, 2019
a7d5399
Update alert.spec.js
tmorehouse Mar 31, 2019
b3cbcfd
Update alert.spec.js
tmorehouse Mar 31, 2019
0a937e4
Update alert.js
tmorehouse Mar 31, 2019
db1811e
Update alert.js
tmorehouse Mar 31, 2019
d6184b8
Update alert.js
tmorehouse Mar 31, 2019
ced08e6
Update alert.js
tmorehouse Mar 31, 2019
218a2d9
Update alert.spec.js
tmorehouse Mar 31, 2019
2a5d280
Update alert.js
tmorehouse Mar 31, 2019
20ac296
Update package.json
tmorehouse Mar 31, 2019
378062d
Update README.md
tmorehouse Mar 31, 2019
66045e7
Update alert.spec.js
tmorehouse Mar 31, 2019
e9b2b06
Update alert.js
tmorehouse Mar 31, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/alert/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ Use the `fade` prop to enable animation. By default alerts are not animated.
dismissible
fade
variant="warning"
@dismissed="dismissCountDown=0"
@dismiss-count-down="countDownChanged"
>
This alert will dismiss after {{ dismissCountDown }} seconds...
Expand Down
121 changes: 73 additions & 48 deletions src/components/alert/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@ import { requestAF } from '../../utils/dom'

const NAME = 'BAlert'

// Convert `show` value to a number
const parseCountDown = show => {
if (show === '' || typeof show === 'boolean') {
return 0
}
show = parseInt(show, 10)
return show > 0 ? show : 0
}

// Convert `show` value to a boolean
const parseShow = show => {
if (show === '' || show === true) {
return true
}
if (parseInt(show, 10) < 1) {
// Boolean will always return false for the above comparison
return false
}
return Boolean(show)
}

// Is a value number like (i.e. a number or a number as string)
const isNumericLike = value => !isNaN(parseInt(value, 10))

// @vue/component
export default {
name: NAME,
components: { BButtonClose },
model: {
prop: 'show',
event: 'input'
Expand All @@ -26,7 +49,7 @@ export default {
default: () => getComponentConfig(NAME, 'dismissLabel')
},
show: {
type: [Boolean, Number],
type: [Boolean, Number, String],
default: false
},
fade: {
Expand All @@ -37,71 +60,74 @@ export default {
data() {
return {
countDownTimerId: null,
dismissed: false,
localShow: this.show,
countDown: 0,
// If initially shown, we need to set these for SSR
localShow: parseShow(this.show),
showClass: this.fade && this.show
}
},
watch: {
show(newVal) {
this.showChanged(newVal)
this.countDown = parseCountDown(newVal)
this.localShow = parseShow(newVal)
},
countDown(newVal) {
this.clearTimer()
this.$emit('dismiss-count-down', newVal)
if (this.show !== newVal) {
// Update the v-model if needed
this.$emit('input', newVal)
}
if (newVal > 0) {
this.localShow = true
this.countDownTimerId = setTimeout(() => {
this.countDown--
}, 1000)
} else {
// Slightly delay the hide to allow any UI updates
this.$nextTick(() => {
requestAF(() => {
this.localShow = false
})
})
}
},
dismissed(newVal) {
if (newVal) {
this.localShow = false
localShow(newVal) {
if (!newVal && (this.dismissible || isNumericLike(this.show))) {
// Only emit dismissed events for dismissible or auto dismissing alerts
this.$emit('dismissed')
}
if (!isNumericLike(this.show) && this.show !== newVal) {
// Only emit booleans if we weren't passed a number via `this.show`
this.$emit('input', newVal)
}
}
},
created() {
this.countDown = parseCountDown(this.show)
this.localShow = parseShow(this.show)
},
mounted() {
this.showChanged(this.show)
this.countDown = parseCountDown(this.show)
this.localShow = parseShow(this.show)
},
destroyed /* istanbul ignore next */() {
this.clearCounter()
beforeDestroy() {
this.clearTimer()
},
methods: {
dismiss() {
this.clearCounter()
if (typeof this.show === 'number') {
this.$emit('dismiss-count-down', 0)
this.$emit('input', 0)
} else {
this.$emit('input', false)
}
this.dismissed = true
this.clearTimer()
this.countDown = 0
this.localShow = false
},
clearCounter() {
clearTimer() {
if (this.countDownTimerId) {
clearInterval(this.countDownTimerId)
this.countDownTimerId = null
}
},
showChanged(show) {
// Reset counter status
this.clearCounter()
// Reset dismiss status
this.dismissed = false
// Set localShow state
this.localShow = Boolean(show)
// No timer for boolean values
if (show === true || show === false || show === null || show === 0) {
return
}
// Start counter (ensure we have an integer value)
let dismissCountDown = parseInt(show, 10) || 1
this.countDownTimerId = setInterval(() => {
if (dismissCountDown < 1) {
this.dismiss()
return
}
dismissCountDown--
this.$emit('dismiss-count-down', dismissCountDown)
this.$emit('input', dismissCountDown)
}, 1000)
},
onBeforeEnter() {
if (this.fade) {
// Add show class one frame after inserted, to make transitions work
requestAF(() => {
this.showClass = true
})
Expand All @@ -113,12 +139,13 @@ export default {
},
render(h) {
const $slots = this.$slots
let $alert = h(false)
let $alert // undefined
if (this.localShow) {
let $dismissBtn = h(false)
if (this.dismissible) {
// Add dismiss button
$dismissBtn = h(
'b-button-close',
BButtonClose,
{ attrs: { 'aria-label': this.dismissLabel }, on: { click: this.dismiss } },
[$slots.dismiss]
)
Expand All @@ -143,8 +170,6 @@ export default {
'transition',
{
props: {
mode: 'out-in',
// Disable use of built-in transition classes
'enter-class': '',
'enter-active-class': '',
'enter-to-class': '',
Expand Down
Loading