二次封装多选框组件

<CheckListWithIcon v-model="checkList" :options="industryOptions" />

const checkList = ref(\[\]);

const industryOptions = ref([

{ value: '0', label: '带发货' },

{ value: '1', label: '运输中' },

{ value: '2', label: '已到货' }

]);

<template>

<div class="checklist-with-icon">

<el-checkbox-group v-model="innerValue" class="checkbox-group">

<div v-for="option in options" :key="option.value" class="checkbox-item" @click="toggleOption(option.value)">

<component :is="loadImageAsset(

innerValue.includes(option.value)

? 'multipleselectio.png'

: 'multiplechoice.png'

)" class="icon" />

<span class="custom-label">{{ option.label }}</span>

</div>

</el-checkbox-group>

</div>

</template>

<script setup>

import { ref, watch } from 'vue'

import { loadImageAsset } from '@/utils/loadImageAsset.js'

const props = defineProps({

modelValue: {

type: Array,

default: () => \[\]

},

options: {

type: Array,

default: () => \[\]

}

})

const emit = defineEmits('update:modelValue')

// 内部值,用于双向绑定

const innerValue = ref(Array.isArray(props.modelValue) ? ...props.modelValue : \[\])

// 监听内部值变化,同步到父组件

watch(innerValue, (newVal) => {

emit('update:modelValue', newVal)

}, { deep: true }) // 数组内部变化也能监听到

// 监听外部值变化

watch(() => props.modelValue, (newVal) => {

if (Array.isArray(newVal)) {

innerValue.value = ...newVal

}

}, { deep: true })

// 切换选项的选中状态(核心方法)

const toggleOption = (value) => {

const index = innerValue.value.indexOf(value)

if (index > -1) {

// 已选中,移除

innerValue.value = innerValue.value.filter(item => item !== value)

} else {

// 未选中,添加

innerValue.value = ...innerValue.value, value

}

}

</script>

<style scoped>

.checklist-with-icon {

display: flex;

align-items: center;

gap: 16px;

flex-wrap: nowrap;

/* 防止换行 */

}

.checkbox-item {

display: flex;

align-items: center;

gap: 8px;

cursor: pointer;

user-select: none;

/* 防止文字被选中 */

}

.icon {

width: 40px;

height: 40px;

flex-shrink: 0;

}

/* 自定义标签样式,替代原生复选框标签 */

.custom-label {

margin: 20px 30px;

padding: 0;

line-height: 1;

font-size: 40px;

cursor: pointer;

font-weight: 400;

color: #656C83;

line-height: 21px;

text-align: left;

font-style: normal;

text-transform: none;

/* margin-left: 20px; */

}

.checkbox-group {

display: flex;

}

</style>

<style>

.el-checkbox__input {

cursor: pointer;

display: inline-flex;

outline: none;

position: relative;

display: none;

white-space: nowrap;

}

</style>

相关推荐
jingchao19982 小时前
Cannot read properties of null (reading ‘insertBefore‘)
前端·javascript·vue.js
计算机魔术师2 小时前
新文章
前端
爱跳舞的烤冷面2 小时前
自学嵌入式第N天(Linux篇——文件编程)
java·开发语言·前端
IT_陈寒2 小时前
Vite的热更新突然失效,原来我忽略了这个配置
前端·人工智能·后端
怪奇云呼军2 小时前
闪电智能VoiceAgent 如何管理呼入、接听、桥接和挂断状态?
java·前端·网络·数据库·人工智能
云浪2 小时前
Milvus + RAG 实战:《红楼梦》问答助手
前端·人工智能·后端
梦醒沉醉2 小时前
2、JavaScript控制流和错误处理
javascript
恋猫de小郭2 小时前
Dart 3.13 大更新,感觉比 Flutter 更带劲
android·前端·flutter
晓得迷路了2 小时前
栗子前端技术周刊第 142 期 - DeepSeek Harness、pnpm 12 RC、crypto‑js...
前端·javascript·ai编程
xiaoxiangsiyan2 小时前
RHCE之2026年全套考点详解
运维·前端·网络·chrome·学习·rhce