Skip to content

perf(progress): use provide and inject for inter component communication #2540

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 2 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 18 additions & 10 deletions src/components/progress/progress-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { htmlOrText } from '../../utils/html'
// @vue/component
export default {
name: 'BProgressBar',
inject: {
progress: {
from: 'progress',
default: function() {
return {}
}
}
},
props: {
value: {
type: Number,
Expand Down Expand Up @@ -49,7 +57,6 @@ export default {
computed: {
progressBarClasses() {
return [
'progress-bar',
this.computedVariant ? `bg-${this.computedVariant}` : '',
this.computedStriped || this.computedAnimated ? 'progress-bar-striped' : '',
this.computedAnimated ? 'progress-bar-animated' : ''
Expand All @@ -60,39 +67,39 @@ export default {
width: 100 * (this.value / this.computedMax) + '%'
}
},
progress() {
computedProgress() {
const p = Math.pow(10, this.computedPrecision)
return Math.round((100 * p * this.value) / this.computedMax) / p
},
computedMax() {
// Prefer our max over parent setting
return typeof this.max === 'number' ? this.max : this.$parent.max || 100
return typeof this.max === 'number' ? this.max : this.progress.max || 100
},
computedVariant() {
// Prefer our variant over parent setting
return this.variant || this.$parent.variant
return this.variant || this.progress.variant
},
computedPrecision() {
// Prefer our precision over parent setting
return typeof this.precision === 'number' ? this.precision : this.$parent.precision || 0
return typeof this.precision === 'number' ? this.precision : this.progress.precision || 0
},
computedStriped() {
// Prefer our striped over parent setting
return typeof this.striped === 'boolean' ? this.striped : this.$parent.striped || false
return typeof this.striped === 'boolean' ? this.striped : this.progress.striped || false
},
computedAnimated() {
// Prefer our animated over parent setting
return typeof this.animated === 'boolean' ? this.animated : this.$parent.animated || false
return typeof this.animated === 'boolean' ? this.animated : this.progress.animated || false
},
computedShowProgress() {
// Prefer our showProgress over parent setting
return typeof this.showProgress === 'boolean'
? this.showProgress
: this.$parent.showProgress || false
: this.progress.showProgress || false
},
computedShowValue() {
// Prefer our showValue over parent setting
return typeof this.showValue === 'boolean' ? this.showValue : this.$parent.showValue || false
return typeof this.showValue === 'boolean' ? this.showValue : this.progress.showValue || false
}
},
render(h) {
Expand All @@ -102,13 +109,14 @@ export default {
} else if (this.label || this.labelHTML) {
childNodes = h('span', { domProps: htmlOrText(this.labelHTML, this.label) })
} else if (this.computedShowProgress) {
childNodes = this.progress.toFixed(this.computedPrecision)
childNodes = this.computedProgress.toFixed(this.computedPrecision)
} else if (this.computedShowValue) {
childNodes = this.value.toFixed(this.computedPrecision)
}
return h(
'div',
{
staticClass: 'progress-bar',
class: this.progressBarClasses,
style: this.progressBarStyles,
attrs: {
Expand Down
3 changes: 3 additions & 0 deletions src/components/progress/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import BProgressBar from './progress-bar'
export default {
name: 'BProgress',
components: { BProgressBar },
provide() {
return { progress: this }
},
props: {
// These props can be inherited via the child b-progress-bar(s)
variant: {
Expand Down