Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
width: 44px;
height: 22px;
line-height: 20px;
padding: 0;
vertical-align: middle;
border-radius: 20px 20px;
border: 1px solid #ccc;
Expand All @@ -20,7 +21,8 @@
color:#fff;
font-size: 12px;
position: absolute;
left:24px;
left: 24px;
top: 0;
}

&:after{
Expand Down
14 changes: 7 additions & 7 deletions src/Switch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class Switch extends Component {
this.setChecked(false);
} else if (e.keyCode === 39) { // Right
this.setChecked(true);
} else if (e.keyCode === 32 || e.keyCode === 13) { // Space, Enter
this.toggle();
}
}

Expand Down Expand Up @@ -86,20 +84,22 @@ class Switch extends Component {

render() {
const { className, prefixCls, disabled, loadingIcon,
checkedChildren, tabIndex, unCheckedChildren, ...restProps } = this.props;
checkedChildren, unCheckedChildren, ...restProps } = this.props;
const checked = this.state.checked;
const switchTabIndex = disabled ? -1 : (tabIndex || 0);
const switchClassName = classNames({
[className]: !!className,
[prefixCls]: true,
[`${prefixCls}-checked`]: checked,
[`${prefixCls}-disabled`]: disabled,
});
return (
<span
<button
{...restProps}
type="button"
role="switch"
aria-checked={checked}
disabled={disabled}
className={switchClassName}
tabIndex={switchTabIndex}
ref={this.saveNode}
onKeyDown={this.handleKeyDown}
onClick={this.toggle}
Expand All @@ -109,7 +109,7 @@ class Switch extends Component {
<span className={`${prefixCls}-inner`}>
{checked ? checkedChildren : unCheckedChildren}
</span>
</span>
</button>
);
}
}
Expand Down
8 changes: 0 additions & 8 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ describe('rc-switch', () => {
expect(switcher.state().checked).toBe(false);
});

it('should toggle upon space and enter key', () => {
expect(switcher.state().checked).toBe(false);
switcher.simulate('keydown', { keyCode: 32 });
expect(switcher.state().checked).toBe(true);
switcher.simulate('keydown', { keyCode: 13 });
expect(switcher.state().checked).toBe(false);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enzyme doesn't fire onClick on such keypresses, but the browsers will call onClick because it's button
Plus, if we'll try to test such behavior we will be actually testing a11y specification, which i think we shouldn't do


it('should change from an initial checked state of true to false on click', () => {
const wrapper = mount(<Switch defaultChecked/>);
expect(wrapper.state().checked).toBe(true);
Expand Down