Skip to content

Commit 7635ae2

Browse files
committed
Improvement - VueUiRating - Improve reactivity, add formatter and roundingValue config options for tooltip
1 parent b8fa0e9 commit 7635ae2

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/components/vue-ui-rating.vue

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup>
2-
import { ref, computed, onMounted } from "vue";
2+
import { ref, computed, onMounted, watch } from "vue";
33
import {
44
createStar,
55
createUid,
@@ -10,6 +10,8 @@ import {
1010
} from "../lib.js";
1111
import { useNestedProp } from "../useNestedProp";
1212
import { useConfig } from "../useConfig";
13+
import { applyDataLabel } from "../lib.js";
14+
import { dataLabel } from "../lib.js";
1315
1416
const { vue_ui_rating: DEFAULT_CONFIG } = useConfig();
1517
@@ -35,12 +37,26 @@ const units = ref([]);
3537
3638
const emit = defineEmits(['rate']);
3739
38-
const FINAL_CONFIG = computed(() => {
40+
const FINAL_CONFIG = computed({
41+
get: () => {
42+
return prepareConfig();
43+
},
44+
set: (newCfg) => {
45+
return newCfg
46+
}
47+
});
48+
49+
function prepareConfig() {
3950
return useNestedProp({
4051
userConfig: props.config,
4152
defaultConfig: DEFAULT_CONFIG
4253
});
43-
});
54+
}
55+
56+
watch(() => props.config, (_newCfg) => {
57+
FINAL_CONFIG.value = prepareConfig();
58+
prepareChart();
59+
}, { deep: true });
4460
4561
const propRating = computed(() => {
4662
if(typeof props.dataset.rating === 'object' && !Array.isArray(props.dataset.rating)) {
@@ -55,8 +71,8 @@ const hasBreakdown = computed(() => {
5571
})
5672
5773
const currentRating = ref(propRating.value);
58-
const isImage = ref(FINAL_CONFIG.value.type === "image");
59-
const isReadonly = ref(FINAL_CONFIG.value.readonly);
74+
const isImage = computed(() => FINAL_CONFIG.value.type === "image");
75+
const isReadonly = computed(() => FINAL_CONFIG.value.readonly)
6076
6177
function calculateAverageRating(source) {
6278
let totalSum = 0;
@@ -79,17 +95,21 @@ function calculateAverageRating(source) {
7995
}
8096
8197
onMounted(() => {
98+
prepareChart();
99+
});
100+
101+
function prepareChart() {
82102
if(objectIsEmpty(props.dataset)) {
83103
error({
84104
componentName: 'VueUiRating',
85105
type: 'dataset'
86106
})
87107
}
88-
108+
units.value = [];
89109
for (let i = FINAL_CONFIG.value.from; i <= FINAL_CONFIG.value.to; i += 1) {
90110
units.value.push(i);
91111
}
92-
});
112+
}
93113
94114
95115
function getInactiveFill(value, isImage = false) {
@@ -306,7 +326,17 @@ defineExpose({
306326
<template v-if="FINAL_CONFIG.style.tooltip.show && hasBreakdown && isReadonly">
307327
<div class="vue-ui-rating-tooltip" :style="`border:1px solid ${FINAL_CONFIG.style.tooltip.borderColor};position:absolute;top:${-48 + FINAL_CONFIG.style.tooltip.offsetY}px;left:50%;transform:translateX(-50%);width:fit-content;text-align:center;background:${FINAL_CONFIG.style.tooltip.backgroundColor};display:${hoveredValue === value ? 'block' : 'none'};padding:2px 12px;border-radius:${FINAL_CONFIG.style.tooltip.borderRadius}px;box-shadow:${FINAL_CONFIG.style.tooltip.boxShadow}`">
308328
<div :data-cy="`rating-tooltip-${i}`" :style="`width:100%;display:flex;flex-direction:row;gap:6px;position:relative;text-align:center;color:${FINAL_CONFIG.style.tooltip.color}`">
309-
<span :style="`font-size:${FINAL_CONFIG.style.tooltip.fontSize}px`">{{ value }}</span> : <span :style="`font-weight:${FINAL_CONFIG.style.tooltip.bold ? 'bold' : 'normal'};font-size:${FINAL_CONFIG.style.tooltip.fontSize}px`">{{ props.dataset.rating[value] }}</span>
329+
<span :style="`font-size:${FINAL_CONFIG.style.tooltip.fontSize}px`">{{ value }}:</span><span :style="`font-weight:${FINAL_CONFIG.style.tooltip.bold ? 'bold' : 'normal'};font-size:${FINAL_CONFIG.style.tooltip.fontSize}px`">
330+
{{ applyDataLabel(
331+
FINAL_CONFIG.style.tooltip.formatter,
332+
props.dataset.rating[value],
333+
dataLabel({
334+
v: props.dataset.rating[value],
335+
r: FINAL_CONFIG.style.tooltip.roundingValue
336+
}),
337+
FINAL_CONFIG
338+
) }}
339+
</span>
310340
<div :style="`font-family:Arial !important;position:absolute;top:calc(100% - 4px);left:50%;transform:translateX(-50%);color:${FINAL_CONFIG.style.tooltip.borderColor}`">
311341
312342
</div>
@@ -316,7 +346,6 @@ defineExpose({
316346
</div>
317347
</template>
318348

319-
320349
<!-- RATING POSITION RIGHT -->
321350
<div data-cy="rating-position-right" v-if="FINAL_CONFIG.style.rating.show && FINAL_CONFIG.style.rating.position === 'right'" :style="`width:fit-content;text-align:center;margin-bottom:${FINAL_CONFIG.style.rating.offsetY}px;font-size:${FINAL_CONFIG.style.rating.fontSize}px;font-weight:${FINAL_CONFIG.style.rating.bold ? 'bold' : 'normal'};padding-left:${FINAL_CONFIG.style.rating.offsetX}px`">
322351
{{ isNaN(currentRating) ? '' : currentRating.toFixed(FINAL_CONFIG.style.rating.roundingValue) }}
@@ -328,8 +357,6 @@ defineExpose({
328357
<div data-cy="rating-position-bottom" v-if="FINAL_CONFIG.style.rating.show && FINAL_CONFIG.style.rating.position === 'bottom'" :style="`width:100%;text-align:center;margin-top:${FINAL_CONFIG.style.rating.offsetY}px;font-size:${FINAL_CONFIG.style.rating.fontSize}px;font-weight:${FINAL_CONFIG.style.rating.bold ? 'bold' : 'normal'};margin-left:${FINAL_CONFIG.style.rating.offsetX}px`">
329358
{{ isNaN(currentRating) ? '' : currentRating.toFixed(FINAL_CONFIG.style.rating.roundingValue) }}
330359
</div>
331-
332-
<!-- TOOLTIP -->
333360
</div>
334361
</template>
335362

0 commit comments

Comments
 (0)