Skip to content

RadioGroup 单选框组

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

基本用法

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

defineOption({
    gender: {
        t: 'radio-group',
        label: '性别',
        options: [
            { label: '男', value: 'male' },
            { label: '女', value: 'female' },
        ],
    },
});

Props

除了 共享 PropsElRadioGroup 等相关组件的 Props 外, RadioGroup 组件还支持以下特定属性:

labelKey

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

展示的字段

valueKey

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

提交的字段

type

  • 类型: 'radio' | 'button'
  • 默认: 'radio'

按钮显示的类型

cancelable

  • 类型: boolean
  • 默认: -

选中状态是否可以被取消

disabledKey

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

选项禁用字段

itemProps

  • 类型: object
  • 默认: -

暴露给 Radio 或 RadioButton 的属性, 可参考https://element-plus.org/zh-CN/component/radio.html#radio-attributes

插槽

  • 插槽项请参考 ElRadio.slots

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

插槽示例
tsx
defineOption({
    sex: {
        t: 'radio-group',
        label: '性别',
        itemSlots: {
            default: ({ option }) => h('div', `prefix-${option.label}`),
        },
        options: [{ label: '男', value: 'nan' }, { label: '女', value: 'nv' }],
    },
});
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({
    level: {
        t: 'radio-group',
        label: '等级',
        options: [
            { label: '初级', value: 'beginner' },
            { label: '中级', value: 'intermediate' },
            { label: '高级', value: 'advanced' },
        ],
    },
    size: {
        t: 'radio-group',
        label: '尺寸',
        type: 'button',
        options: [
            { label: '小', value: 'small' },
            { label: '中', value: 'medium' },
            { label: '大', value: 'large' },
        ],
    },
});
点我查看在线示例

可取消选择 + 带禁用选项 + 自定义字段名 + 远程数据

tsx
defineOption({
    priority: {
        t: 'radio-group',
        label: '优先级',
        cancelable: true,
        options: [
            { label: '低', value: 'low' },
            { label: '中', value: 'medium' },
            { label: '高', value: 'high' },
        ],
    },
    role: {
        t: 'radio-group',
        label: '角色',
        options: [
            { label: '用户', value: 'user' },
            { label: '管理员', value: 'admin' },
            { label: '超级管理员', value: 'super_admin', disabled: true },
        ],
    },
    department: {
        t: 'radio-group',
        label: '部门',
        labelKey: 'name',
        valueKey: 'id',
        options: [
            { name: '技术部', id: 1 },
            { name: '设计部', id: 2 },
            { name: '产品部', id: 3 },
        ],
    },
    category: {
        t: 'radio-group',
        label: '分类',
        async getOptions(callback) {
            const categories = await fetchCategories();
            callback(categories);
        },
    },
});
点我查看在线示例

注意事项

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

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

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

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