| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <template>
- <view class="rating-single">
- <text class="rating-label" v-if="label">{{ label }}</text>
- <view class="stars" @click.stop="handleClick">
- <!-- 修复:v-for 循环从 0 到 maxScore,共显示 maxScore 个星星 -->
- <text class="star" :class="{ active: currentScore >= index }" v-for="index in maxScore" :key="index"
- :data-index="index">★</text>
- </view>
- <text class="rating-text" v-if="showText">{{ getRatingText(currentScore) }}</text>
- </view>
- </template>
- <script>
- export default {
- name: "RatingSingle",
- model: {
- prop: "value",
- event: "change"
- },
- props: {
- label: {
- type: String,
- default: ""
- },
- value: {
- type: Number,
- default: 0
- },
- maxScore: {
- type: Number,
- default: 5
- },
- showText: {
- type: Boolean,
- default: false
- },
- textMap: {
- type: Array,
- default () {
- return ['非常不满意', '不满意', '一般', '满意', '非常满意'];
- }
- }
- },
- computed: {
- currentScore() {
- return Math.min(Math.max(this.value, 0), this.maxScore);
- }
- },
- methods: {
- getRatingText(score) {
- console.log(score, 'score')
- const index = Math.floor(score) - 1;
- return this.textMap[index] || "";
- },
- handleClick(e) {
- const target = e.target;
- const score = parseInt(target.dataset.index);
- if (!isNaN(score) && score >= 1 && score <= this.maxScore) {
- this.$emit("change", score);
- }
- }
- }
- };
- </script>
- <style scoped>
- .rating-single {
- display: flex;
- align-items: center;
- margin: 10px 0;
- }
- .rating-label {
- width: 80px;
- text-align: right;
- margin-right: 10px;
- color: #333;
- }
- .stars {
- display: flex;
- cursor: pointer;
- }
- .star {
- font-size: 20px;
- color: #e6e6e6;
- margin-right: 3px;
- transition: color 0.2s;
- }
- .star.active {
- /* color: #ffd700; */
- color: rgb(255, 202, 62);
- }
- .rating-text {
- margin-left: 8px;
- color: #666;
- }
- </style>
|