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

181 lines
5.7 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-06 20:34:27 +00:00
<div :class="containerClass" v-bind="ptm('root')">
<div v-if="cancel" :class="['p-rating-item p-rating-cancel-item', { 'p-focus': focusedOptionIndex === 0 }]" @click="onOptionClick($event, 0)" v-bind="ptm('cancelItem')">
<span class="p-hidden-accessible" v-bind="ptm('cancelInputAria')">
<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)"
v-bind="ptm('cancelInput')"
/>
2022-09-06 12:03:37 +00:00
</span>
2022-09-14 11:26:01 +00:00
<slot name="cancelicon">
2023-05-06 20:34:27 +00:00
<component :is="cancelIcon ? 'span' : 'BanIcon'" :class="['p-rating-icon p-rating-cancel', cancelIcon]" v-bind="ptm('cancelIcon')" />
2022-09-14 11:26:01 +00:00
</slot>
</div>
<template v-for="value in stars" :key="value">
2023-05-06 20:34:27 +00:00
<div :class="['p-rating-item', { 'p-rating-item-active': value <= modelValue, 'p-focus': value === focusedOptionIndex }]" @click="onOptionClick($event, value)" v-bind="ptm('item')">
<span class="p-hidden-accessible" v-bind="ptm('itemInputAria')">
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)"
2023-05-06 20:34:27 +00:00
v-bind="ptm('itemInput')"
2022-09-14 11:26:01 +00:00
/>
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">
2023-05-06 20:34:27 +00:00
<component :is="onIcon ? 'span' : 'StarFillIcon'" :class="['p-rating-icon', onIcon]" v-bind="ptm('onIcon')" />
2022-09-14 11:26:01 +00:00
</slot>
<slot v-else name="officon" :value="value">
2023-05-06 20:34:27 +00:00
<component :is="onIcon ? 'span' : 'StarIcon'" :class="['p-rating-icon', offIcon]" v-bind="ptm('offIcon')" />
2022-09-14 11:26:01 +00:00
</slot>
</div>
2022-09-06 12:03:37 +00:00
</template>
</div>
</template>
<script>
2023-05-06 20:34:27 +00:00
import BaseComponent from 'primevue/basecomponent';
import BanIcon from 'primevue/icons/ban';
import StarIcon from 'primevue/icons/star';
import StarFillIcon from 'primevue/icons/starfill';
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',
2023-05-06 20:34:27 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
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: undefined
2022-09-14 11:26:01 +00:00
},
offIcon: {
type: String,
default: undefined
2022-09-14 11:26:01 +00:00
},
cancelIcon: {
type: String,
default: undefined
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
}
];
}
},
components: {
StarFillIcon: StarFillIcon,
StarIcon: StarIcon,
BanIcon: BanIcon
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>