primevue-mirror/components/lib/rating/Rating.vue

167 lines
5.0 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="containerClass">
2022-09-14 11:26:01 +00:00
<div v-if="cancel" :class="['p-rating-item p-rating-cancel-item', { 'p-focus': focusedOptionIndex === 0 }]" @click="onOptionClick($event, 0)">
<span class="p-hidden-accessible">
<input type="radio" value="0" :name="name" :checked="modelValue === 0" :disabled="disabled" :readonly="readonly" :aria-label="cancelAriaLabel()" @focus="onFocus($event, 0)" @blur="onBlur" @change="onChange($event, 0)" />
2022-09-06 12:03:37 +00:00
</span>
2022-09-14 11:26:01 +00:00
<slot name="cancelicon">
<span :class="cancelIconClass" />
</slot>
</div>
<template v-for="value in stars" :key="value">
<div :class="['p-rating-item', { 'p-rating-item-active': value <= modelValue, 'p-focus': value === focusedOptionIndex }]" @click="onOptionClick($event, value)">
2022-09-06 12:03:37 +00:00
<span class="p-hidden-accessible">
2022-09-14 11:26:01 +00:00
<input
type="radio"
:value="value"
:name="name"
:checked="modelValue === value"
:disabled="disabled"
:readonly="readonly"
:aria-label="starAriaLabel(value)"
@focus="onFocus($event, value)"
@blur="onBlur"
@change="onChange($event, value)"
/>
2022-09-06 12:03:37 +00:00
</span>
2022-09-14 11:26:01 +00:00
<slot v-if="value <= modelValue" name="onicon" :value="value">
<span :class="onIconClass" />
</slot>
<slot v-else name="officon" :value="value">
<span :class="offIconClass" />
</slot>
</div>
2022-09-06 12:03:37 +00:00
</template>
</div>
</template>
<script>
2022-09-14 11:26:01 +00:00
import { DomHandler, UniqueComponentId } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'Rating',
emits: ['update:modelValue', 'change', 'focus', 'blur'],
props: {
modelValue: {
type: Number,
default: null
},
2022-09-14 11:26:01 +00:00
disabled: {
2022-09-06 12:03:37 +00:00
type: Boolean,
default: false
},
2022-09-14 11:26:01 +00:00
readonly: {
2022-09-06 12:03:37 +00:00
type: Boolean,
default: false
},
stars: {
type: Number,
default: 5
},
cancel: {
type: Boolean,
default: true
2022-09-14 11:26:01 +00:00
},
onIcon: {
type: String,
default: 'pi pi-star-fill'
},
offIcon: {
type: String,
default: 'pi pi-star'
},
cancelIcon: {
type: String,
default: 'pi pi-ban'
2022-09-06 12:03:37 +00:00
}
},
data() {
return {
name: this.$attrs.name,
2022-09-14 11:26:01 +00:00
focusedOptionIndex: -1
2022-09-06 12:03:37 +00:00
};
},
watch: {
'$attrs.name': function (newValue) {
this.name = newValue || UniqueComponentId();
}
},
mounted() {
this.name = this.name || UniqueComponentId();
},
2022-09-06 12:03:37 +00:00
methods: {
2022-09-14 11:26:01 +00:00
onOptionClick(event, value) {
2022-09-06 12:03:37 +00:00
if (!this.readonly && !this.disabled) {
2022-09-14 11:26:01 +00:00
this.onOptionSelect(event, value);
const firstFocusableEl = DomHandler.getFirstFocusableElement(event.currentTarget);
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
firstFocusableEl && DomHandler.focus(firstFocusableEl);
2022-09-06 12:03:37 +00:00
}
},
2022-09-14 11:26:01 +00:00
onFocus(event, value) {
this.focusedOptionIndex = value;
this.$emit('focus', event);
},
2022-09-06 12:03:37 +00:00
onBlur(event) {
2022-09-14 11:26:01 +00:00
this.focusedOptionIndex = -1;
2022-09-06 12:03:37 +00:00
this.$emit('blur', event);
},
2022-09-14 11:26:01 +00:00
onChange(event, value) {
this.onOptionSelect(event, value);
},
onOptionSelect(event, value) {
this.focusedOptionIndex = value;
this.updateModel(event, value || null);
2022-09-06 12:03:37 +00:00
},
updateModel(event, value) {
this.$emit('update:modelValue', value);
2022-09-14 11:26:01 +00:00
this.$emit('change', { originalEvent: event, value });
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
cancelAriaLabel() {
return this.$primevue.config.locale.clear;
},
starAriaLabel(value) {
return value === 1 ? this.$primevue.config.locale.aria.star : this.$primevue.config.locale.aria.stars.replace(/{star}/g, value);
2022-09-06 12:03:37 +00:00
}
},
computed: {
containerClass() {
return [
'p-rating',
{
'p-readonly': this.readonly,
'p-disabled': this.disabled
}
];
2022-09-14 11:26:01 +00:00
},
cancelIconClass() {
return ['p-rating-icon p-rating-cancel', this.cancelIcon];
},
onIconClass() {
return ['p-rating-icon', this.onIcon];
},
offIconClass() {
return ['p-rating-icon', this.offIcon];
2022-09-06 12:03:37 +00:00
}
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>
<style>
2022-09-14 11:26:01 +00:00
.p-rating {
position: relative;
2022-09-14 11:26:01 +00:00
display: flex;
align-items: center;
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
.p-rating-item {
display: inline-flex;
align-items: center;
cursor: pointer;
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
.p-rating.p-readonly .p-rating-item {
cursor: default;
2022-09-06 12:03:37 +00:00
}
</style>