forked from JedWatson/react-select
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAsync.js
270 lines (217 loc) · 9.32 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
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
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _Select = require('./Select');
var _Select2 = _interopRequireDefault(_Select);
var _utilsStripDiacritics = require('./utils/stripDiacritics');
var _utilsStripDiacritics2 = _interopRequireDefault(_utilsStripDiacritics);
var propTypes = {
autoload: _propTypes2['default'].bool.isRequired, // automatically call the `loadOptions` prop on-mount; defaults to true
cache: _propTypes2['default'].any, // object to use to cache results; set to null/false to disable caching
children: _propTypes2['default'].func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element
ignoreAccents: _propTypes2['default'].bool, // strip diacritics when filtering; defaults to true
ignoreCase: _propTypes2['default'].bool, // perform case-insensitive filtering; defaults to true
loadingPlaceholder: _propTypes2['default'].oneOfType([// replaces the placeholder while options are loading
_propTypes2['default'].string, _propTypes2['default'].node]),
loadOptions: _propTypes2['default'].func.isRequired, // callback to load options asynchronously; (inputValue: string, callback: Function): ?Promise
multi: _propTypes2['default'].bool, // multi-value input
options: _propTypes2['default'].array.isRequired, // array of options
placeholder: _propTypes2['default'].oneOfType([// field placeholder, displayed when there's no value (shared with Select)
_propTypes2['default'].string, _propTypes2['default'].node]),
noResultsText: _propTypes2['default'].oneOfType([// field noResultsText, displayed when no options come back from the server
_propTypes2['default'].string, _propTypes2['default'].node]),
onChange: _propTypes2['default'].func, // onChange handler: function (newValue) {}
searchPromptText: _propTypes2['default'].oneOfType([// label to prompt for search input
_propTypes2['default'].string, _propTypes2['default'].node]),
onInputChange: _propTypes2['default'].func, // optional for keeping track of what is being typed
value: _propTypes2['default'].any };
// initial field value
var defaultCache = {};
var defaultProps = {
autoload: true,
cache: defaultCache,
children: defaultChildren,
ignoreAccents: true,
ignoreCase: true,
loadingPlaceholder: 'Loading...',
options: [],
searchPromptText: 'Type to search'
};
var Async = (function (_Component) {
_inherits(Async, _Component);
function Async(props, context) {
_classCallCheck(this, Async);
_get(Object.getPrototypeOf(Async.prototype), 'constructor', this).call(this, props, context);
this._cache = props.cache === defaultCache ? {} : props.cache;
this.state = {
isLoading: false,
options: props.options
};
this._onInputChange = this._onInputChange.bind(this);
}
_createClass(Async, [{
key: 'componentDidMount',
value: function componentDidMount() {
var autoload = this.props.autoload;
if (autoload) {
this.loadOptions('');
}
}
}, {
key: 'componentWillUpdate',
value: function componentWillUpdate(nextProps, nextState) {
var _this = this;
var propertiesToSync = ['options'];
propertiesToSync.forEach(function (prop) {
if (_this.props[prop] !== nextProps[prop]) {
_this.setState(_defineProperty({}, prop, nextProps[prop]));
}
});
}
}, {
key: 'clearOptions',
value: function clearOptions() {
this.setState({ options: [] });
}
}, {
key: 'loadOptions',
value: function loadOptions(inputValue) {
var _this2 = this;
var loadOptions = this.props.loadOptions;
var cache = this._cache;
if (cache && cache.hasOwnProperty(inputValue)) {
this.setState({
options: cache[inputValue]
});
return;
}
var callback = function callback(error, data) {
if (callback === _this2._callback) {
_this2._callback = null;
var options = data && data.options || [];
if (cache) {
cache[inputValue] = options;
}
_this2.setState({
isLoading: false,
options: options
});
}
};
// Ignore all but the most recent request
this._callback = callback;
var promise = loadOptions(inputValue, callback);
if (promise) {
promise.then(function (data) {
return callback(null, data);
}, function (error) {
return callback(error);
});
}
if (this._callback && !this.state.isLoading) {
this.setState({
isLoading: true
});
}
return inputValue;
}
}, {
key: '_onInputChange',
value: function _onInputChange(inputValue) {
var _props = this.props;
var ignoreAccents = _props.ignoreAccents;
var ignoreCase = _props.ignoreCase;
var onInputChange = _props.onInputChange;
if (ignoreAccents) {
inputValue = (0, _utilsStripDiacritics2['default'])(inputValue);
}
if (ignoreCase) {
inputValue = inputValue.toLowerCase();
}
if (onInputChange) {
onInputChange(inputValue);
}
return this.loadOptions(inputValue);
}
}, {
key: 'inputValue',
value: function inputValue() {
if (this.select) {
return this.select.state.inputValue;
}
return '';
}
}, {
key: 'noResultsText',
value: function noResultsText() {
var _props2 = this.props;
var loadingPlaceholder = _props2.loadingPlaceholder;
var noResultsText = _props2.noResultsText;
var searchPromptText = _props2.searchPromptText;
var isLoading = this.state.isLoading;
var inputValue = this.inputValue();
if (isLoading) {
return loadingPlaceholder;
}
if (inputValue && noResultsText) {
return noResultsText;
}
return searchPromptText;
}
}, {
key: 'focus',
value: function focus() {
this.select.focus();
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var _props3 = this.props;
var children = _props3.children;
var loadingPlaceholder = _props3.loadingPlaceholder;
var placeholder = _props3.placeholder;
var _state = this.state;
var isLoading = _state.isLoading;
var options = _state.options;
var props = {
noResultsText: this.noResultsText(),
placeholder: isLoading ? loadingPlaceholder : placeholder,
options: isLoading && loadingPlaceholder ? [] : options,
ref: function ref(_ref) {
return _this3.select = _ref;
},
onChange: function onChange(newValues) {
if (_this3.props.multi && _this3.props.value && newValues.length > _this3.props.value.length) {
_this3.clearOptions();
}
_this3.props.onChange(newValues);
}
};
return children(_extends({}, this.props, props, {
isLoading: isLoading,
onInputChange: this._onInputChange
}));
}
}]);
return Async;
})(_react.Component);
exports['default'] = Async;
Async.propTypes = propTypes;
Async.defaultProps = defaultProps;
function defaultChildren(props) {
return _react2['default'].createElement(_Select2['default'], props);
}
module.exports = exports['default'];