-
Notifications
You must be signed in to change notification settings - Fork 93
/
Async.js
218 lines (181 loc) · 5.21 KB
/
Async.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Select from './Select';
import stripDiacritics from './utils/stripDiacritics';
const propTypes = {
autoload: PropTypes.bool.isRequired, // automatically call the `loadOptions` prop on-mount; defaults to true
cache: PropTypes.any, // object to use to cache results; set to null/false to disable caching
children: PropTypes.func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element
ignoreAccents: PropTypes.bool, // strip diacritics when filtering; defaults to true
ignoreCase: PropTypes.bool, // perform case-insensitive filtering; defaults to true
loadingPlaceholder: PropTypes.oneOfType([ // replaces the placeholder while options are loading
PropTypes.string,
PropTypes.node
]),
loadOptions: PropTypes.func.isRequired, // callback to load options asynchronously; (inputValue: string, callback: Function): ?Promise
multi: PropTypes.bool, // multi-value input
options: PropTypes.array.isRequired, // array of options
placeholder: PropTypes.oneOfType([ // field placeholder, displayed when there's no value (shared with Select)
PropTypes.string,
PropTypes.node
]),
noResultsText: PropTypes.oneOfType([ // field noResultsText, displayed when no options come back from the server
PropTypes.string,
PropTypes.node
]),
onChange: PropTypes.func, // onChange handler: function (newValue) {}
searchPromptText: PropTypes.oneOfType([ // label to prompt for search input
PropTypes.string,
PropTypes.node
]),
onInputChange: PropTypes.func, // optional for keeping track of what is being typed
value: PropTypes.any, // initial field value
};
const defaultCache = {};
const defaultProps = {
autoload: true,
cache: defaultCache,
children: defaultChildren,
ignoreAccents: true,
ignoreCase: true,
loadingPlaceholder: 'Loading...',
options: [],
searchPromptText: 'Type to search',
};
export default class Async extends Component {
constructor (props, context) {
super(props, context);
this._cache = props.cache === defaultCache ? {} : props.cache;
this.state = {
isLoading: false,
options: props.options,
};
this._onInputChange = this._onInputChange.bind(this);
}
componentDidMount () {
const { autoload } = this.props;
if (autoload) {
this.loadOptions('');
}
}
componentWillUpdate (nextProps, nextState) {
const propertiesToSync = ['options'];
propertiesToSync.forEach((prop) => {
if (this.props[prop] !== nextProps[prop]) {
this.setState({
[prop]: nextProps[prop]
});
}
});
}
clearOptions() {
this.setState({ options: [] });
}
loadOptions (inputValue) {
const { loadOptions } = this.props;
const cache = this._cache;
if (
cache &&
cache.hasOwnProperty(inputValue)
) {
this.setState({
options: cache[inputValue]
});
return;
}
const callback = (error, data) => {
if (callback === this._callback) {
this._callback = null;
const options = data && data.options || [];
if (cache) {
cache[inputValue] = options;
}
this.setState({
isLoading: false,
options
});
}
};
// Ignore all but the most recent request
this._callback = callback;
const promise = loadOptions(inputValue, callback);
if (promise) {
promise.then(
(data) => callback(null, data),
(error) => callback(error)
);
}
if (
this._callback &&
!this.state.isLoading
) {
this.setState({
isLoading: true
});
}
return inputValue;
}
_onInputChange (inputValue) {
const { ignoreAccents, ignoreCase, onInputChange } = this.props;
if (ignoreAccents) {
inputValue = stripDiacritics(inputValue);
}
if (ignoreCase) {
inputValue = inputValue.toLowerCase();
}
if (onInputChange) {
onInputChange(inputValue);
}
return this.loadOptions(inputValue);
}
inputValue() {
if (this.select) {
return this.select.state.inputValue;
}
return '';
}
noResultsText() {
const { loadingPlaceholder, noResultsText, searchPromptText } = this.props;
const { isLoading } = this.state;
const inputValue = this.inputValue();
if (isLoading) {
return loadingPlaceholder;
}
if (inputValue && noResultsText) {
return noResultsText;
}
return searchPromptText;
}
focus () {
this.select.focus();
}
render () {
const { children, loadingPlaceholder, placeholder } = this.props;
const { isLoading, options } = this.state;
const props = {
noResultsText: this.noResultsText(),
placeholder: isLoading ? loadingPlaceholder : placeholder,
options: (isLoading && loadingPlaceholder) ? [] : options,
ref: (ref) => (this.select = ref),
onChange: (newValues) => {
if (this.props.multi && this.props.value && (newValues.length > this.props.value.length)) {
this.clearOptions();
}
this.props.onChange(newValues);
}
};
return children({
...this.props,
...props,
isLoading,
onInputChange: this._onInputChange
});
}
}
Async.propTypes = propTypes;
Async.defaultProps = defaultProps;
function defaultChildren (props) {
return (
<Select {...props} />
);
}