Checkbox 多选框
===
[](https://jaywcjlove.github.io/#/sponsor)
[](https://t.me/s/AwesomeMacApps)
[](https://x.com/jaywcjlove)
[](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-checkbox/file/README.md)
[](https://www.npmjs.com/package/@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 = () => (
)
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) | - |