layui/src/module/rate/index.vue

67 lines
1.4 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
>
2021-10-26 18:04:47 +00:00
<i
v-if="rate"
class="layui-icon layui-icon-rate-solid"
:style="{ color: theme }"
/>
<i v-else class="layui-icon layui-icon-rate" :style="{ color: theme }" />
2021-10-04 00:06:19 +00:00
</li>
</ul>
</template>
<script setup lang="ts">
2021-10-26 18:04:47 +00:00
import { defineProps, Ref, ref, watch, withDefaults } from 'vue'
2021-10-04 00:06:19 +00:00
2021-10-26 18:04:47 +00:00
const rates: Ref<Array<boolean>> = ref([])
2021-10-04 00:06:19 +00:00
const props = withDefaults(
defineProps<{
length?: number
modelValue?: number
character?: string
2021-10-26 18:04:47 +00:00
readonly?: boolean
theme?: string
2021-10-04 00:06:19 +00:00
}>(),
{
length: 5,
2021-10-08 07:42:14 +00:00
modelValue: 0,
2021-10-26 18:04:47 +00:00
readonly: false,
2021-10-04 00:06:19 +00:00
}
)
2021-10-08 07:42:14 +00:00
2021-10-27 01:26:01 +00:00
watch(
props,
function () {
rates.value = []
for (let index = 0; index < props.length; index++) {
rates.value.push(false)
}
for (let index = props.modelValue - 1; index >= 0; index--) {
rates.value[index] = true
}
},
{ deep: true, immediate: true }
)
2021-10-08 07:42:14 +00:00
const emit = defineEmits(['update:modelValue'])
const mouseenter = function (index: number) {
2021-10-26 18:04:47 +00:00
if (props.readonly) {
return false
}
2021-10-08 07:42:14 +00:00
for (let i = index; i >= 0; i--) {
2021-10-26 18:04:47 +00:00
rates.value[i] = true
2021-10-08 07:42:14 +00:00
}
2021-10-26 18:04:47 +00:00
for (let i = index + 1; i < props.length; i++) {
rates.value[i] = false
2021-10-08 07:42:14 +00:00
}
emit('update:modelValue', index + 1)
2021-10-08 07:42:14 +00:00
}
2021-10-04 00:06:19 +00:00
</script>