Skip to content

CheckboxGroup 复选框组

CheckboxGroup 组件是一个基于 ElementPlus 复选框组的封装组件, 支持多选功能和选项配置.

基本用法

tsx
import { defineOption } from '@xiaohaih/json-form-plus';

defineOption({
    hobbies: {
        t: 'checkbox-group',
        label: '兴趣爱好',
        options: [
            { label: '阅读', value: 'reading' },
            { label: '音乐', value: 'music' },
            { label: '运动', value: 'sports' },
        ],
    },
});

Props

除了 共享 PropsElCheckboxGroup 自带的 Props 外, CheckboxGroup 组件还支持以下特定属性:

labelKey

  • 类型: string
  • 默认: 'label'

展示的字段

valueKey

  • 类型: string
  • 默认: 'value'

提交的字段

type

  • 类型: 'checkbox' | 'button'
  • 默认: -

按钮类型(checkbox|button), 默认 checkbox

disabledKey

  • 类型: string
  • 默认: 'disabled'

选项禁用字段

itemProps

  • 类型: Partial<ExtractPublicPropTypes<ReturnType<typeof emits2props<typeof elCheckboxProps, [NonNullable<typeof elCheckboxEmits>]>>>>
  • 默认: -

暴露给 CheckboxCheckboxButton 的属性

插槽

  • 插槽项请参考 ElCheckbox.slots

  • 插槽实际的 Props 等于插槽自带的 Props + SlotProps

插槽示例
tsx
defineOption({
    sex: {
        t: 'checkbox-group',
        label: '性别',
        itemSlots: {
            default: ({ option }) => h('span', `自定义-${option.label}`),
        },
        options: [
            { label: '男', value: '1' },
            { label: '女', value: '2' },
        ],
    },
});
SlotProps 声明如下
ts
export interface SlotProps {
    /**
     * 获取传递给 FormItem props
     * 可参考 https://element-plus.org/zh-CN/component/form.html#formitem-api
     */
    getFormItemProps: () => Record<string, any>;
    /** 获取传递给当前 ElementPlus 组件 props */
    getItemProps: () => Record<string, any>;
    /** 获取传递给当前组件 props */
    getProps: () => Record<string, any>;
    /** 额外的参数(纯纯的语法糖配置) */
    extraOptions: {
        /** 传递给组件的值 */
        modelValue: any;
        /** 数据源 */
        options: any;
        /** 更新值 */
        onChange: (value: any) => void;
    };
    /** 封装给组件的通用选项 */
    plain: PlainReturnValue;
}

ts
/** usePlain 的返回值 */
export interface PlainReturnValue {
    /** 覆盖 props 的最新的值(defaultValue, initialValue) */
    coverProps: Record<'defaultValue' | 'initialValue', any>;
    /** HForm 暴露给组件的选项 */
    wrapper: WrapperProvideValue;
    /** 在特定时机中 HForm 会调用该选项下的方法 */
    option: CommonMethod;
    /** 数据加载状态 */
    loading: Ref<boolean>;
    /** 可主动获取远程数据 */
    getOptions: (trigger: 'initial' | 'depend' | 'other', option?: {
        /** 筛选值 */
        filterValue?: string;
    }) => Promise<void>;
    /** 当前组件的值 */
    checked: Ref<any>;
    /** 获取当前组件提交给后端的值 */
    getQuery: () => Record<string, any>;
    /** 远程数据(getOptions 返回的数据) */
    remoteOption: Ref<Record<string, any>[]>;
    /** 优先返回 remoteOption, 其次返回外部传递的 options */
    finalOption: ComputedRef<Record<string, any>[]>;
    /** 是否隐藏组件 */
    insetHide: ComputedRef<boolean>;
    /** 更新值但不触发外部的搜索事件 */
    updateCheckedValue: (value: any) => void;
    /**
     * 更新值并根据 HForm realtime 状态判断
     * - realtime: true 则触发搜索事件
     * - realtime: false 则仅更新值(等同于 updateCheckedValue 函数)
     */
    change: (value: any) => void;
    /** 等同于 updateCheckedValue, 只是可以选择不传值(用来兼容低版本) */
    trigger: (value?: any) => void;
    /** 触发搜索事件 */
    search: () => void;
    /** 重置 */
    reset: () => void;
    /** 传递给 HForm 的 readonly 属性 */
    globalReadonly: Ref<boolean>;
    /** 传递给 HForm 的 disabled 属性 */
    globalDisabled: Ref<boolean>;
}

