Checkbox多选框
多选框。
何时使用#
在一组可选项中进行多项选择时;
单独使用可以表示两种状态之间的切换,和
switch
类似。区别在于切换switch
会直接触发状态改变,而checkbox
一般用于状态标记,需要和提交操作配合。
代码演示
TypeScript
JavaScript
import { Checkbox } from 'infrad';
import type { CheckboxChangeEvent } from 'infrad/es/checkbox';
import React from 'react';
const onChange = (e: CheckboxChangeEvent) => {
console.log(`checked = ${e.target.checked}`);
};
const App: React.FC = () => <Checkbox onChange={onChange}>Checkbox</Checkbox>;
export default App;
TypeScript
JavaScript
import { Button, Checkbox } from 'infrad';
import type { CheckboxChangeEvent } from 'infrad/es/checkbox';
import React, { useState } from 'react';
const App: React.FC = () => {
const [checked, setChecked] = useState(true);
const [disabled, setDisabled] = useState(false);
const toggleChecked = () => {
setChecked(!checked);
};
const toggleDisable = () => {
setDisabled(!disabled);
};
const onChange = (e: CheckboxChangeEvent) => {
console.log('checked = ', e.target.checked);
setChecked(e.target.checked);
};
const label = `${checked ? 'Checked' : 'Unchecked'}-${disabled ? 'Disabled' : 'Enabled'}`;
return (
<>
<p style={{ marginBottom: '20px' }}>
<Checkbox checked={checked} disabled={disabled} onChange={onChange}>
{label}
</Checkbox>
</p>
<p>
<Button type="primary" size="small" onClick={toggleChecked}>
{!checked ? 'Check' : 'Uncheck'}
</Button>
<Button style={{ margin: '0 10px' }} type="primary" size="small" onClick={toggleDisable}>
{!disabled ? 'Disable' : 'Enable'}
</Button>
</p>
</>
);
};
export default App;
TypeScript
JavaScript
import { Checkbox, Divider } from 'infrad';
import type { CheckboxChangeEvent } from 'infrad/es/checkbox';
import type { CheckboxValueType } from 'infrad/es/checkbox/Group';
import React, { useState } from 'react';
const CheckboxGroup = Checkbox.Group;
const plainOptions = ['Apple', 'Pear', 'Orange'];
const defaultCheckedList = ['Apple', 'Orange'];
const App: React.FC = () => {
const [checkedList, setCheckedList] = useState<CheckboxValueType[]>(defaultCheckedList);
const [indeterminate, setIndeterminate] = useState(true);
const [checkAll, setCheckAll] = useState(false);
const onChange = (list: CheckboxValueType[]) => {
setCheckedList(list);
setIndeterminate(!!list.length && list.length < plainOptions.length);
setCheckAll(list.length === plainOptions.length);
};
const onCheckAllChange = (e: CheckboxChangeEvent) => {
setCheckedList(e.target.checked ? plainOptions : []);
setIndeterminate(false);
setCheckAll(e.target.checked);
};
return (
<>
<Checkbox indeterminate={indeterminate} onChange={onCheckAllChange} checked={checkAll}>
Check all
</Checkbox>
<Divider />
<CheckboxGroup options={plainOptions} value={checkedList} onChange={onChange} />
</>
);
};
export default App;
TypeScript
JavaScript
import { Checkbox } from 'infrad';
import React from 'react';
const App: React.FC = () => (
<>
<Checkbox defaultChecked={false} disabled />
<br />
<Checkbox defaultChecked disabled />
</>
);
export default App;
TypeScript
JavaScript
import { Checkbox } from 'infrad';
import type { CheckboxValueType } from 'infrad/es/checkbox/Group';
import React from 'react';
const onChange = (checkedValues: CheckboxValueType[]) => {
console.log('checked = ', checkedValues);
};
const plainOptions = ['Apple', 'Pear', 'Orange'];
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Pear', value: 'Pear' },
{ label: 'Orange', value: 'Orange' },
];
const optionsWithDisabled = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Pear', value: 'Pear' },
{ label: 'Orange', value: 'Orange', disabled: false },
];
const App: React.FC = () => (
<>
<Checkbox.Group options={plainOptions} defaultValue={['Apple']} onChange={onChange} />
<br />
<br />
<Checkbox.Group options={options} defaultValue={['Pear']} onChange={onChange} />
<br />
<br />
<Checkbox.Group
options={optionsWithDisabled}
disabled
defaultValue={['Apple']}
onChange={onChange}
/>
</>
);
export default App;
TypeScript
JavaScript
import { Checkbox, Col, Row } from 'infrad';
import type { CheckboxValueType } from 'infrad/es/checkbox/Group';
import React from 'react';
const onChange = (checkedValues: CheckboxValueType[]) => {
console.log('checked = ', checkedValues);
};
const App: React.FC = () => (
<Checkbox.Group style={{ width: '100%' }} onChange={onChange}>
<Row>
<Col span={8}>
<Checkbox value="A">A</Checkbox>
</Col>
<Col span={8}>
<Checkbox value="B">B</Checkbox>
</Col>
<Col span={8}>
<Checkbox value="C">C</Checkbox>
</Col>
<Col span={8}>
<Checkbox value="D">D</Checkbox>
</Col>
<Col span={8}>
<Checkbox value="E">E</Checkbox>
</Col>
</Row>
</Checkbox.Group>
);
export default App;
API#
属性#
Checkbox#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
autoFocus | 自动获取焦点 | boolean | false | |
checked | 指定当前是否选中 | boolean | false | |
defaultChecked | 初始是否选中 | boolean | false | |
disabled | 失效状态 | boolean | false | |
indeterminate | 设置 indeterminate 状态,只负责样式控制 | boolean | false | |
onChange | 变化时回调函数 | function(e:Event) | - |
Checkbox Group#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
defaultValue | 默认选中的选项 | string[] | [] | |
disabled | 整组失效 | boolean | false | |
name | CheckboxGroup 下所有 input[type="checkbox"] 的 name 属性 | string | - | |
options | 指定可选项 | string[] | number[] | Option[] | [] | |
value | 指定选中的选项 | string[] | [] | |
onChange | 变化时回调函数 | function(checkedValue) | - |
Option#
interface Option {
label: string;
value: string;
disabled?: boolean;
}
方法#
Checkbox#
名称 | 描述 | 版本 |
---|---|---|
blur() | 移除焦点 | |
focus() | 获取焦点 |