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

112 lines
4.2 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:21 +00:00
<div :class="cx('root')" v-bind="ptmi('root')">
2022-09-14 11:26:01 +00:00
<template v-for="value in stars" :key="value">
<div :class="cx('option', { value })" @click="onOptionClick($event, value)" v-bind="getPTOptions('option', value)" :data-p-active="value <= modelValue" :data-p-focused="value === focusedOptionIndex">
2024-05-06 15:32:04 +00:00
<span class="p-hidden-accessible" v-bind="ptm('hiddenOptionInputContainer')" :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)"
v-bind="ptm('hiddenOptionInput')"
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,
2024-02-11 23:48:21 +00:00
inheritAttrs: false,
2022-09-06 12:03:37 +00:00
emits: ['update:modelValue', 'change', 'focus', 'blur'],
data() {
return {
name: this.$attrs.name,
focusedOptionIndex: -1,
isFocusVisibleItem: true
2022-09-06 12:03:37 +00:00
};
},
watch: {
2024-04-16 09:35:02 +00:00
'$attrs.name': function (newValue) {
this.name = newValue || UniqueComponentId();
}
},
2024-04-16 09:35:02 +00:00
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);
this.isFocusVisibleItem = false;
2022-09-14 11:26:01 +00:00
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);
this.isFocusVisibleItem = true;
2022-09-14 11:26:01 +00:00
},
onOptionSelect(event, value) {
2024-03-26 07:28:22 +00:00
if (this.focusedOptionIndex === value || this.modelValue === value) {
this.focusedOptionIndex = -1;
this.updateModel(event, null);
} else {
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
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>