Skip to content

Commit 2a7a0e2

Browse files
committed
feat(Nav)!: migrate to restart/ui
BREAKING CHANGE: removed keyboard navigation
1 parent 7030465 commit 2a7a0e2

File tree

4 files changed

+8
-176
lines changed

4 files changed

+8
-176
lines changed

src/Nav.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,22 @@ import all from 'prop-types-extra/lib/all';
55
import * as React from 'react';
66
import { useContext } from 'react';
77
import { useUncontrolled } from 'uncontrollable';
8-
8+
import BaseNav, { NavProps as BaseNavProps } from '@restart/ui/Nav';
9+
import { EventKey } from '@restart/ui/types';
910
import { useBootstrapPrefix } from './ThemeProvider';
1011
import NavbarContext from './NavbarContext';
1112
import CardHeaderContext from './CardHeaderContext';
12-
import AbstractNav from './AbstractNav';
1313
import NavItem from './NavItem';
1414
import NavLink from './NavLink';
15-
import {
16-
BsPrefixProps,
17-
BsPrefixRefForwardingComponent,
18-
SelectCallback,
19-
} from './helpers';
20-
import { EventKey } from './types';
21-
22-
export interface NavProps
23-
extends BsPrefixProps,
24-
Omit<React.HTMLAttributes<HTMLElement>, 'onSelect'> {
15+
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
16+
17+
export interface NavProps extends BsPrefixProps, BaseNavProps {
2518
navbarBsPrefix?: string;
2619
cardHeaderBsPrefix?: string;
2720
variant?: 'tabs' | 'pills';
28-
activeKey?: EventKey;
2921
defaultActiveKey?: EventKey;
3022
fill?: boolean;
3123
justify?: boolean;
32-
onSelect?: SelectCallback;
3324
navbar?: boolean;
3425
navbarScroll?: boolean;
3526
}
@@ -149,7 +140,7 @@ const Nav: BsPrefixRefForwardingComponent<'div', NavProps> = React.forwardRef<
149140
}
150141

