forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropdown.js
More file actions
354 lines (299 loc) · 8.22 KB
/
Copy pathDropdown.js
File metadata and controls
354 lines (299 loc) · 8.22 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import classNames from 'classnames';
import activeElement from 'dom-helpers/activeElement';
import contains from 'dom-helpers/query/contains';
import keycode from 'keycode';
import React, { cloneElement } from 'react';
import ReactDOM from 'react-dom';
import all from 'react-prop-types/lib/all';
import elementType from 'react-prop-types/lib/elementType';
import isRequiredForA11y from 'react-prop-types/lib/isRequiredForA11y';
import uncontrollable from 'uncontrollable';
import warning from 'warning';
import ButtonGroup from './ButtonGroup';
import DropdownMenu from './DropdownMenu';
import DropdownToggle from './DropdownToggle';
import { bsClass as setBsClass, prefix } from './utils/bootstrapUtils';
import createChainedFunction from './utils/createChainedFunction';
import { exclusiveRoles, requiredRoles } from './utils/PropTypes';
import ValidComponentChildren from './utils/ValidComponentChildren';
const TOGGLE_ROLE = DropdownToggle.defaultProps.bsRole;
const MENU_ROLE = DropdownMenu.defaultProps.bsRole;
const propTypes = {
/**
* The menu will open above the dropdown button, instead of below it.
*/
dropup: React.PropTypes.bool,
/**
* An html id attribute, necessary for assistive technologies, such as screen readers.
* @type {string|number}
* @required
*/
id: isRequiredForA11y(React.PropTypes.oneOfType([
React.PropTypes.string, React.PropTypes.number,
])),
componentClass: elementType,
/**
* The children of a Dropdown may be a `<Dropdown.Toggle>` or a `<Dropdown.Menu>`.
* @type {node}
*/
children: all(
requiredRoles(TOGGLE_ROLE, MENU_ROLE),
exclusiveRoles(MENU_ROLE)
),
/**
* Whether or not component is disabled.
*/
disabled: React.PropTypes.bool,
/**
* Align the menu to the right side of the Dropdown toggle
*/
pullRight: React.PropTypes.bool,
/**
* Whether or not the Dropdown is visible.
*
* @controllable onToggle
*/
open: React.PropTypes.bool,
/**
* A callback fired when the Dropdown closes.
*/
onClose: React.PropTypes.func,
/**
* A callback fired when the Dropdown wishes to change visibility. Called with the requested
* `open` value.
*
* ```js
* function(Boolean isOpen) {}
* ```
* @controllable open
*/
onToggle: React.PropTypes.func,
/**
* A callback fired when a menu item is selected.
*
* ```js
* (eventKey: any, event: Object) => any
* ```
*/
onSelect: React.PropTypes.func,
/**
* If `'menuitem'`, causes the dropdown to behave like a menu item rather than
* a menu button.
*/
role: React.PropTypes.string,
/**
* Which event when fired outside the component will cause it to be closed
*/
rootCloseEvent: React.PropTypes.oneOf(['click', 'mousedown']),
/**
* @private
*/
onMouseEnter: React.PropTypes.func,
/**
* @private
*/
onMouseLeave: React.PropTypes.func,
};
const defaultProps = {
componentClass: ButtonGroup,
};
class Dropdown extends React.Component {
constructor(props, context) {
super(props, context);
this.handleClick = this.handleClick.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleClose = this.handleClose.bind(this);
this._focusInDropdown = false;
this.lastOpenEventType = null;
}
componentDidMount() {
this.focusNextOnOpen();
}
componentWillUpdate(nextProps) {
if (!nextProps.open && this.props.open) {
this._focusInDropdown = contains(
ReactDOM.findDOMNode(this.menu), activeElement(document)
);
}
}
componentDidUpdate(prevProps) {
const { open } = this.props;
const prevOpen = prevProps.open;
if (open && !prevOpen) {
this.focusNextOnOpen();
}
if (!open && prevOpen) {
// if focus hasn't already moved from the menu lets return it
// to the toggle
if (this._focusInDropdown) {
this._focusInDropdown = false;
this.focus();
}
}
}
handleClick() {
if (this.props.disabled) {
return;
}
this.toggleOpen('click');
}
handleKeyDown(event) {
if (this.props.disabled) {
return;
}
switch (event.keyCode) {
case keycode.codes.down:
if (!this.props.open) {
this.toggleOpen('keydown');
} else if (this.menu.focusNext) {
this.menu.focusNext();
}
event.preventDefault();
break;
case keycode.codes.esc:
case keycode.codes.tab:
this.handleClose(event);
break;
default:
}
}
toggleOpen(eventType) {
let open = !this.props.open;
if (open) {
this.lastOpenEventType = eventType;
}
if (this.props.onToggle) {
this.props.onToggle(open);
}
}
handleClose() {
if (!this.props.open) {
return;
}
this.toggleOpen(null);
}
focusNextOnOpen() {
const menu = this.menu;
if (!menu.focusNext) {
return;
}
if (
this.lastOpenEventType === 'keydown' ||
this.props.role === 'menuitem'
) {
menu.focusNext();
}
}
focus() {
const toggle = ReactDOM.findDOMNode(this.toggle);
if (toggle && toggle.focus) {
toggle.focus();
}
}
renderToggle(child, props) {
let ref = c => { this.toggle = c; };
if (typeof child.ref === 'string') {
warning(false,
'String refs are not supported on `<Dropdown.Toggle>` components. ' +
'To apply a ref to the component use the callback signature:\n\n ' +
'https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute'
);
} else {
ref = createChainedFunction(child.ref, ref);
}
return cloneElement(child, {
...props,
ref,
bsClass: prefix(props, 'toggle'),
onClick: createChainedFunction(
child.props.onClick, this.handleClick
),
onKeyDown: createChainedFunction(
child.props.onKeyDown, this.handleKeyDown
),
});
}
renderMenu(child, { id, onClose, onSelect, rootCloseEvent, ...props }) {
let ref = c => { this.menu = c; };
if (typeof child.ref === 'string') {
warning(false,
'String refs are not supported on `<Dropdown.Menu>` components. ' +
'To apply a ref to the component use the callback signature:\n\n ' +
'https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute'
);
} else {
ref = createChainedFunction(child.ref, ref);
}
return cloneElement(child, {
...props,
ref,
labelledBy: id,
bsClass: prefix(props, 'menu'),
onClose: createChainedFunction(
child.props.onClose, onClose, this.handleClose,
),
onSelect: createChainedFunction(
child.props.onSelect, onSelect, this.handleClose,
),
rootCloseEvent
});
}
render() {
const {
componentClass: Component,
id,
dropup,
disabled,
pullRight,
open,
onClose,
onSelect,
role,
bsClass,
className,
rootCloseEvent,
children,
...props
} = this.props;
delete props.onToggle;
const classes = {
[bsClass]: true,
open,
disabled,
};
if (dropup) {
classes[bsClass] = false;
classes.dropup = true;
}
// This intentionally forwards bsSize and bsStyle (if set) to the
// underlying component, to allow it to render size and style variants.
return (
<Component
{...props}
className={classNames(className, classes)}
>
{ValidComponentChildren.map(children, child => {
switch (child.props.bsRole) {
case TOGGLE_ROLE:
return this.renderToggle(child, {
id, disabled, open, role, bsClass,
});
case MENU_ROLE:
return this.renderMenu(child, {
id, open, pullRight, bsClass, onClose, onSelect, rootCloseEvent,
});
default:
return child;
}
})}
</Component>
);
}
}
Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;
setBsClass('dropdown', Dropdown);
const UncontrolledDropdown = uncontrollable(Dropdown, { open: 'onToggle' });
UncontrolledDropdown.Toggle = DropdownToggle;
UncontrolledDropdown.Menu = DropdownMenu;
export default UncontrolledDropdown;