Skip to content

Commit 60bacaf

Browse files
committed
refactor: add provide/inject and templates in CProgress and CProgressBar
1 parent 5cd03fa commit 60bacaf

File tree

3 files changed

+72
-122
lines changed

3 files changed

+72
-122
lines changed

src/components/Progress/CProgress.vue

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
1+
<template>
2+
<div class="progress" :style="{ height }">
3+
<slot>
4+
<CProgressBar/>
5+
</slot>
6+
</div>
7+
</template>
8+
19
<script>
210
import CProgressBar from './CProgressBar'
11+
import props from './progressProps'
312
413
export default {
514
name:'CProgress',
615
components: { CProgressBar },
7-
render (h) {
8-
let childNodes = this.$slots.default
9-
if (!childNodes) {
10-
childNodes = h(
11-
'CProgressBar',
12-
{
13-
props: {
14-
value: this.value,
15-
max: this.max,
16-
precision: this.precision,
17-
variant: this.variant,
18-
animated: this.animated,
19-
striped: this.striped,
20-
showProgress: this.showProgress,
21-
showValue: this.showValue
22-
}
23-
}
24-
)
25-
}
26-
return h(
27-
'div',
28-
{
29-
class: [ 'progress' ],
30-
style: { height: this.height }
31-
},
32-
[ childNodes ]
33-
)
34-
},
35-
props: {
36-
height: String,
37-
// These props can be inherited via the child CProgressBar(s)
38-
variant: String,
39-
striped: Boolean,
40-
animated: Boolean,
41-
precision: Number,
42-
showProgress: Boolean,
43-
showValue: Boolean,
44-
max: Number,
45-
// This prop is not inherited by child CProgressBar(s)
46-
value: Number,
16+
props,
17+
provide () {
18+
const progress = {}
19+
Object.defineProperty(progress, 'props', {
20+
get: () => this._props
21+
})
22+
return { progress }
4723
}
4824
}
4925
</script>

src/components/Progress/CProgressBar.vue

Lines changed: 38 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,57 @@
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+
114
<script>
15+
import props from './progressProps'
216
export default {
317
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
1422
}
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-
)
2923
},
3024
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+
},
3133
progressBarClasses () {
3234
return [
3335
'progress-bar',
34-
this.computedVariant ? `bg-${this.computedVariant}` : '',
3536
{
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
3840
}
3941
]
4042
},
43+
4144
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))}%` }
5146
},
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
5550
},
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+
}
9955
}
10056
}
10157
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
height: String,
3+
// These props can be inherited via the child CProgressBar(s)
4+
variant: String,
5+
striped: Boolean,
6+
animated: Boolean,
7+
precision: {
8+
type: Number,
9+
default: 0
10+
},
11+
showProgress: Boolean,
12+
showValue: Boolean,
13+
max: {
14+
type: Number,
15+
default: 100
16+
},
17+
value: Number
18+
}

0 commit comments

Comments
 (0)