Skip to content

Commit e9d54e6

Browse files
feat(b-form-rating): add show-value-max prop to show possible max rating when show-value is true (#5200)
* feat(b-form-rating): add `show-max-value` prop to show possible max rating when `show-value` is `true` * Update form-rating.js * Update form-rating.js * Update package.json * lint * Update form-rating.js * Update form-rating.spec.js * Update form-rating.js * Update README.md * Update README.md * Update README.md Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
1 parent 6df560b commit e9d54e6

File tree

4 files changed

+95
-9
lines changed

4 files changed

+95
-9
lines changed

src/components/form-rating/README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ inside [input groups](/docs/components/input-group).
1212
There are two main modes for `<b-form-rating>`: interactive and readonly.
1313

1414
Interactive mode allows the user to chose a rating from `1` to the number of stars (default `5`) in
15-
whole number increments.
15+
_whole number_ increments.
1616

1717
**Interactive rating (input mode):**
1818

@@ -209,6 +209,39 @@ decimal) simply set the `precision` prop to the number of digits after the decim
209209
<!-- b-form-rating-value-precision.vue -->
210210
```
211211

212+
#### Show maximum value
213+
214+
<span class="badge badge-info small">2.13.0+</span>
215+
216+
Optionally show the maximum rating possible by also setting the prop `show-value-max` to `true`:
217+
218+
```html
219+
<template>
220+
<div>
221+
<b-form-rating
222+
v-model="value"
223+
readonly
224+
show-value
225+
show-value-max
226+
precision="2"
227+
></b-form-rating>
228+
<p class="mt-2">Value: {{ value }}</p>
229+
</div>
230+
</template>
231+
232+
<script>
233+
export default {
234+
data() {
235+
return {
236+
value: 3.555
237+
}
238+
}
239+
}
240+
</script>
241+
242+
<!-- b-form-rating-value-max.vue -->
243+
```
244+
212245
### Control sizing
213246

214247
Fancy a small or large rating control? Simply set the prop `size` to either `'sm'` or `'lg'`

src/components/form-rating/form-rating.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ export const BFormRating = /*#__PURE__*/ Vue.extend({
127127
type: Boolean,
128128
default: false
129129
},
130+
showValueMax: {
131+
type: Boolean,
132+
default: false
133+
},
130134
disabled: {
131135
type: Boolean,
132136
default: false
@@ -216,15 +220,22 @@ export const BFormRating = /*#__PURE__*/ Vue.extend({
216220
return isLocaleRTL(this.computedLocale)
217221
},
218222
formattedRating() {
219-
const value = this.localValue
220223
const precision = toInteger(this.precision)
221-
return isNull(value)
222-
? ''
223-
: value.toLocaleString(this.computedLocale, {
224-
notation: 'standard',
225-
minimumFractionDigits: isNaN(precision) ? 0 : precision,
226-
maximumFractionDigits: isNaN(precision) ? 3 : precision
227-
})
224+
const showValueMax = this.showValueMax
225+
const locale = this.computedLocale
226+
const formatOptions = {
227+
notation: 'standard',
228+
minimumFractionDigits: isNaN(precision) ? 0 : precision,
229+
maximumFractionDigits: isNaN(precision) ? 3 : precision
230+
}
231+
const stars = this.computedStars.toLocaleString(locale)
232+
let value = this.localValue
233+
value = isNull(value)
234+
? showValueMax
235+
? '-'
236+
: ''
237+
: value.toLocaleString(locale, formatOptions)
238+
return showValueMax ? `${value}/${stars}` : value
228239
}
229240
},
230241
watch: {

src/components/form-rating/form-rating.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,43 @@ describe('form-rating', () => {
252252
wrapper.destroy()
253253
})
254254

255+
it('has expected structure when prop `show-value` and `show-value-max` are set', async () => {
256+
const wrapper = mount(BFormRating, {
257+
propsData: {
258+
showValue: true,
259+
showValueMax: true,
260+
value: '3.5',
261+
precision: 2
262+
}
263+
})
264+
265+
expect(wrapper.isVueInstance()).toBe(true)
266+
await waitNT(wrapper.vm)
267+
268+
const $stars = wrapper.findAll('.b-rating-star')
269+
expect($stars.length).toBe(5)
270+
271+
const $value = wrapper.find('.b-rating-value')
272+
expect($value.exists()).toBe(true)
273+
expect($value.text()).toEqual('3.50/5')
274+
275+
wrapper.setProps({
276+
value: null
277+
})
278+
await waitNT(wrapper.vm)
279+
280+
expect($value.text()).toEqual('-/5')
281+
282+
wrapper.setProps({
283+
value: '1.236'
284+
})
285+
await waitNT(wrapper.vm)
286+
287+
expect($value.text()).toEqual('1.24/5')
288+
289+
wrapper.destroy()
290+
})
291+
255292
it('focus and blur methods work', async () => {
256293
const wrapper = mount(BFormRating, {
257294
attachToDocument: true,

src/components/form-rating/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"prop": "showValue",
3737
"description": "When `true` shows the current rating value in the control"
3838
},
39+
{
40+
"prop": "showValueMax",
41+
"version": "2.13.0",
42+
"description": "When set to `true` and prop `show-value` is `true`, includes the maximum star rating possible in the formatted value"
43+
},
3944
{
4045
"prop": "noBorder",
4146
"description": "When `true` disables the default border"

0 commit comments

Comments
 (0)