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

126 lines
4.9 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-29 09:19:55 +00:00
<div :class="cx('root')" v-bind="ptm('root')" data-pc-name="rating">
2023-08-02 14:21:01 +00:00
<div v-if="cancel" :class="cx('cancelItem')" @click="onOptionClick($event, 0)" v-bind="getPTOptions('cancelItem', 0)" :data-p-focused="focusedOptionIndex === 0">
<span class="p-hidden-accessible" v-bind="ptm('hiddenCancelInputWrapper')" :data-p-hidden-accessible="true">
2023-05-06 20:34:27 +00:00
<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)"
2023-05-09 08:34:26 +00:00
v-bind="ptm('hiddenCancelInput')"
2023-05-06 20:34:27 +00:00
/>
2022-09-06 12:03:37 +00:00
</span>
2023-05-24 12:46:10 +00:00
<slot name="cancelicon" :class="cx('cancelIcon')">
<component :is="cancelIcon ? 'span' : 'BanIcon'" :class="[cx('cancelIcon'), cancelIcon]" v-bind="ptm('cancelIcon')" />
2022-09-14 11:26:01 +00:00
</slot>
</div>
<template v-for="value in stars" :key="value">
2023-08-02 14:21:01 +00:00
<div :class="cx('item', { value })" @click="onOptionClick($event, value)" v-bind="getPTOptions('item', value)" :data-p-active="value <= modelValue" :data-p-focused="value === focusedOptionIndex">
<span class="p-hidden-accessible" v-bind="ptm('hiddenItemInputWrapper')" :data-p-hidden-accessible="true">
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-09 08:34:26 +00:00
v-bind="ptm('hiddenItemInput')"
2022-09-14 11:26:01 +00:00
/>
2022-09-06 12:03:37 +00:00
</span>
2023-05-24 12:46:10 +00:00
<slot v-if="value <= modelValue" name="onicon" :value="value" :class="cx('onIcon')">
<component :is="onIcon ? 'span' : 'StarFillIcon'" :class="[cx('onIcon'), onIcon]" v-bind="ptm('onIcon')" />
2022-09-14 11:26:01 +00:00
</slot>
2023-05-24 12:46:10 +00:00
<slot v-else name="officon" :value="value" :class="cx('offIcon')">
<component :is="offIcon ? 'span' : 'StarIcon'" :class="[cx('offIcon'), 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>
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';
2023-05-24 12:46:10 +00:00
import BaseRating from './BaseRating.vue';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
export default {
name: 'Rating',
2023-05-24 12:46:10 +00:00
extends: BaseRating,
2022-09-06 12:03:37 +00:00
emits: ['update:modelValue', 'change', 'focus', 'blur'],
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: {
2023-08-02 14:21:01 +00:00
getPTOptions(key, value) {
2023-05-09 08:34:26 +00:00
return this.ptm(key, {
context: {
active: value <= this.modelValue,
focused: value === this.focusedOptionIndex
}
});
},
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
}
},
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>