二次封装多选框组件

<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>

相关推荐
咬人喵喵2 小时前
植树节主题核心 SVG 交互玩法 + 品牌 / 账号案例 + 组件 / 教程
前端·css·编辑器·svg·e2编辑器
Sailing2 小时前
🚀AI 写代码越来越快,但我开始不敢上线了
前端·后端·面试
Alair‎2 小时前
Vue 3 基础教程:从入门到精通
前端·javascript·vue.js
百锦再2 小时前
树形数据展示:树形表格与树形控件的深度对比(Vue实现)
javascript·vue.js·ecmascript·递归·tree·data·table
Sylvia33.2 小时前
体育数据API实战:用火星数据实现NBA赛事实时比分与状态同步
java·linux·开发语言·前端·python
码农阿豪2 小时前
Vue+Ant Design表格组件开发实战:从问题到优化的完整指南
前端·javascript·vue.js
QQ24391972 小时前
spring boot医院挂号就诊系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
Coder-coco2 小时前
家政服务管理系统|基于springboot + vue家政服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·家政服务管理系统
用户69371750013842 小时前
OS级AI Agent:手机操作系统的下一个战场
android·前端·人工智能