Skip to content

Commit aefbdd7

Browse files
committed
fix(Navbar): better backwards compatibility
1 parent a2a33e3 commit aefbdd7

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

src/Navbar.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,48 @@ const propTypes = {
1515
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
1616
className: PropTypes.string,
1717
cssModule: PropTypes.object,
18-
toggleable: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
19-
expandable: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
18+
toggleable: deprecated(PropTypes.oneOfType([PropTypes.bool, PropTypes.string]), 'Please use the prop "expand"'),
19+
expand: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
2020
};
2121

2222
const defaultProps = {
2323
tag: 'nav',
24-
toggleable: false,
25-
expandable: false,
24+
expand: false,
25+
};
26+
27+
const getExpandClass = (expand) => {
28+
if (expand === false) {
29+
return false;
30+
} else if (expand === true || expand === 'xs') {
31+
return 'navbar-expand';
32+
}
33+
34+
return `navbar-expand-${expand}`;
35+
};
36+
37+
// To better maintain backwards compatibility while toggleable is deprecated.
38+
// We must map breakpoints to the next breakpoint so that toggleable and expand do the same things at the same breakpoint.
39+
const toggleableToExpand = {
40+
xs: 'sm',
41+
sm: 'md',
42+
md: 'lg',
43+
lg: 'xl',
2644
};
2745

2846
const getToggleableClass = (toggleable) => {
29-
if (toggleable === false) {
47+
if (toggleable === undefined || toggleable === 'xl') {
3048
return false;
31-
} else if (toggleable === true || toggleable === 'xs') {
49+
} else if (toggleable === false) {
3250
return 'navbar-expand';
3351
}
3452

35-
return `navbar-expand-${toggleable}`;
53+
return `navbar-expand-${toggleable === true ? 'sm' : (toggleableToExpand[toggleable] || toggleable)}`;
3654
};
3755

3856
const Navbar = (props) => {
3957
const {
4058
toggleable,
41-
expandable,
59+
expand,
4260
className,
4361
cssModule,
4462
light,
@@ -54,7 +72,7 @@ const Navbar = (props) => {
5472
const classes = mapToCssModules(classNames(
5573
className,
5674
'navbar',
57-
getToggleableClass(toggleable || expandable),
75+
getExpandClass(expand) || getToggleableClass(toggleable),
5876
{
5977
'navbar-light': light,
6078
'navbar-dark': inverse || dark,

src/__tests__/Navbar.spec.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,36 @@ describe('Navbar', () => {
99
expect(wrapper.html()).toBe('<nav class="navbar"></nav>');
1010
});
1111

12-
it('should render default .navbar-toggleable class', () => {
13-
const wrapper = shallow(<Navbar toggleable />);
12+
it('should render default .navbar-expand class', () => {
13+
const wrapper = shallow(<Navbar expand />);
1414

1515
expect(wrapper.html()).toBe('<nav class="navbar navbar-expand"></nav>');
1616
});
1717

18-
it('should render size based .navbar-toggleable-* classes', () => {
19-
const wrapper = shallow(<Navbar toggleable="md" />);
18+
it('should render size based .navbar-expand-* classes', () => {
19+
const wrapper = shallow(<Navbar expand="md" />);
2020

2121
expect(wrapper.html()).toBe('<nav class="navbar navbar-expand-md"></nav>');
2222
});
2323

24+
it('should render default .navbar-expand class for toggleable false [DEPRECATED]', () => {
25+
const wrapper = shallow(<Navbar toggleable={false} />);
26+
27+
expect(wrapper.html()).toBe('<nav class="navbar navbar-expand"></nav>');
28+
});
29+
30+
it('should render default .navbar-expand class for toggleable true [DEPRECATED]', () => {
31+
const wrapper = shallow(<Navbar toggleable />);
32+
33+
expect(wrapper.html()).toBe('<nav class="navbar navbar-expand-sm"></nav>');
34+
});
35+
36+
it('should render size based .navbar-expand-* classes for toggleable (bumping breakpoint) [DEPRECATED]', () => {
37+
const wrapper = shallow(<Navbar toggleable="md" />);
38+
39+
expect(wrapper.html()).toBe('<nav class="navbar navbar-expand-lg"></nav>');
40+
});
41+
2442
it('should render custom tag', () => {
2543
const wrapper = shallow(<Navbar tag="div" />);
2644

@@ -47,7 +65,7 @@ describe('Navbar', () => {
4765
});
4866

4967
it('should render prop based classes', () => {
50-
const wrapper = shallow(<Navbar light dark toggleable="sm" color="success" full sticky="top" fixed="top" />);
68+
const wrapper = shallow(<Navbar light dark expand="sm" color="success" full sticky="top" fixed="top" />);
5169

5270
expect(wrapper.hasClass('bg-success')).toBe(true);
5371
expect(wrapper.hasClass('navbar')).toBe(true);

0 commit comments

Comments
 (0)