mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 11:32:52 +08:00
fix: checkbox group support options other than string (#33678)
* fix: checkbox group support options other than string * docs: update checkbox docs * fix: radio group support value other than string * fix: only support number and string * docs: update group options type
This commit is contained in:
parent
7a3f7ba58c
commit
29f68f9e28
@ -17,7 +17,7 @@ export interface CheckboxOptionType {
|
|||||||
export interface AbstractCheckboxGroupProps {
|
export interface AbstractCheckboxGroupProps {
|
||||||
prefixCls?: string;
|
prefixCls?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
options?: Array<CheckboxOptionType | string>;
|
options?: Array<CheckboxOptionType | string | number>;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
style?: React.CSSProperties;
|
style?: React.CSSProperties;
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ const InternalCheckboxGroup: React.ForwardRefRenderFunction<HTMLDivElement, Chec
|
|||||||
|
|
||||||
const getOptions = () =>
|
const getOptions = () =>
|
||||||
options.map(option => {
|
options.map(option => {
|
||||||
if (typeof option === 'string') {
|
if (typeof option === 'string' || typeof option === 'number') {
|
||||||
return {
|
return {
|
||||||
label: option,
|
label: option,
|
||||||
value: option,
|
value: option,
|
||||||
|
@ -213,4 +213,13 @@ describe('CheckboxGroup', () => {
|
|||||||
/>,
|
/>,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support number option', () => {
|
||||||
|
const onChange = jest.fn();
|
||||||
|
|
||||||
|
const wrapper = mount(<Checkbox.Group options={[1, 'Pear', 'Orange']} onChange={onChange} />);
|
||||||
|
|
||||||
|
wrapper.find('.ant-checkbox-input').at(0).simulate('change');
|
||||||
|
expect(onChange).toHaveBeenCalledWith([1]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -34,7 +34,7 @@ Checkbox component.
|
|||||||
| defaultValue | Default selected value | string\[] | \[] | |
|
| defaultValue | Default selected value | string\[] | \[] | |
|
||||||
| disabled | If disable all checkboxes | boolean | false | |
|
| disabled | If disable all checkboxes | boolean | false | |
|
||||||
| name | The `name` property of all `input[type="checkbox"]` children | string | - | |
|
| name | The `name` property of all `input[type="checkbox"]` children | string | - | |
|
||||||
| options | Specifies options | string\[] \| Option\[] | \[] | |
|
| options | Specifies options | string\[] \| number\[] \| Option\[] | \[] | |
|
||||||
| value | Used for setting the currently selected value | string\[] | \[] | |
|
| value | Used for setting the currently selected value | string\[] | \[] | |
|
||||||
| onChange | The callback function that is triggered when the state changes | function(checkedValue) | - | |
|
| onChange | The callback function that is triggered when the state changes | function(checkedValue) | - | |
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ Checkbox component.
|
|||||||
|
|
||||||
#### Checkbox
|
#### Checkbox
|
||||||
|
|
||||||
| Name | Description | Version |
|
| Name | Description | Version |
|
||||||
| --- | --- | --- |
|
| ------- | ------------ | ------- |
|
||||||
| blur() | Remove focus | |
|
| blur() | Remove focus | |
|
||||||
| focus() | Get focus | |
|
| focus() | Get focus | |
|
||||||
|
@ -19,14 +19,14 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8nbVbHEm_/CheckBox.svg
|
|||||||
|
|
||||||
#### Checkbox
|
#### Checkbox
|
||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
||||||
| --- | --- | --- | --- | --- |
|
| -------------- | --------------------------------------- | ----------------- | ------ | ---- |
|
||||||
| autoFocus | 自动获取焦点 | boolean | false | |
|
| autoFocus | 自动获取焦点 | boolean | false | |
|
||||||
| checked | 指定当前是否选中 | boolean | false | |
|
| checked | 指定当前是否选中 | boolean | false | |
|
||||||
| defaultChecked | 初始是否选中 | boolean | false | |
|
| defaultChecked | 初始是否选中 | boolean | false | |
|
||||||
| disabled | 失效状态 | boolean | false | |
|
| disabled | 失效状态 | boolean | false | |
|
||||||
| indeterminate | 设置 indeterminate 状态,只负责样式控制 | boolean | false | |
|
| indeterminate | 设置 indeterminate 状态,只负责样式控制 | boolean | false | |
|
||||||
| onChange | 变化时回调函数 | function(e:Event) | - | |
|
| onChange | 变化时回调函数 | function(e:Event) | - | |
|
||||||
|
|
||||||
#### Checkbox Group
|
#### Checkbox Group
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8nbVbHEm_/CheckBox.svg
|
|||||||
| defaultValue | 默认选中的选项 | string\[] | \[] | |
|
| defaultValue | 默认选中的选项 | string\[] | \[] | |
|
||||||
| disabled | 整组失效 | boolean | false | |
|
| disabled | 整组失效 | boolean | false | |
|
||||||
| name | CheckboxGroup 下所有 `input[type="checkbox"]` 的 `name` 属性 | string | - | |
|
| name | CheckboxGroup 下所有 `input[type="checkbox"]` 的 `name` 属性 | string | - | |
|
||||||
| options | 指定可选项 | string\[] \| Option\[] | \[] | |
|
| options | 指定可选项 | string\[] \| number\[] \| Option\[] | \[] | |
|
||||||
| value | 指定选中的选项 | string\[] | \[] | |
|
| value | 指定选中的选项 | string\[] | \[] | |
|
||||||
| onChange | 变化时回调函数 | function(checkedValue) | - | |
|
| onChange | 变化时回调函数 | function(checkedValue) | - | |
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ interface Option {
|
|||||||
|
|
||||||
#### Checkbox
|
#### Checkbox
|
||||||
|
|
||||||
| 名称 | 描述 | 版本 |
|
| 名称 | 描述 | 版本 |
|
||||||
| --- | --- | --- |
|
| ------- | -------- | ---- |
|
||||||
| blur() | 移除焦点 | |
|
| blur() | 移除焦点 | |
|
||||||
| focus() | 获取焦点 | |
|
| focus() | 获取焦点 | |
|
||||||
|
@ -50,11 +50,11 @@ const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>((props, ref
|
|||||||
if (options && options.length > 0) {
|
if (options && options.length > 0) {
|
||||||
const optionsPrefixCls = optionType === 'button' ? `${prefixCls}-button` : prefixCls;
|
const optionsPrefixCls = optionType === 'button' ? `${prefixCls}-button` : prefixCls;
|
||||||
childrenToRender = options.map(option => {
|
childrenToRender = options.map(option => {
|
||||||
if (typeof option === 'string') {
|
if (typeof option === 'string' || typeof option === 'number') {
|
||||||
// 此处类型自动推导为 string
|
// 此处类型自动推导为 string
|
||||||
return (
|
return (
|
||||||
<Radio
|
<Radio
|
||||||
key={option}
|
key={option.toString()}
|
||||||
prefixCls={optionsPrefixCls}
|
prefixCls={optionsPrefixCls}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
value={option}
|
value={option}
|
||||||
|
@ -34,7 +34,7 @@ Radio group can wrap a group of `Radio`。
|
|||||||
| defaultValue | Default selected value | any | - | |
|
| defaultValue | Default selected value | any | - | |
|
||||||
| disabled | Disable all radio buttons | boolean | false | |
|
| disabled | Disable all radio buttons | boolean | false | |
|
||||||
| name | The `name` property of all `input[type="radio"]` children | string | - | |
|
| name | The `name` property of all `input[type="radio"]` children | string | - | |
|
||||||
| options | Set children optional | string\[] \| Array<{ label: string value: string disabled?: boolean }> | - | |
|
| options | Set children optional | string\[] \| number\[] \| Array<{ label: string value: string disabled?: boolean }> | - | |
|
||||||
| optionType | Set Radio optionType | `default` \| `button` | `default` | 4.4.0 |
|
| optionType | Set Radio optionType | `default` \| `button` | `default` | 4.4.0 |
|
||||||
| size | The size of radio button style | `large` \| `middle` \| `small` | - | |
|
| size | The size of radio button style | `large` \| `middle` \| `small` | - | |
|
||||||
| value | Used for setting the currently selected value | any | - | |
|
| value | Used for setting the currently selected value | any | - | |
|
||||||
@ -44,7 +44,7 @@ Radio group can wrap a group of `Radio`。
|
|||||||
|
|
||||||
### Radio
|
### Radio
|
||||||
|
|
||||||
| Name | Description |
|
| Name | Description |
|
||||||
| --- | --- |
|
| ------- | ------------ |
|
||||||
| blur() | Remove focus |
|
| blur() | Remove focus |
|
||||||
| focus() | Get focus |
|
| focus() | Get focus |
|
||||||
|
@ -17,13 +17,13 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8cYb5seNB/Radio.svg
|
|||||||
|
|
||||||
### Radio/Radio.Button
|
### Radio/Radio.Button
|
||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 |
|
| 参数 | 说明 | 类型 | 默认值 |
|
||||||
| --- | --- | --- | --- |
|
| -------------- | --------------------------------- | ------- | ------ |
|
||||||
| autoFocus | 自动获取焦点 | boolean | false |
|
| autoFocus | 自动获取焦点 | boolean | false |
|
||||||
| checked | 指定当前是否选中 | boolean | false |
|
| checked | 指定当前是否选中 | boolean | false |
|
||||||
| defaultChecked | 初始是否选中 | boolean | false |
|
| defaultChecked | 初始是否选中 | boolean | false |
|
||||||
| disabled | 禁用 Radio | boolean | false |
|
| disabled | 禁用 Radio | boolean | false |
|
||||||
| value | 根据 value 进行比较,判断是否选中 | any | - |
|
| value | 根据 value 进行比较,判断是否选中 | any | - |
|
||||||
|
|
||||||
### RadioGroup
|
### RadioGroup
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8cYb5seNB/Radio.svg
|
|||||||
| defaultValue | 默认选中的值 | any | - | | |
|
| defaultValue | 默认选中的值 | any | - | | |
|
||||||
| disabled | 禁选所有子单选器 | boolean | false | | |
|
| disabled | 禁选所有子单选器 | boolean | false | | |
|
||||||
| name | RadioGroup 下所有 `input[type="radio"]` 的 `name` 属性 | string | - | | |
|
| name | RadioGroup 下所有 `input[type="radio"]` 的 `name` 属性 | string | - | | |
|
||||||
| options | 以配置形式设置子元素 | string\[] \| Array<{ label: string value: string disabled?: boolean }> | - | | |
|
| options | 以配置形式设置子元素 | string\[] \| number\[] \| Array<{ label: string value: string disabled?: boolean }> | - | | |
|
||||||
| optionType | 用于设置 Radio `options` 类型 | `default` \| `button` | `default` | 4.4.0 | |
|
| optionType | 用于设置 Radio `options` 类型 | `default` \| `button` | `default` | 4.4.0 | |
|
||||||
| size | 大小,只对按钮样式生效 | `large` \| `middle` \| `small` | - | | |
|
| size | 大小,只对按钮样式生效 | `large` \| `middle` \| `small` | - | | |
|
||||||
| value | 用于设置当前选中的值 | any | - | | |
|
| value | 用于设置当前选中的值 | any | - | | |
|
||||||
@ -45,7 +45,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8cYb5seNB/Radio.svg
|
|||||||
|
|
||||||
### Radio
|
### Radio
|
||||||
|
|
||||||
| 名称 | 描述 |
|
| 名称 | 描述 |
|
||||||
| --- | --- |
|
| ------- | -------- |
|
||||||
| blur() | 移除焦点 |
|
| blur() | 移除焦点 |
|
||||||
| focus() | 获取焦点 |
|
| focus() | 获取焦点 |
|
||||||
|
Loading…
Reference in New Issue
Block a user