使用 我的应用 也是一种 支持 我的方式:
DockLift: 点击 Dock 应用窗口回到当前屏幕 Zipora: Zip/RAR/7Z 解压工具 Scap: Screenshot & Markup Edit Screen Test Deskmark Keyzer Vidwall Hub VidCrop Vidwall Mousio Hint Mousio Musicer Audioer FileSentinel FocusCursor Videoer KeyClicker DayBar Iconed Menuist Quick RSS Quick RSS Web Serve Copybook Generator DevTutor for SwiftUI RegexMate Time Passage Iconize Folder Textsound Saver Create Custom Symbols DevHub Resume Revise Palette Genius Symbol Scribe

Checkbox 多选框 === [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) [![Telegram Channel](https://img.shields.io/badge/%20-Telegram-00618f.svg?logo=Telegram)](https://t.me/s/AwesomeMacApps) [![Follow On X](https://img.shields.io/badge/Follow%20on%20X-333333?logo=x&logoColor=white)](https://x.com/jaywcjlove) [![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-checkbox/file/README.md) [![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-checkbox.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-checkbox) [![npm version](https://img.shields.io/npm/v/@uiw/react-checkbox.svg?label=@uiw/react-checkbox)](https://npmjs.com/@uiw/react-checkbox) 一组备选项中进行多选 ```jsx import { Checkbox } from 'uiw'; // or import Checkbox from '@uiw/react-checkbox'; ``` ### 基础用法 单独使用可以表示两种状态之间的切换,半选中只是样式上的表现。 ```jsx mdx:preview&bg=#fff import React from 'react'; import { Checkbox } from 'uiw'; export default function Demo() { return(
{ console.log(e.target.checked) console.log(e.target) }}>未选中 选中
); } ``` ### Form 中使用 Checkbox ```jsx mdx:preview&bg=#fff import React from 'react'; import { Form, Checkbox, Row, Col, Button, Notify } from 'uiw'; const Demo = () => (
{ console.log('onChange~~~:::', initial, current); }} onSubmit={({initial, current}) => { console.log('onSubmit~~~:::', initial, current) Notify.success({ title: '提交成功!', description: `选项为:【${current.checkbox}】【${JSON.stringify(current.checkboxGroup)}】,提交完成,将自动填充初始化值!`, }); }} fields={{ checkbox: { inline: true, initialValue: true, label: '单选条件', children: 四川菜, validator: (value) => { return !value ? '必须统一服务条款' : null; }, }, checkboxGroup: { initialValue: ['四川菜'], label: '选择你想吃的菜', children: (
菜系
四川菜 湖北菜 西北菜 新疆菜 东北菜
家常菜
红烧武昌鱼 麻婆豆腐 北京烤鸭
), }, }} > {({ fields, state, canSubmit }) => { return (
{fields.checkbox} {fields.checkboxGroup}
                  {JSON.stringify(state.current, null, 2)}
                
) }}
) export default Demo; ``` ### 禁用样式 通过设置 `disabled` 属性来禁用多选框。 ```jsx mdx:preview&bg=#fff import React from 'react'; import { Checkbox } from 'uiw'; export default function Demo() { return(
未选中禁用 选中禁用 半选中禁用
); } ``` ### 全选 ```jsx mdx:preview&bg=#fff import React from 'react'; import { Checkbox, Divider } from 'uiw'; const checkedList = [ '四川菜', '湖北菜', '西北菜', '新疆菜', '东北菜' ]; class Demo extends React.Component { constructor(props) { super(props); this.state = { value: ['四川菜', '新疆菜'], indeterminate: true, checkAll: false, }; } onChange(e, list) { console.log('list', list); console.log('list', checkedList); this.setState({ value: list, indeterminate: !!list.length && (list.length < checkedList.length), checkAll: list.length === checkedList.length, }); } onCheckAllChange(e) { this.setState({ value: e.target.checked ? checkedList : [], indeterminate: false, checkAll: e.target.checked, }); } render() { const { indeterminate, checkAll } = this.state; return (
全选 {checkedList.map((item, idx) => { return ( {item} ) })}
) } } export default Demo; ``` ### 多选组 方便的从数组生成 `Checkbox` 组。 ```jsx mdx:preview&bg=#fff import React from 'react'; import { Checkbox, Divider, Button } from 'uiw'; class Demo extends React.Component { constructor(props) { super(props); this.state = { value: ['四川菜', '新疆菜'], }; } onChange(e, list) { console.log('Value:-->', e.target.value, list); this.setState({ value: list }); } render() { return (
川菜
四川菜 湖北菜
本帮江浙菜
西北菜 新疆菜
家常菜
东北菜 北京烤鸭
) } } export default Demo; ``` ### 控制组件 通过 `checked` 属性改变 `Checkbox` 组件状态。 ```jsx mdx:preview&bg=#fff import React from 'react'; import { Checkbox, Button } from 'uiw'; class Demo extends React.Component { constructor(props) { super(props); this.state = { checked: true } } onChange(e) { console.log('e.target.checked:', e.target.checked); this.setState({ checked: e.target.checked }); } render() { return (
) } } export default Demo; ``` ## Checkbox | 参数 | 说明 | 类型 | 默认值 | |--------- |-------- |---------- |-------- | | options | 指定当前是否选中 | Boolean | false | | disabled | 禁用 | Boolean | false | | onChange | 变化时回调函数 | Function(e:Event, checked:Boolean) | - | | checked | 指定当前是否选中 | Boolean | false | | indeterminate | 半选中,只负责样式控制 | Boollean | false | ## Checkbox.Group | 参数 | 说明 | 类型 | 默认值 | |--------- |-------- |---------- |-------- | | name | 隐藏输入的名称,在表单中使用时很有用 | string | - | | value | 控制时 `select` 的值必须与 `onChange` 函数一起使用才能更新 `select` 的值 | Array | `[]` | | onChange | 变化时回调函数 | Function(e:Event, checkedValues: Array) | - |