Radiogroup with name (#7009)

* Add a name property for RadioGroup

* Add document for RadioGroup's name property

* Add test case for RadioGroup name property
This commit is contained in:
Randy 2017-07-31 11:33:47 +08:00 committed by 乐仪
parent 456cea652b
commit e4c5ab9c69
7 changed files with 132 additions and 0 deletions

View File

@ -675,6 +675,90 @@ exports[`renders ./components/radio/demo/radiogroup-options.md correctly 1`] = `
</div>
`;
exports[`renders ./components/radio/demo/radiogroup-with-name.md correctly 1`] = `
<div
class="ant-radio-group"
>
<label
class="ant-radio-wrapper ant-radio-wrapper-checked"
>
<span
class="ant-radio ant-radio-checked"
>
<input
checked=""
class="ant-radio-input"
name="radiogroup"
type="radio"
/>
<span
class="ant-radio-inner"
/>
</span>
<span>
A
</span>
</label>
<label
class="ant-radio-wrapper"
>
<span
class="ant-radio"
>
<input
class="ant-radio-input"
name="radiogroup"
type="radio"
/>
<span
class="ant-radio-inner"
/>
</span>
<span>
B
</span>
</label>
<label
class="ant-radio-wrapper"
>
<span
class="ant-radio"
>
<input
class="ant-radio-input"
name="radiogroup"
type="radio"
/>
<span
class="ant-radio-inner"
/>
</span>
<span>
C
</span>
</label>
<label
class="ant-radio-wrapper"
>
<span
class="ant-radio"
>
<input
class="ant-radio-input"
name="radiogroup"
type="radio"
/>
<span
class="ant-radio-inner"
/>
</span>
<span>
D
</span>
</label>
</div>
`;
exports[`renders ./components/radio/demo/size.md correctly 1`] = `
<div>
<div>

View File

@ -101,4 +101,15 @@ describe('Radio', () => {
expect(radios.length).toBe(3);
});
it('all children should have a name property', () => {
const GROUP_NAME = 'radiogroup';
const wrapper = mount(
createRadioGroup({ name: GROUP_NAME })
);
expect(wrapper.find('input[type="radio"]').forEach((el) => {
expect(el.props().name).toEqual(GROUP_NAME);
}));
});
});

View File

@ -0,0 +1,32 @@
---
order: 4
title:
zh-CN: 单选组合 - 配合 name 使用
en-US: RadioGroup with name
---
## zh-CN
可以为 RadioGroup 配置 `name` 参数,为组合内的 input 元素赋予相同的 `name` 属性,使浏览器把 RadioGroup 下的 Radio 真正看作是一组(例如可以通过方向键始终**在同一组内**更改选项)。
## en-US
Passing the `name` property to all `input[type="radio"]` that are in the same RadioGroup. It is usually used to let the browser see your RadioGroup as a real "group" and keep the default behavior. For example, using left/right keyboard arrow to change your selection that in the same RadioGroup.
```jsx
import { Radio } from 'antd';
const RadioGroup = Radio.Group;
function App() {
return (
<RadioGroup name="radiogroup" defaultValue={1}>
<Radio value={1}>A</Radio>
<Radio value={2}>B</Radio>
<Radio value={3}>C</Radio>
<Radio value={4}>D</Radio>
</RadioGroup>
);
}
ReactDOM.render(<App />, mountNode);
```

View File

@ -24,6 +24,7 @@ export interface RadioGroupProps extends AbstractCheckboxGroupProps {
size?: 'large' | 'default' | 'small';
onMouseEnter?: React.FormEventHandler<any>;
onMouseLeave?: React.FormEventHandler<any>;
name?: string;
}
export default class RadioGroup extends React.Component<RadioGroupProps, any> {
@ -57,6 +58,7 @@ export default class RadioGroup extends React.Component<RadioGroupProps, any> {
onChange: this.onRadioChange,
value: this.state.value,
disabled: this.props.disabled,
name: this.props.name,
},
};
}

View File

@ -27,6 +27,7 @@ radio groupwrap a group of `Radio`。
| Property | Description | Type | optional | Default |
|----------------|----------------------------------|-------------------|--------|--------|
| name | The `name` property of all `input[type="radio"]` children | string | | none |
| onChange | The callback function that is triggered when the state changes. | Function(e:Event) | none | none |
| value | Used for setting the currently selected value. | any | none | none |
| defaultValue | Default selected value | any | none | none |

View File

@ -28,6 +28,7 @@ title: Radio
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|----------------|----------------------------------|-------------------|--------|--------|
| name | RadioGroup 下所有 `input[type="radio"]``name` 属性 | string | | 无 |
| onChange | 选项变化时的回调函数 | Function(e:Event) | 无 | 无 |
| value | 用于设置当前选中的值 | any | 无 | 无 |
| defaultValue | 默认选中的值 | any | 无 | 无 |

View File

@ -40,6 +40,7 @@ export default class Radio extends React.Component<RadioProps, any> {
const { radioGroup } = context;
let radioProps: RadioProps = { ...restProps };
if (radioGroup) {
radioProps.name = radioGroup.name;
radioProps.onChange = radioGroup.onChange;
radioProps.checked = props.value === radioGroup.value;
radioProps.disabled = props.disabled || radioGroup.disabled;