UnisKB/ui/src/components/common-list/index.vue

94 lines
1.9 KiB
Vue
Raw Normal View History

2023-11-15 09:42:31 +00:00
<template>
<div class="common-list">
<ul v-if="data.length > 0">
<template v-for="(item, index) in data" :key="index">
<li
2024-10-23 09:01:19 +00:00
@click.stop="clickHandle(item, index)"
2024-05-20 09:50:14 +00:00
:class="current === item[props.valueKey] ? 'active' : ''"
class="cursor"
2024-10-23 09:01:19 +00:00
@mouseenter.stop="mouseenter(item)"
@mouseleave.stop="mouseleave()"
>
<slot :row="item" :index="index"> </slot>
</li>
</template>
</ul>
2024-05-20 09:50:14 +00:00
<slot name="empty" v-else>
<el-empty description="暂无数据" />
</slot>
2023-11-15 09:42:31 +00:00
</div>
</template>
<script setup lang="ts">
2024-05-20 09:50:14 +00:00
import { ref, watch } from 'vue'
2023-11-27 10:08:58 +00:00
2023-11-15 09:42:31 +00:00
defineOptions({ name: 'CommonList' })
2024-01-23 07:26:20 +00:00
const props = withDefaults(
2023-11-27 10:08:58 +00:00
defineProps<{
data: Array<any>
2024-01-29 07:06:44 +00:00
defaultActive?: string
2024-05-20 09:50:14 +00:00
valueKey?: string // 唯一标识的键名
2023-11-27 10:08:58 +00:00
}>(),
{
2024-01-23 07:26:20 +00:00
data: () => [],
2024-05-20 09:50:14 +00:00
defaultActive: '',
valueKey: 'id'
2023-11-27 10:08:58 +00:00
}
)
2023-11-15 09:42:31 +00:00
2024-05-20 09:50:14 +00:00
const current = ref<Number | String>(0)
2024-01-23 07:26:20 +00:00
watch(
() => props.defaultActive,
(val) => {
2024-05-20 09:50:14 +00:00
current.value = val
2024-01-23 07:26:20 +00:00
},
{ immediate: true }
)
const emit = defineEmits(['click', 'mouseenter', 'mouseleave'])
function mouseenter(row: any) {
emit('mouseenter', row)
}
function mouseleave() {
emit('mouseleave')
}
2023-12-01 03:36:04 +00:00
function clickHandle(row: any, index: number) {
2024-05-20 09:50:14 +00:00
current.value = row[props.valueKey]
2023-11-15 09:42:31 +00:00
emit('click', row)
}
2024-10-16 09:08:11 +00:00
function clearCurrent() {
current.value = 0
}
defineExpose({
clearCurrent
})
2023-11-15 09:42:31 +00:00
</script>
<style lang="scss" scoped>
2023-11-22 03:05:06 +00:00
/* 通用 ui li样式 */
2023-11-15 09:42:31 +00:00
.common-list {
li {
2023-12-04 03:18:12 +00:00
padding: 10px 16px;
2024-05-20 09:50:14 +00:00
font-weight: 400;
2024-12-06 10:23:32 +00:00
color: var(--el-text-color-regular);
font-size: 14px;
2023-11-15 09:42:31 +00:00
&.active {
background: var(--el-color-primary-light-9);
border-radius: 4px;
color: var(--el-color-primary);
2024-05-20 09:50:14 +00:00
font-weight: 500;
2024-10-23 09:01:19 +00:00
&:hover {
background: var(--el-color-primary-light-9);
}
}
&:hover {
border-radius: 4px;
background: var(--app-text-color-light-1);
2023-11-15 09:42:31 +00:00
}
}
}
</style>