layui/src/module/rate/index.vue

52 lines
1.1 KiB
Vue
Raw Normal View History

2021-10-04 00:06:19 +00:00
<template>
<ul class="layui-rate">
2021-10-08 07:42:14 +00:00
<li
v-for="(rate, index) in rates"
:key="rate"
class="layui-inline"
@mouseenter="mouseenter(index)"
2021-10-08 07:42:14 +00:00
>
<i class="layui-icon" :class="[rate]" />
2021-10-04 00:06:19 +00:00
</li>
</ul>
</template>
<script setup lang="ts">
2021-10-08 07:42:14 +00:00
import { defineProps, Ref, ref, withDefaults } from 'vue'
2021-10-04 00:06:19 +00:00
const rates: Ref<Array<string>> = ref([])
2021-10-04 00:06:19 +00:00
const props = withDefaults(
defineProps<{
length?: number
modelValue?: number
character?: string
2021-10-04 00:06:19 +00:00
}>(),
{
length: 5,
2021-10-08 07:42:14 +00:00
modelValue: 0,
2021-10-04 00:06:19 +00:00
}
)
2021-10-08 07:42:14 +00:00
for (let index = 0; index < props.length; index++) {
rates.value.push('layui-icon-rate')
}
for (let index = props.modelValue - 1; index >= 0; index--) {
rates.value[index] = 'layui-icon-rate-solid'
}
const emit = defineEmits(['update:modelValue'])
const mouseenter = function (index: number) {
for (let i = index; i >= 0; i--) {
rates.value[i] = 'layui-icon-rate-solid'
}
for (let j = index + 1; j < props.length; j++) {
rates.value[j] = 'layui-icon-rate'
}
// select update 时, 通知 change 事件
emit('update:modelValue', index + 1)
2021-10-08 07:42:14 +00:00
}
2021-10-04 00:06:19 +00:00
</script>