151142
return (
152-
<AbstractNav
143+
<BaseNav
153144
as={as}
154145
ref={ref}
155146
activeKey={activeKey}

src/NavContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { EventKey } from './types';
2+
import { EventKey } from '@restart/ui/types';
33

44
interface NavContextType {
55
role?: string; // used by NavLink to determine it's role

test/NavLinkSpec.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,6 @@ describe('<NavLink>', () => {
2929
);
3030
});
3131

32-
it('Should call `onSelect` when item is selected', (done) => {
33-
function handleSelect(key, event) {
34-
assert.equal(key, '2');
35-
assert.ok(event.target.tagName === 'A');
36-
done();
37-
}
38-
mount(
39-
<NavLink eventKey="2" onSelect={handleSelect}>
40-
<span>Item content</span>
41-
</NavLink>,
42-
).simulate('click');
43-
});
44-
45-
it('Should not call `onSelect` when item disabled and is selected', () => {
46-
function handleSelect() {
47-
throw new Error('onSelect should not be called');
48-
}
49-
50-
mount(
51-
<NavLink disabled onSelect={handleSelect}>
52-
<span>Item content</span>
53-
</NavLink>,
54-
).simulate('click');
55-
});
56-
5732
describe('Web Accessibility', () => {
5833
it('Should add aria-selected to the link when role is "tab"', () => {
5934
mount(

test/NavSpec.js

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('<Nav>', () => {
128128
</NavDropdown>
129129
</Nav>,
130130
)
131-
.find('DropdownItem')
131+
.find('a.dropdown-item')
132132
.simulate('click');
133133

134134
onSelectSpy.should.have.been.calledOnce;
@@ -151,140 +151,6 @@ describe('<Nav>', () => {
151151
mount(<Nav navbar justify />);
152152
});
153153

154-
describe('keyboard navigation', () => {
155-
let wrapper;
156-
let selectSpy;
157-
let keyDownSpy;
158-
159-
beforeEach(() => {
160-
selectSpy = sinon.spy((activeKey) => {
161-
wrapper.setProps({ activeKey });
162-
});
163-
keyDownSpy = sinon.spy();
164-
165-
wrapper = mount(
166-
<Nav
167-
activeKey={1}
168-
onSelect={selectSpy}
169-
onKeyDown={keyDownSpy}
170-
role="tablist"
171-
>
172-
<Nav.Link eventKey={1}>Nav.Link 1 content</Nav.Link>
173-
<Nav.Link eventKey={2} disabled>
174-
Nav.Link 2 content
175-
</Nav.Link>
176-
<Nav.Link eventKey={3}>Nav.Link 3 content</Nav.Link>
177-
<Nav.Link eventKey={4} disabled>
178-
Nav.Link 4 content
179-
</Nav.Link>
180-
<Nav.Link eventKey={5}>Nav.Link 5 content</Nav.Link>
181-
</Nav>,
182-
{ attachTo: mountPoint },
183-
);
184-
});
185-
186-
afterEach(() => wrapper.unmount());
187-
188-
it('should not allow focusing on disabled tabs', () => {
189-
const links = wrapper.find('a').map((n) => n.getDOMNode());
190-
191-
expect(links[0].getAttribute('tabindex')).to.not.equal('-1');
192-
expect(links[1].getAttribute('tabindex')).to.equal('-1');
193-
expect(links[2].getAttribute('tabindex')).to.not.equal('-1');
194-
expect(links[3].getAttribute('tabindex')).to.equal('-1');
195-
expect(links[4].getAttribute('tabindex')).to.not.equal('-1');
196-
});
197-
198-
it('should focus the next tab on arrow key', () => {
199-
const anchors = wrapper.find('a');
200-
201-
anchors
202-
.at(0)
203-
.tap((a) => a.getDOMNode().focus())
204-
.simulate('keydown', {
205-
key: 'ArrowRight',
206-
});
207-
208-
expect(wrapper.prop('activeKey')).to.equal('3');
209-
210-
expect(document.activeElement).to.equal(anchors.at(2).getDOMNode());
211-
});
212-
213-
it('should focus the previous tab on arrow key', () => {
214-
wrapper.setProps({ activeKey: 5 });
215-
216-
const anchors = wrapper.find('a');
217-
218-
anchors
219-
.at(4)
220-
.tap((a) => a.getDOMNode().focus())
221-
.simulate('keydown', {
222-
key: 'ArrowLeft',
223-
});
224-
225-
expect(wrapper.prop('activeKey')).to.equal('3');
226-
expect(document.activeElement).to.equal(anchors.at(2).getDOMNode());
227-
});
228-
229-
it('should wrap to the next tab on arrow key', () => {
230-
wrapper.setProps({ activeKey: 5 });
231-
const anchors = wrapper.find('a');
232-
233-
anchors
234-
.at(4)
235-
.tap((a) => a.getDOMNode().focus())
236-
.simulate('keydown', {
237-
key: 'ArrowDown',
238-
});
239-
240-
expect(wrapper.prop('activeKey')).to.equal('1');
241-
expect(document.activeElement).to.equal(anchors.at(0).getDOMNode());
242-
});
243-
244-
it('should wrap to the previous tab on arrow key', () => {
245-
const anchors = wrapper.find('a');
246-
247-
anchors
248-
.at(0)
249-
.tap((a) => a.getDOMNode().focus())
250-
.simulate('keydown', {
251-
key: 'ArrowUp',
252-
});
253-
254-
expect(wrapper.prop('activeKey')).to.equal('5');
255-
expect(document.activeElement).to.equal(anchors.at(4).getDOMNode());
256-
});
257-
258-
it('should forward to a onKeyDown listener', () => {
259-
const anchors = wrapper.find('a');
260-
261-
anchors
262-
.at(0)
263-
.tap((a) => a.getDOMNode().focus())
264-
.simulate('keydown', {
265-
key: 'ArrowUp',
266-
});
267-
268-
sinon.assert.calledOnce(keyDownSpy);
269-
});
270-
271-
['a', 'b', 'left', 'up'].forEach(({ key }) => {
272-
it(`should ignore non-arrow keys: case ${key}`, () => {
273-
const anchors = wrapper.find('a');
274-
275-
anchors
276-
.at(0)
277-
.tap((a) => a.getDOMNode().focus())
278-
.simulate('keydown', {
279-
key,
280-
});
281-
282-
expect(document.activeElement).to.equal(anchors.at(0).getDOMNode());
283-
sinon.assert.calledOnce(keyDownSpy);
284-
});
285-
});
286-
});
287-
288154
describe('Web Accessibility', () => {
289155
it('should have tablist and tab roles', () => {
290156
const wrapper = mount(

0 commit comments

Comments
 (0)