/
Navbar.js
264 lines (231 loc) · 6.83 KB
/
Navbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// TODO: Remove this pragma once we upgrade eslint-config-airbnb.
/* eslint-disable react/no-multi-comp */
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import elementType from 'prop-types-extra/lib/elementType';
import { uncontrollable } from 'uncontrollable';
import Grid from './Grid';
import NavbarBrand from './NavbarBrand';
import NavbarCollapse from './NavbarCollapse';
import NavbarHeader from './NavbarHeader';
import NavbarToggle from './NavbarToggle';
import {
bsClass as setBsClass,
bsStyles,
getClassSet,
prefix,
splitBsPropsAndOmit
} from './utils/bootstrapUtils';
import { Style } from './utils/StyleConfig';
import createChainedFunction from './utils/createChainedFunction';
const propTypes = {
/**
* Create a fixed navbar along the top of the screen, that scrolls with the
* page
*/
fixedTop: PropTypes.bool,
/**
* Create a fixed navbar along the bottom of the screen, that scrolls with
* the page
*/
fixedBottom: PropTypes.bool,
/**
* Create a full-width navbar that scrolls away with the page
*/
staticTop: PropTypes.bool,
/**
* An alternative dark visual style for the Navbar
*/
inverse: PropTypes.bool,
/**
* Allow the Navbar to fluidly adjust to the page or container width, instead
* of at the predefined screen breakpoints
*/
fluid: PropTypes.bool,
/**
* Set a custom element for this component.
*/
componentClass: elementType,
/**
* A callback fired when the `<Navbar>` body collapses or expands. Fired when
* a `<Navbar.Toggle>` is clicked and called with the new `expanded`
* boolean value.
*
* @controllable expanded
*/
onToggle: PropTypes.func,
/**
* A callback fired when a descendant of a child `<Nav>` is selected. Should
* be used to execute complex closing or other miscellaneous actions desired
* after selecting a descendant of `<Nav>`. Does nothing if no `<Nav>` or `<Nav>`
* descendants exist. The callback is called with an eventKey, which is a
* prop from the selected `<Nav>` descendant, and an event.
*
* ```js
* function (
* Any eventKey,
* SyntheticEvent event?
* )
* ```
*
* For basic closing behavior after all `<Nav>` descendant onSelect events in
* mobile viewports, try using collapseOnSelect.
*
* Note: If you are manually closing the navbar using this `OnSelect` prop,
* ensure that you are setting `expanded` to false and not *toggling* between
* true and false.
*/
onSelect: PropTypes.func,
/**
* Sets `expanded` to `false` after the onSelect event of a descendant of a
* child `<Nav>`. Does nothing if no `<Nav>` or `<Nav>` descendants exist.
*
* The onSelect callback should be used instead for more complex operations
* that need to be executed after the `select` event of `<Nav>` descendants.
*/
collapseOnSelect: PropTypes.bool,
/**
* Explicitly set the visiblity of the navbar body
*
* @controllable onToggle
*/
expanded: PropTypes.bool,
role: PropTypes.string
};
const defaultProps = {
componentClass: 'nav',
fixedTop: false,
fixedBottom: false,
staticTop: false,
inverse: false,
fluid: false,
collapseOnSelect: false
};
const childContextTypes = {
$bs_navbar: PropTypes.shape({
bsClass: PropTypes.string,
expanded: PropTypes.bool,
onToggle: PropTypes.func.isRequired,
onSelect: PropTypes.func
})
};
class Navbar extends React.Component {
constructor(props, context) {
super(props, context);
this.handleToggle = this.handleToggle.bind(this);
this.handleCollapse = this.handleCollapse.bind(this);
}
getChildContext() {
const { bsClass, expanded, onSelect, collapseOnSelect } = this.props;
return {
$bs_navbar: {
bsClass,
expanded,
onToggle: this.handleToggle,
onSelect: createChainedFunction(
onSelect,
collapseOnSelect ? this.handleCollapse : null
)
}
};
}
handleCollapse() {
const { onToggle, expanded } = this.props;
if (expanded) {
onToggle(false);
}
}
handleToggle() {
const { onToggle, expanded } = this.props;
onToggle(!expanded);
}
render() {
const {
componentClass: Component,
fixedTop,
fixedBottom,
staticTop,
inverse,
fluid,
className,
children,
...props
} = this.props;
const [bsProps, elementProps] = splitBsPropsAndOmit(props, [
'expanded',
'onToggle',
'onSelect',
'collapseOnSelect'
]);
// will result in some false positives but that seems better
// than false negatives. strict `undefined` check allows explicit
// "nulling" of the role if the user really doesn't want one
if (elementProps.role === undefined && Component !== 'nav') {
elementProps.role = 'navigation';
}
if (inverse) {
bsProps.bsStyle = Style.INVERSE;
}
const classes = {
...getClassSet(bsProps),
[prefix(bsProps, 'fixed-top')]: fixedTop,
[prefix(bsProps, 'fixed-bottom')]: fixedBottom,
[prefix(bsProps, 'static-top')]: staticTop
};
return (
<Component {...elementProps} className={classNames(className, classes)}>
<Grid fluid={fluid}>{children}</Grid>
</Component>
);
}
}
Navbar.propTypes = propTypes;
Navbar.defaultProps = defaultProps;
Navbar.childContextTypes = childContextTypes;
setBsClass('navbar', Navbar);
const UncontrollableNavbar = uncontrollable(Navbar, { expanded: 'onToggle' });
function createSimpleWrapper(tag, suffix, displayName) {
const Wrapper = (
{ componentClass: Component, className, pullRight, pullLeft, ...props },
{ $bs_navbar: navbarProps = { bsClass: 'navbar' } }
) => (
<Component
{...props}
className={classNames(
className,
prefix(navbarProps, suffix),
pullRight && prefix(navbarProps, 'right'),
pullLeft && prefix(navbarProps, 'left')
)}
/>
);
Wrapper.displayName = displayName;
Wrapper.propTypes = {
componentClass: elementType,
pullRight: PropTypes.bool,
pullLeft: PropTypes.bool
};
Wrapper.defaultProps = {
componentClass: tag,
pullRight: false,
pullLeft: false
};
Wrapper.contextTypes = {
$bs_navbar: PropTypes.shape({
bsClass: PropTypes.string
})
};
return Wrapper;
}
UncontrollableNavbar.Brand = NavbarBrand;
UncontrollableNavbar.Header = NavbarHeader;
UncontrollableNavbar.Toggle = NavbarToggle;
UncontrollableNavbar.Collapse = NavbarCollapse;
UncontrollableNavbar.Form = createSimpleWrapper('div', 'form', 'NavbarForm');
UncontrollableNavbar.Text = createSimpleWrapper('p', 'text', 'NavbarText');
UncontrollableNavbar.Link = createSimpleWrapper('a', 'link', 'NavbarLink');
// Set bsStyles here so they can be overridden.
export default bsStyles([Style.DEFAULT, Style.INVERSE], Style.DEFAULT)(
UncontrollableNavbar
);