/** 组件提供给 HForm 的选项 */
export interface CommonMethod {
    /** 重置 */
    reset: () => void;
    /** 更新 HForm 中 query 的值 */
    updateWrapperQuery: () => void;
    /** 校验方法 */
    validator?: (query: Record<string, string>) => Promise<any> | any;
    /** 获取该组件拼接的参数 */
    getQuery: () => Record<string, any>;
    /** 在 watch 中 backfill 改变后, 需要执行回调 */
    onChangeByBackfill?: () => void;
}

/** HForm 暴露给组件的选项 */
export interface WrapperProvideValue {
    /** 表单是否只读 */
    readonly?: Ref<boolean | undefined>;
    /** 表单是否禁用 */
    disabled?: Ref<boolean | undefined>;
    /**
     * 是否实时触发
     */
    realtime: Ref<boolean | undefined>;
    /**
     * 子组件需主动注册组件, 否则不会生效
     * @param {CommonMethod} config 提供父组件校验, 重置等方法
     *
     * @returns {() => void} 取消注册 - 默认会自动取消, 如果是异步任务内注册, 需自己手动取消
     */
    register: (config: CommonMethod) => () => void;
    /**
     * 子组件通知父级更新 query 中的值 - 静默修改, 不触发搜索事件
     * @param {string} field 更新的字段
     * @param {*} value 更新的值
     * @param {string} nativeField 原始提供的字段(不受 as, fields 等属性的影响)
     */
    updateQueryValue: (field: string, value: any, nativeField: string) => void;
    /**
     * 子组件内部值发生了变动, 由父级决定是否触发搜索事件(实时搜索时需要区分这两种方式)
     * @param {string | string[]} [tryFields] 比较 query[tryFields] 与 backfill[tryFields]是否一致, 不一致时才触发搜索
     */
    insetSearch: (tryFields?: string | string[]) => void;
    /**
     * 提供给组件内部的直接触发到外部的搜索事件
     */
    search: () => Promise<string | void>;
    /** 删除内部无引用的字段 */
    removeUnreferencedField: (field: string) => void;
    /** 所有条件的 options 数据 */
    options: Record<string, any[]>;
}

示例

基础复选框组 + 自定义字段名 + 禁用特定字段

tsx
defineOption({
    skills: {
        t: 'checkbox-group',
        label: '技能',
        labelKey: 'name',
        valueKey: 'id',
        options: [
            { name: 'JavaScript', id: 'js' },
            { name: 'TypeScript', id: 'ts' },
            { name: 'Vue', id: 'vue' },
            { name: 'React', id: 'react', disabled: true },
        ],
    },
});
点我查看在线示例

按钮样式 + 校验

tsx
defineOption({
    colors: {
        t: 'checkbox-group',
        label: '颜色',
        type: 'button',
        options: [
            { label: '红色', value: 'red' },
            { label: '绿色', value: 'green' },
            { label: '蓝色', value: 'blue' },
        ],
        rules: [
            { required: true, message: '不能为空' },
            { validator(rule, value, cb) {
                if (!value) return cb([]);
                cb(value.includes('green') ? [] : ['绿色是必不可少的']);
            } },
        ],
    },
});
点我查看在线示例

远程数据

tsx
defineOption({
    tags: {
        t: 'checkbox-group',
        label: '标签',
        async getOptions(callback) {
            const tags = await fetchTags();
            callback(tags);
        },
    },
});

注意事项

  1. 支持 ElFormItem 组件所有的 Props
  2. 支持 ElCheckboxGroup 组件所有的 Props
  3. 选项对象需要包含 labelvalue 字段, 可通过 labelKeyvalueKey 自定义字段名
  4. 设置 type: 'button' 可以显示为按钮样式
  5. 可以通过 disabled 字段禁用特定选项

tips: 当 ElFormItem 组件与 ElCheckboxGroup 组件的 Props 冲突时

  • 可通过 formItemProps 将属性传递给 ElFormItem

  • 可通过 staticProps 将属性传递给 ElCheckboxGroup