import{ax as l,al as k,z as p,A as e,B as i,R as n,b5 as d,O as a,J as h}from"./chunks/framework.V2ssSR2R.js";const b=JSON.parse('{"title":"Checkbox 多选框","description":"","frontmatter":{},"headers":[],"relativePath":"form-config/fields/checkbox.md","filePath":"form-config/fields/checkbox.md"}'),r={name:"form-config/fields/checkbox.md"};function g(E,s,F,y,o,c){const t=k("demo-block");return e(),p("div",null,[s[7]||(s[7]=i("h1",{id:"checkbox-多选框",tabindex:"-1"},[a("Checkbox 多选框 "),i("a",{class:"header-anchor",href:"#checkbox-多选框","aria-label":"Permalink to “Checkbox 多选框”"},"​")],-1)),s[8]||(s[8]=i("p",null,"一组备选项中进行多选",-1)),s[9]||(s[9]=i("h2",{id:"基础用法",tabindex:"-1"},[a("基础用法 "),i("a",{class:"header-anchor",href:"#基础用法","aria-label":"Permalink to “基础用法”"},"​")],-1)),s[10]||(s[10]=i("p",null,"单独使用可以表示两种状态之间的切换。",-1)),n(t,{type:"form",config:[{type:"checkbox",name:"checkbox",text:"选项"}]},{source:h(()=>[...s[0]||(s[0]=[i("p",null," 要使用 Checkbox 组件,只需要配置 type: 'checkbox',选中意味着变量的值为 true。默认绑定变量的值会是 Boolean,选中为 true。 ",-1)])]),_:1}),s[11]||(s[11]=i("h2",{id:"禁用状态",tabindex:"-1"},[a("禁用状态 "),i("a",{class:"header-anchor",href:"#禁用状态","aria-label":"Permalink to “禁用状态”"},"​")],-1)),s[12]||(s[12]=i("p",null,"多选框不可用状态。",-1)),n(t,{type:"form",config:[{type:"checkbox",name:"checkbox",text:"选项",disabled:()=>!0}]},{source:h(()=>[...s[1]||(s[1]=[i("p",null," 设置 disabled 属性即可,它接受一个 Boolean,true 为禁用,也可以接受一个返回 Boolean 的函数。 ",-1)])]),_:1}),s[13]||(s[13]=i("h2",{id:"自定义选中值",tabindex:"-1"},[a("自定义选中值 "),i("a",{class:"header-anchor",href:"#自定义选中值","aria-label":"Permalink to “自定义选中值”"},"​")],-1)),s[14]||(s[14]=i("p",null,[a("通过 "),i("code",null,"activeValue"),a(" 和 "),i("code",null,"inactiveValue"),a(" 自定义选中和未选中时的值。")],-1)),n(t,{type:"form",config:[{type:"checkbox",name:"checkbox",text:"选项",activeValue:"yes",inactiveValue:"no"}]},{source:h(()=>[...s[2]||(s[2]=[i("p",null," 设置 activeValue 为选中时的值,inactiveValue 为未选中时的值。 ",-1)])]),_:1}),s[15]||(s[15]=i("h2",{id:"数值类型",tabindex:"-1"},[a("数值类型 "),i("a",{class:"header-anchor",href:"#数值类型","aria-label":"Permalink to “数值类型”"},"​")],-1)),s[16]||(s[16]=i("p",null,[a("当需要绑定数值类型时,可以使用 "),i("code",null,"filter: 'number'"),a(" 配合默认的 activeValue/inactiveValue。")],-1)),n(t,{type:"form",config:[{type:"checkbox",name:"checkbox",text:"选项",filter:"number"}]},{source:h(()=>[...s[3]||(s[3]=[i("p",null," 配置 filter 为 'number' 时,默认 activeValue 为 1,inactiveValue 为 0。 ",-1)])]),_:1}),s[17]||(s[17]=i("h2",{id:"使用-label-显示",tabindex:"-1"},[a("使用 Label 显示 "),i("a",{class:"header-anchor",href:"#使用-label-显示","aria-label":"Permalink to “使用 Label 显示”"},"​")],-1)),s[18]||(s[18]=i("p",null,[a("通过 "),i("code",null,"useLabel"),a(" 属性控制是否使用 label 作为显示内容。")],-1)),n(t,{type:"form",config:[{type:"checkbox",name:"checkbox",text:"选项",useLabel:!0}]},{source:h(()=>[...s[4]||(s[4]=[i("p",null," 设置 useLabel 为 true 时,不显示 text 内容,由外部 label 控制显示。 ",-1)])]),_:1}),s[19]||(s[19]=i("h2",{id:"多选框组",tabindex:"-1"},[a("多选框组 "),i("a",{class:"header-anchor",href:"#多选框组","aria-label":"Permalink to “多选框组”"},"​")],-1)),s[20]||(s[20]=i("p",null,"适用于多个勾选框绑定到同一个数组的情景,通过是否勾选来表示这一组选项中选中的项。",-1)),n(t,{type:"form",config:[{type:"checkbox-group",name:"checkbox",text:"选项",options:[{text:"选项1",value:1},{text:"选项2",value:2},{text:"选项3",value:3}]}]},{source:h(()=>[...s[5]||(s[5]=[i("p",null," checkbox-group 元素能把多个 checkbox 管理为一组,绑定值为数组类型。 ",-1)])]),_:1}),s[21]||(s[21]=i("h2",{id:"多选框组禁用选项",tabindex:"-1"},[a("多选框组禁用选项 "),i("a",{class:"header-anchor",href:"#多选框组禁用选项","aria-label":"Permalink to “多选框组禁用选项”"},"​")],-1)),s[22]||(s[22]=i("p",null,[a("在选项中设置 "),i("code",null,"disabled"),a(" 可禁用单个选项。")],-1)),n(t,{type:"form",config:[{type:"checkbox-group",name:"checkbox",text:"选项",options:[{text:"选项1",value:1},{text:"选项2",value:2,disabled:!0},{text:"选项3",value:3}]}]},{source:h(()=>[...s[6]||(s[6]=[i("p",null," 在 options 中设置 disabled: true 可禁用该选项。 ",-1)])]),_:1}),s[23]||(s[23]=d(`

动态选项

options 支持函数形式,可根据表单状态动态生成选项。

typescript
{
  type: 'checkbox-group',
  name: 'checkbox',
  text: '选项',
  options: (mForm, { model, formValue }) => {
    // 根据表单值动态返回选项
    return [
      { text: '选项A', value: 'a' },
      { text: '选项B', value: 'b' }
    ];
  }
}

Checkbox Attributes

参数说明类型可选值默认值
name绑定值string
text表单标签string
disabled是否禁用boolean / FilterFunctionfalse
activeValue选中时的值string / numbertrue(filter 为 'number' 时默认 1)
inactiveValue未选中时的值string / numberfalse(filter 为 'number' 时默认 0)
useLabel是否使用外部 label 显示booleanfalse
filter值过滤器'number' / Function
onChange值变化时触发的函数OnChangeHandler
查看 FilterFunction / OnChangeHandler 及关联类型定义
ts
export type FilterFunction<T = boolean> = (
  mForm: FormState | undefined,
  data: {
    model: FormValue;
    values: FormValue;
    parent?: FormValue;
    formValue: FormValue;
    prop: string;
    config: any;
    index?: number;
    getFormValue: (prop: string) => any;
  },
) => T;
ts
export type OnChangeHandler = (mForm: FormState | undefined, value: any, data: OnChangeHandlerData) => any;
ts
export interface OnChangeHandlerData {
  model: FormValue;
  values?: Readonly<FormValue> | null;
  parent?: FormValue;
  formValue?: FormValue;
  config: Readonly<any>;
  prop: string;
  changeRecords: ChangeRecord[];
  setModel: (prop: string, value: any) => void;
  setFormValue: (prop: string, value: any) => void;
}
ts
export interface ChangeRecord {
  propPath?: string;
  value: any;
}
ts
export type FormValue = Record<string | number, any>;

配置类型

查看 CheckboxConfig / CheckboxGroupConfig 配置类型定义
ts
export interface CheckboxConfig extends FormItem {
  type: 'checkbox';
  activeValue?: number | string;
  inactiveValue?: number | string;
  useLabel?: boolean;
}
ts
export interface CheckboxGroupConfig extends FormItem {
  type: 'checkbox-group' | 'checkboxGroup';
  options: CheckboxGroupOption[] | FilterFunction<CheckboxGroupOption[]>;
}
ts
export interface FormItem {
  /** vnode的key值,默认是遍历数组时的index */
  __key?: string | number;
  /** 表单域标签的的宽度,例如 '50px'。支持 auto。 */
  labelWidth?: string | number;
  /** label 标签的title属性 */
  labelTitle?: string;
  className?: string;
  /** 字段名 */
  name?: string | number;
  /** 额外的提示信息,和 help 类似,当提示文案同时出现时,可以使用这个。 */
  extra?: string | FilterFunction<string>;
  /** 配置提示信息 */
  tooltip?: ToolTipConfigType | FilterFunction<ToolTipConfigType>;
  /** 是否置灰 */
  disabled?: boolean | FilterFunction;
  /** 使用表单中的值作为key,例如配置了text,则使用model.text作为key */
  key?: string;
  /** 是否显示 */
  display?: boolean | 'expand' | FilterFunction<boolean | 'expand'>;
  /** 值发生改变时调用的方法 */
  onChange?: OnChangeHandler;
  /** label 标签的文本 */
  text?: string | FilterFunction<string>;
  /** 右侧感叹号 */
  tip?: string;

  filter?: 'number' | OnChangeHandler;
  /** 是否去除首尾空格 */
  trim?: boolean;
  /** 默认值 */
  defaultValue?: any | DefaultValueFunction;
  /** 表单验证规则 */
  rules?: Rule[];
  extensible?: boolean;
  dynamicKey?: string;
  /** 是否需要显示\`展开更多配置\` */
  expand?: boolean;
  style?: Record<string, any>;
  fieldStyle?: Record<string, any>;
  labelPosition?: 'top' | 'left' | 'right';
}

CheckboxGroup Attributes

参数说明类型可选值默认值
name绑定值string
text表单标签string
disabled是否禁用boolean / FilterFunctionfalse
options选项列表Array / FilterFunction
onChange值变化时触发的函数OnChangeHandler

options item

参数说明类型可选值默认值
value选项的值any
text选项的标签string
disabled是否禁用该选项booleanfalse
`,12))])}const A=l(r,[["render",g]]);export{b as __pageData,A as default};