forked from JedWatson/react-select
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsync.js
More file actions
154 lines (145 loc) · 4.47 KB
/
Async.js
File metadata and controls
154 lines (145 loc) · 4.47 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
import React from 'react';
import Select from './Select';
import stripDiacritics from './utils/stripDiacritics';
let requestId = 0;
function initCache (cache) {
if (cache && typeof cache !== 'object') {
cache = {};
}
return cache ? cache : null;
}
function updateCache (cache, input, data) {
if (!cache) return;
cache[input] = data;
}
function getFromCache (cache, input) {
if (!cache) return;
for (let i = input.length; i >= 0; --i) {
let cacheKey = input.slice(0, i);
if (cache[cacheKey] && (input === cacheKey || cache[cacheKey].complete)) {
return cache[cacheKey];
}
}
}
function thenPromise (promise, callback) {
if (!promise || typeof promise.then !== 'function') return;
return promise.then((data) => {
callback(null, data);
}, (err) => {
callback(err);
});
}
const stringOrNode = React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.node
]);
const Async = React.createClass({
propTypes: {
cache: React.PropTypes.any, // object to use to cache results, can be null to disable cache
ignoreAccents: React.PropTypes.bool, // whether to strip diacritics when filtering (shared with Select)
ignoreCase: React.PropTypes.bool, // whether to perform case-insensitive filtering (shared with Select)
isLoading: React.PropTypes.bool, // overrides the isLoading state when set to true
loadOptions: React.PropTypes.func.isRequired, // function to call to load options asynchronously
loadingPlaceholder: React.PropTypes.string, // replaces the placeholder while options are loading
minimumInput: React.PropTypes.number, // the minimum number of characters that trigger loadOptions
noResultsText: stringOrNode, // placeholder displayed when there are no matching search results (shared with Select)
placeholder: stringOrNode, // field placeholder, displayed when there's no value (shared with Select)
searchPromptText: React.PropTypes.string, // label to prompt for search input
searchingText: React.PropTypes.string, // message to display while options are loading
},
getDefaultProps () {
return {
cache: true,
ignoreAccents: true,
ignoreCase: true,
loadingPlaceholder: 'Loading...',
minimumInput: 0,
searchingText: 'Searching...',
searchPromptText: 'Type to search',
};
},
getInitialState () {
return {
cache: initCache(this.props.cache),
isLoading: false,
options: [],
};
},
componentWillMount () {
this._lastInput = '';
},
componentDidMount () {
this.loadOptions('');
},
componentWillReceiveProps (nextProps) {
if (nextProps.cache !== this.props.cache) {
this.setState({
cache: initCache(nextProps.cache),
});
}
},
focus () {
this.refs.select.focus();
},
resetState () {
this._currentRequestId = -1;
this.setState({
isLoading: false,
options: [],
});
},
getResponseHandler (input) {
let _requestId = this._currentRequestId = requestId++;
return (err, data) => {
if (err) throw err;
if (!this.isMounted()) return;
updateCache(this.state.cache, input, data);
if (_requestId !== this._currentRequestId) return;
this.setState({
isLoading: false,
options: data && data.options || [],
});
};
},
loadOptions (input) {
if (this.props.ignoreAccents) input = stripDiacritics(input);
if (this.props.ignoreCase) input = input.toLowerCase();
this._lastInput = input;
if (input.length < this.props.minimumInput) {
return this.resetState();
}
let cacheResult = getFromCache(this.state.cache, input);
if (cacheResult) {
return this.setState({
options: cacheResult.options,
});
}
this.setState({
isLoading: true,
});
let responseHandler = this.getResponseHandler(input);
return thenPromise(this.props.loadOptions(input, responseHandler), responseHandler);
},
render () {
let { noResultsText } = this.props;
let { isLoading, options } = this.state;
if (this.props.isLoading) isLoading = true;
let placeholder = isLoading ? this.props.loadingPlaceholder : this.props.placeholder;
if (!options.length) {
if (this._lastInput.length < this.props.minimumInput) noResultsText = this.props.searchPromptText;
if (isLoading) noResultsText = this.props.searchingText;
}
return (
<Select
{...this.props}
ref="select"
isLoading={isLoading}
noResultsText={noResultsText}
onInputChange={this.loadOptions}
options={options}
placeholder={placeholder}
/>
);
}
});
module.exports = Async;