|
| 1 | +<template> |
| 2 | + <div class="common-list"> |
| 3 | + <ul v-if="data.length > 0"> |
| 4 | + <template v-for="(item, index) in data" :key="index"> |
| 5 | + <li |
| 6 | + @click.stop="clickHandle(item, index)" |
| 7 | + :class="current === item[props.valueKey] ? 'active color-primary-1' : ''" |
| 8 | + class="cursor" |
| 9 | + @mouseenter.stop="mouseenter(item)" |
| 10 | + @mouseleave.stop="mouseleave()" |
| 11 | + > |
| 12 | + <slot :row="item" :index="index"> </slot> |
| 13 | + </li> |
| 14 | + </template> |
| 15 | + </ul> |
| 16 | + <slot name="empty" v-else> |
| 17 | + <el-empty :description="$t('common.noData')" /> |
| 18 | + </slot> |
| 19 | + </div> |
| 20 | +</template> |
| 21 | +<script setup lang="ts"> |
| 22 | +import { ref, watch } from 'vue' |
| 23 | +
|
| 24 | +defineOptions({ name: 'CommonList' }) |
| 25 | +
|
| 26 | +const props = withDefaults( |
| 27 | + defineProps<{ |
| 28 | + data: Array<any> |
| 29 | + defaultActive?: string |
| 30 | + valueKey?: string // 唯一标识的键名 |
| 31 | + }>(), |
| 32 | + { |
| 33 | + data: () => [], |
| 34 | + defaultActive: '', |
| 35 | + valueKey: 'id' |
| 36 | + } |
| 37 | +) |
| 38 | +
|
| 39 | +const current = ref<Number | String>(0) |
| 40 | +
|
| 41 | +watch( |
| 42 | + () => props.defaultActive, |
| 43 | + (val) => { |
| 44 | + current.value = val |
| 45 | + }, |
| 46 | + { immediate: true } |
| 47 | +) |
| 48 | +
|
| 49 | +const emit = defineEmits(['click', 'mouseenter', 'mouseleave']) |
| 50 | +
|
| 51 | +function mouseenter(row: any) { |
| 52 | + emit('mouseenter', row) |
| 53 | +} |
| 54 | +function mouseleave() { |
| 55 | + emit('mouseleave') |
| 56 | +} |
| 57 | +
|
| 58 | +function clickHandle(row: any, index: number) { |
| 59 | + current.value = row[props.valueKey] |
| 60 | + emit('click', row) |
| 61 | +} |
| 62 | +
|
| 63 | +function clearCurrent() { |
| 64 | + current.value = 0 |
| 65 | +} |
| 66 | +defineExpose({ |
| 67 | + clearCurrent |
| 68 | +}) |
| 69 | +</script> |
| 70 | +<style lang="scss" scoped> |
| 71 | +/* 通用 ui li样式 */ |
| 72 | +.common-list { |
| 73 | + li { |
| 74 | + padding: 8px 16px; |
| 75 | + font-weight: 400; |
| 76 | + color: var(--el-text-color-regular); |
| 77 | + font-size: 14px; |
| 78 | + margin-bottom: 4px; |
| 79 | + &.active { |
| 80 | + border-radius: 4px; |
| 81 | + color: var(--el-color-primary); |
| 82 | + font-weight: 500; |
| 83 | + &:hover { |
| 84 | + background: var(--el-color-primary-light-9); |
| 85 | + } |
| 86 | + } |
| 87 | + &:hover { |
| 88 | + border-radius: 4px; |
| 89 | + background: var(--app-text-color-light-1); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +</style> |
0 commit comments