Naive UI 多选框自定义tag和label

 Naive UI 的多选框可使用render-label和render-tag自定义下拉框的样式和选中后标签的样式。具体代码如下:

<script setup lang="ts">
    import { CascaderOption, NCheckbox, SelectRenderLabel, SelectRenderTag } from 'naive-ui'

    const renderLabel: SelectRenderLabel = (option: any, selected: Boolean) =>
    h(
        'div',null,{
            default: () => [
                h(
                    NCheckbox,
                    { value: option.value as string, label: option.label as string, checked: selected},
                    { default: () => option.value }
                ),
            ],
        }
    )
    const renderTag: SelectRenderTag = ({ option, handleClose }) => 
    h(
        NTag,
        {
            class: 'multiple-tag',
            closable: true,
            bordered: false,
            onMousedown: (e: FocusEvent) => {
                e.preventDefault()
            },
            onClose: (e: MouseEvent) => {
                e.stopPropagation()
                handleClose()
            }
        },
        { default: () => option.label }
    )
    
    const handleUpdateValue = (value: string, option: CascaderOption) => {
        console.log(value, option)
    }

    const selectOptions = ref([
        {
          label: "Everybody's Got Something to Hide Except Me and My Monkey",
          value: 'song0',
        },
        {
          label: 'Drive My Car',
          value: 'song1'
        },
        {
          label: 'Norwegian Wood',
          value: 'song2'
        },
        {
          label: "You Won't See",
          value: 'song3',
        },
        {
          label: 'Nowhere Man',
          value: 'song4'
        },
        {
          label: 'Think For Yourself',
          value: 'song5'
        },
        {
          label: 'The Word',
          value: 'song6'
        },
        {
          label: 'Michelle',
          value: 'song7',
        },
        {
          label: 'What goes on',
          value: 'song8'
        },
        {
          label: 'Girl',
          value: 'song9'
        },
        {
          label: "I'm looking through you",
          value: 'song10'
        },
        {
          label: 'In My Life',
          value: 'song11'
        },
        {
          label: 'Wait',
          value: 'song12'
        }
    ])
    const selectValue = ref([])
    const selectCheckValue = ref([])
</script>

<template>
    <n-space>
        <n-select class="cascader-div" v-model:value="selectValue" :show-checkmark = "false" :options="selectOptions" />
        <n-select
            class="cascader-div"
            v-model:value="selectCheckValue"
            :options="selectOptions"
            placeholder="请输入"
            filterable
            multiple
            clearable
            :show-checkmark = "false"
            :render-label = "renderLabel"
            :render-tag = "renderTag"
            @update:value="handleUpdateValue"
        />
    </n-space>
</template>

<style scoped lang="scss">
.cascader-div {
    width: 200px;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值