From 15969affdad1fc4e4b8401089926979dfc438418 Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Wed, 12 Jan 2022 22:57:43 +0800 Subject: [PATCH] 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 --- components/checkbox/Group.tsx | 4 ++-- components/checkbox/__tests__/group.test.js | 9 +++++++ components/checkbox/index.en-US.md | 10 ++++---- components/checkbox/index.zh-CN.md | 26 ++++++++++----------- components/radio/group.tsx | 4 ++-- components/radio/index.en-US.md | 10 ++++---- components/radio/index.zh-CN.md | 22 ++++++++--------- 7 files changed, 47 insertions(+), 38 deletions(-) diff --git a/components/checkbox/Group.tsx b/components/checkbox/Group.tsx index 360bc08d1d0f..faca13268cc7 100644 --- a/components/checkbox/Group.tsx +++ b/components/checkbox/Group.tsx @@ -17,7 +17,7 @@ export interface CheckboxOptionType { export interface AbstractCheckboxGroupProps { prefixCls?: string; className?: string; - options?: Array; + options?: Array; disabled?: boolean; style?: React.CSSProperties; } @@ -69,7 +69,7 @@ const InternalCheckboxGroup: React.ForwardRefRenderFunction options.map(option => { - if (typeof option === 'string') { + if (typeof option === 'string' || typeof option === 'number') { return { label: option, value: option, diff --git a/components/checkbox/__tests__/group.test.js b/components/checkbox/__tests__/group.test.js index 6cf014fe3e51..b944b5bce807 100644 --- a/components/checkbox/__tests__/group.test.js +++ b/components/checkbox/__tests__/group.test.js @@ -213,4 +213,13 @@ describe('CheckboxGroup', () => { />, ); }); + + it('should support number option', () => { + const onChange = jest.fn(); + + const wrapper = mount(); + + wrapper.find('.ant-checkbox-input').at(0).simulate('change'); + expect(onChange).toHaveBeenCalledWith([1]); + }); }); diff --git a/components/checkbox/index.en-US.md b/components/checkbox/index.en-US.md index 230d188c5e5b..29d8d03650af 100644 --- a/components/checkbox/index.en-US.md +++ b/components/checkbox/index.en-US.md @@ -34,7 +34,7 @@ Checkbox component. | defaultValue | Default selected value | string\[] | \[] | | | disabled | If disable all checkboxes | boolean | false | | | 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\[] | \[] | | | onChange | The callback function that is triggered when the state changes | function(checkedValue) | - | | @@ -42,7 +42,7 @@ Checkbox component. #### Checkbox -| Name | Description | Version | -| --- | --- | --- | -| blur() | Remove focus | | -| focus() | Get focus | | +| Name | Description | Version | +| ------- | ------------ | ------- | +| blur() | Remove focus | | +| focus() | Get focus | | diff --git a/components/checkbox/index.zh-CN.md b/components/checkbox/index.zh-CN.md index 3bb66a7af8be..991aac142ae5 100644 --- a/components/checkbox/index.zh-CN.md +++ b/components/checkbox/index.zh-CN.md @@ -19,14 +19,14 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8nbVbHEm_/CheckBox.svg #### Checkbox -| 参数 | 说明 | 类型 | 默认值 | 版本 | -| --- | --- | --- | --- | --- | -| autoFocus | 自动获取焦点 | boolean | false | | -| checked | 指定当前是否选中 | boolean | false | | -| defaultChecked | 初始是否选中 | boolean | false | | -| disabled | 失效状态 | boolean | false | | -| indeterminate | 设置 indeterminate 状态,只负责样式控制 | boolean | false | | -| onChange | 变化时回调函数 | function(e:Event) | - | | +| 参数 | 说明 | 类型 | 默认值 | 版本 | +| -------------- | --------------------------------------- | ----------------- | ------ | ---- | +| autoFocus | 自动获取焦点 | boolean | false | | +| checked | 指定当前是否选中 | boolean | false | | +| defaultChecked | 初始是否选中 | boolean | false | | +| disabled | 失效状态 | boolean | false | | +| indeterminate | 设置 indeterminate 状态,只负责样式控制 | boolean | false | | +| onChange | 变化时回调函数 | function(e:Event) | - | | #### Checkbox Group @@ -35,7 +35,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8nbVbHEm_/CheckBox.svg | defaultValue | 默认选中的选项 | string\[] | \[] | | | disabled | 整组失效 | boolean | false | | | name | CheckboxGroup 下所有 `input[type="checkbox"]` 的 `name` 属性 | string | - | | -| options | 指定可选项 | string\[] \| Option\[] | \[] | | +| options | 指定可选项 | string\[] \| number\[] \| Option\[] | \[] | | | value | 指定选中的选项 | string\[] | \[] | | | onChange | 变化时回调函数 | function(checkedValue) | - | | @@ -53,7 +53,7 @@ interface Option { #### Checkbox -| 名称 | 描述 | 版本 | -| --- | --- | --- | -| blur() | 移除焦点 | | -| focus() | 获取焦点 | | +| 名称 | 描述 | 版本 | +| ------- | -------- | ---- | +| blur() | 移除焦点 | | +| focus() | 获取焦点 | | diff --git a/components/radio/group.tsx b/components/radio/group.tsx index d9bcb376b942..1d768ee077dc 100644 --- a/components/radio/group.tsx +++ b/components/radio/group.tsx @@ -50,11 +50,11 @@ const RadioGroup = React.forwardRef((props, ref if (options && options.length > 0) { const optionsPrefixCls = optionType === 'button' ? `${prefixCls}-button` : prefixCls; childrenToRender = options.map(option => { - if (typeof option === 'string') { + if (typeof option === 'string' || typeof option === 'number') { // 此处类型自动推导为 string return ( | - | | +| options | Set children optional | string\[] \| number\[] \| Array<{ label: string value: string disabled?: boolean }> | - | | | optionType | Set Radio optionType | `default` \| `button` | `default` | 4.4.0 | | size | The size of radio button style | `large` \| `middle` \| `small` | - | | | value | Used for setting the currently selected value | any | - | | @@ -44,7 +44,7 @@ Radio group can wrap a group of `Radio`。 ### Radio -| Name | Description | -| --- | --- | -| blur() | Remove focus | -| focus() | Get focus | +| Name | Description | +| ------- | ------------ | +| blur() | Remove focus | +| focus() | Get focus | diff --git a/components/radio/index.zh-CN.md b/components/radio/index.zh-CN.md index 82e31db6ac07..404830a478cc 100644 --- a/components/radio/index.zh-CN.md +++ b/components/radio/index.zh-CN.md @@ -17,13 +17,13 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8cYb5seNB/Radio.svg ### Radio/Radio.Button -| 参数 | 说明 | 类型 | 默认值 | -| --- | --- | --- | --- | -| autoFocus | 自动获取焦点 | boolean | false | -| checked | 指定当前是否选中 | boolean | false | -| defaultChecked | 初始是否选中 | boolean | false | -| disabled | 禁用 Radio | boolean | false | -| value | 根据 value 进行比较,判断是否选中 | any | - | +| 参数 | 说明 | 类型 | 默认值 | +| -------------- | --------------------------------- | ------- | ------ | +| autoFocus | 自动获取焦点 | boolean | false | +| checked | 指定当前是否选中 | boolean | false | +| defaultChecked | 初始是否选中 | boolean | false | +| disabled | 禁用 Radio | boolean | false | +| value | 根据 value 进行比较,判断是否选中 | any | - | ### RadioGroup @@ -35,7 +35,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8cYb5seNB/Radio.svg | defaultValue | 默认选中的值 | any | - | | | | disabled | 禁选所有子单选器 | boolean | false | | | | 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 | | | size | 大小,只对按钮样式生效 | `large` \| `middle` \| `small` | - | | | | value | 用于设置当前选中的值 | any | - | | | @@ -45,7 +45,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/8cYb5seNB/Radio.svg ### Radio -| 名称 | 描述 | -| --- | --- | -| blur() | 移除焦点 | +| 名称 | 描述 | +| ------- | -------- | +| blur() | 移除焦点 | | focus() | 获取焦点 |