Skip to content

Commit 86c4e10

Browse files
guanputao1991123
authored andcommitted
fix(Tab): a11y support closeable tab
1 parent a00b206 commit 86c4e10

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/tab/tabs/nav.jsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Icon from '../../icon';
66
import Overlay from '../../overlay';
77
import Menu from '../../menu';
88
import Animate from '../../animate';
9-
import { events } from '../../util';
9+
import { events, KEYCODE } from '../../util';
1010
import { triggerEvents, getOffsetLT, getOffsetWH, isTransformSupported } from './utils';
1111

1212
const noop = () => { };
@@ -203,8 +203,16 @@ class Nav extends React.Component {
203203
this.props.onClose(key);
204204
}
205205

206+
onCloseKeyDown = (key, e) => {
207+
if (e.keyCode === KEYCODE.ENTER) {
208+
e.stopPropagation();
209+
e.preventDefault();
210+
this.props.onClose(key);
211+
}
212+
}
213+
206214
defaultTabTemplateRender = (key, { prefix, title, closeable }) => {
207-
const tail = closeable ? <Icon type="close" onClick={(e) => this.removeTab(key, e)} className={`${prefix}tabs-tab-close`} /> : null;
215+
const tail = closeable ? <Icon type="close" tabIndex="0" onKeyDown={(e) => this.onCloseKeyDown(key, e)} onClick={(e) => this.removeTab(key, e)} className={`${prefix}tabs-tab-close`} /> : null;
208216
return <div className={`${this.props.prefix}tabs-tab-inner`}>{title}{tail}</div>;
209217
}
210218

test/tab/index-spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ describe('Tab', () => {
169169
assert(tabKey === '1');
170170
});
171171

172+
it('should close tab with keyboard', ()=>{
173+
let tabKey;
174+
wrapper = mount(<Tab onClose={key => tabKey = key}>
175+
<Tab.Item title="foo" />
176+
<Tab.Item title="bar" closeable />
177+
</Tab>);
178+
const closeIcon = wrapper.find('.next-icon-close');
179+
assert(closeIcon.length === 1);
180+
closeIcon.simulate('focus');
181+
closeIcon.simulate('keyDown', { keyCode: KEYCODE.ENTER });
182+
assert(tabKey === '1');
183+
});
184+
172185
it('should unmountInactiveTabs', () => {
173186
wrapper = mount(<Tab unmountInactiveTabs>{panes}</Tab>);
174187
wrapper.find('.next-tabs-tab').at(3).simulate('click');
@@ -221,7 +234,7 @@ describe('Tab', () => {
221234
it('should scrollToActiveTab', () => {
222235
wrapper = mount(<div style={boxStyle}><Tab defaultActiveKey="9">{panes}</Tab></div>, { attachTo: target });
223236
wrapper.find('.next-tabs-tab').at(3).simulate('click');
224-
assert(wrapper.find('.next-tabs-tab').at(3).hasClass('active'))
237+
assert(wrapper.find('.next-tabs-tab').at(3).hasClass('active'));
225238
});
226239
});
227240
});

0 commit comments

Comments
 (0)