|
| 1 | +<template> |
| 2 | + <div |
| 3 | + :class="progressBarClasses" |
| 4 | + :style="progressBarStyles" |
| 5 | + role="progressbar" |
| 6 | + aria-valuemin="0" |
| 7 | + :aria-valuemax="props.max.toString()" |
| 8 | + :aria-valuenow="props.value.toFixed(props.precision)" |
| 9 | + > |
| 10 | + <slot>{{ text }}</slot> |
| 11 | + </div> |
| 12 | +</template> |
| 13 | + |
1 | 14 | <script>
|
| 15 | +import props from './progressProps' |
2 | 16 | export default {
|
3 | 17 | name: 'CProgressBar',
|
4 |
| - render (h) { |
5 |
| - let childNodes = h(false) |
6 |
| - if (this.$slots.default) { |
7 |
| - childNodes = this.$slots.default |
8 |
| - } else if (this.labelHtml) { |
9 |
| - childNodes = h('span', { domProps: { innerHTML: this.labelHtml } }) |
10 |
| - } else if (this.computedShowProgress) { |
11 |
| - childNodes = this.progress.toFixed(this.computedPrecision) |
12 |
| - } else if (this.computedShowValue) { |
13 |
| - childNodes = this.value.toFixed(this.computedPrecision) |
| 18 | + props, |
| 19 | + inject: { |
| 20 | + progress: { |
| 21 | + default: null |
14 | 22 | }
|
15 |
| - return h( |
16 |
| - 'div', |
17 |
| - { |
18 |
| - class: this.progressBarClasses, |
19 |
| - style: this.progressBarStyles, |
20 |
| - attrs: { |
21 |
| - role: 'progressbar', |
22 |
| - 'aria-valuemin': '0', |
23 |
| - 'aria-valuemax': this.computedMax.toString(), |
24 |
| - 'aria-valuenow': this.value.toFixed(this.computedPrecision) |
25 |
| - } |
26 |
| - }, |
27 |
| - [ childNodes ] |
28 |
| - ) |
29 | 23 | },
|
30 | 24 | computed: {
|
| 25 | + props () { |
| 26 | + return Object.keys(props).reduce((props, key) => { |
| 27 | + const propUndefined = !Object.keys(this.$options.propsData).includes(key) |
| 28 | + const propInheritedFromProgress = propUndefined && this.progress.props[key] |
| 29 | + props[key] = propInheritedFromProgress ? this.progress.props[key] : this[key] |
| 30 | + return props |
| 31 | + }, {}) |
| 32 | + }, |
31 | 33 | progressBarClasses () {
|
32 | 34 | return [
|
33 | 35 | 'progress-bar',
|
34 |
| - this.computedVariant ? `bg-${this.computedVariant}` : '', |
35 | 36 | {
|
36 |
| - 'progress-bar-striped': this.computedStriped || this.computedAnimated, |
37 |
| - 'progress-bar-animated': this.computedAnimated |
| 37 | + [`bg-${this.props.variant}`]: this.props.variant, |
| 38 | + 'progress-bar-striped': this.props.striped || this.props.animated, |
| 39 | + 'progress-bar-animated': this.props.animated |
38 | 40 | }
|
39 | 41 | ]
|
40 | 42 | },
|
| 43 | +
|
41 | 44 | progressBarStyles () {
|
42 |
| - return { width: `${(100 * (this.value / this.computedMax))}%` } |
43 |
| - }, |
44 |
| - progress () { |
45 |
| - const p = Math.pow(10, this.computedPrecision) |
46 |
| - return Math.round((100 * p * this.value) / this.computedMax) / p |
47 |
| - }, |
48 |
| - computedMax () { |
49 |
| - // Prefer our max over parent setting |
50 |
| - return this.max ? this.max : (this.$parent.max || 100) |
| 45 | + return { width: `${(100 * (this.props.value / this.props.max))}%` } |
51 | 46 | },
|
52 |
| - computedVariant () { |
53 |
| - // Prefer our variant over parent setting |
54 |
| - return this.variant || this.$parent.variant |
| 47 | + progressValue () { |
| 48 | + const p = Math.pow(10, this.props.precision) |
| 49 | + return Math.round((100 * p * this.props.value) / this.props.max) / p |
55 | 50 | },
|
56 |
| - computedPrecision () { |
57 |
| - // Prefer our precision over parent setting |
58 |
| - return this.precision ? this.precision : (this.$parent.precision || 0) |
59 |
| - }, |
60 |
| - computedStriped () { |
61 |
| - return typeof this.striped === 'boolean' ? this.striped : (this.$parent.striped || false) |
62 |
| - }, |
63 |
| - computedAnimated () { |
64 |
| - return typeof this.animated === 'boolean' ? this.animated : (this.$parent.animated || false) |
65 |
| - }, |
66 |
| - computedShowProgress () { |
67 |
| - return typeof this.showProgress === 'boolean' ? this.showProgress : (this.$parent.showProgress || false) |
68 |
| - }, |
69 |
| - computedShowValue () { |
70 |
| - return typeof this.showValue === 'boolean' ? this.showValue : (this.$parent.showValue || false) |
71 |
| - } |
72 |
| - }, |
73 |
| - props: { |
74 |
| - value: { |
75 |
| - type: Number, |
76 |
| - default: 0 |
77 |
| - }, |
78 |
| - labelHtml: String, |
79 |
| - // $parent prop values take precedence over the following props |
80 |
| - // Which is why they are defaulted to null |
81 |
| - max: Number, |
82 |
| - precision: Number, |
83 |
| - variant: String, |
84 |
| - striped: { |
85 |
| - type: Boolean, |
86 |
| - default: null |
87 |
| - }, |
88 |
| - animated: { |
89 |
| - type: Boolean, |
90 |
| - default: null |
91 |
| - }, |
92 |
| - showProgress: { |
93 |
| - type: Boolean, |
94 |
| - default: null |
95 |
| - }, |
96 |
| - showValue: { |
97 |
| - type: Boolean, |
98 |
| - default: null |
| 51 | + text () { |
| 52 | + if (this.props.showValue || this.props.showProgress) { |
| 53 | + return this.progressValue || this.props.value |
| 54 | + } |
99 | 55 | }
|
100 | 56 | }
|
101 | 57 | }
|
|
0 commit comments