二次封装多选框组件

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

相关推荐
橙子家4 小时前
浏览器缓存之【基础键值存储】:Local storage 和 Session storage
前端
星星在线6 小时前
MusicFree:一个「All in One」的个人音乐服务器,让听歌回归简单
前端·后端
IT_陈寒7 小时前
Redis的SETNX并发问题让我加了三天班
前端·人工智能·后端
demo007x8 小时前
Docling 文档转换以及技术架构分析
前端·后端·程序员
京东云开发者8 小时前
京东市民服务又“上新”!这次是黑龙江“龙易办”
前端
袋鱼不重9 小时前
我的神奇同事,AI 用多了居然写了个 Open In Codex
前端·后端·ai编程
竹林8189 小时前
Web3表单签名验证:我用 wagmi 和 ethers 给 DApp 加了一个“免密登录”,踩坑记录全在这了
javascript
用户6990304848759 小时前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
雪碧聊技术9 小时前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
Fireworks9 小时前
深入vue3源码解读 -- 1、响应式的基础概念
前端