-
Notifications
You must be signed in to change notification settings - Fork 407
/
Typeahead.react.js
166 lines (149 loc) · 3.95 KB
/
Typeahead.react.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
import cx from 'classnames';
import {pick} from 'lodash';
import PropTypes from 'prop-types';
import React from 'react';
import {findDOMNode} from 'react-dom';
import ClearButton from './ClearButton.react';
import Loader from './Loader.react';
import Overlay from './Overlay.react';
import TypeaheadInputMulti from './TypeaheadInputMulti.react';
import TypeaheadInputSingle from './TypeaheadInputSingle.react';
import TypeaheadMenu from './TypeaheadMenu.react';
import typeaheadContainer from './containers/typeaheadContainer';
import {getAccessibilityStatus, preventInputBlur} from './utils';
class Typeahead extends React.Component {
render() {
const {
bodyContainer,
children,
className,
isMenuShown,
menuId,
renderMenu,
results,
} = this.props;
const inputProps = pick(this.props, [
'activeIndex',
'activeItem',
'bsSize',
'disabled',
'inputProps',
'inputRef',
'isFocused',
'isInvalid',
'isMenuShown',
'isValid',
'labelKey',
'menuId',
'multiple',
'onBlur',
'onChange',
'onFocus',
'onKeyDown',
'onRemove',
'placeholder',
'renderToken',
'selected',
'text',
]);
const overlayProps = pick(this.props, [
'align',
'className',
'dropup',
'flip',
'onMenuHide',
'onMenuShow',
'onMenuToggle',
]);
const menuProps = pick(this.props, [
'emptyLabel',
'labelKey',
'maxHeight',
'newSelectionPrefix',
'renderMenuItemChildren',
'text',
]);
const auxContent = this._renderAux();
return (
<div
className={cx('rbt', 'clearfix', 'open', {
'has-aux': !!auxContent,
}, className)}
style={{position: 'relative'}}
tabIndex={-1}>
{this._renderInput({
...inputProps,
// Use `findDOMNode` here since it's easier and less fragile than
// forwarding refs down to the input's container.
// TODO: Consider using `forwardRef` when React 16.3 usage is higher.
/* eslint-disable-next-line react/no-find-dom-node */
ref: (node) => this._inputContainer = findDOMNode(node),
})}
{typeof children === 'function' ? children(this.props) : children}
{auxContent}
<Overlay
{...overlayProps}
container={bodyContainer ? document.body : this}
referenceElement={this._inputContainer}
show={isMenuShown}>
{renderMenu(results, {...menuProps, id: menuId})}
</Overlay>
<div
aria-atomic
aria-live="polite"
className="sr-only rbt-sr-status"
role="status">
{getAccessibilityStatus(this.props)}
</div>
</div>
);
}
_renderInput = (inputProps) => {
const Input = inputProps.multiple ?
TypeaheadInputMulti :
TypeaheadInputSingle;
return <Input {...inputProps} />;
}
_renderAux = () => {
const {
bsSize,
clearButton,
disabled,
isLoading,
onClear,
selected,
} = this.props;
let content;
if (isLoading) {
content = <Loader bsSize={bsSize} />;
} else if (clearButton && !disabled && selected.length) {
content =
<ClearButton
bsSize={bsSize}
onClick={onClear}
onFocus={(e) => {
// Prevent the main input from auto-focusing again.
e.stopPropagation();
}}
onMouseDown={preventInputBlur}
/>;
}
return content ?
<div
className={cx('rbt-aux', {
'rbt-aux-lg': bsSize === 'large' || bsSize === 'lg',
})}>
{content}
</div> :
null;
}
}
Typeahead.propTypes = {
renderMenu: PropTypes.func,
};
Typeahead.defaultProps = {
renderMenu: (results, menuProps) => (
<TypeaheadMenu {...menuProps} options={results} />
),
};
export default typeaheadContainer(Typeahead);