react-select
Version:
A Select control built with and for ReactJS
212 lines (179 loc) • 8.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.makeAsyncSelect = exports.defaultProps = undefined;
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 _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _Select = require('./Select');
var _Select2 = _interopRequireDefault(_Select);
var _utils = require('./utils');
var _stateManager = require('./stateManager');
var _stateManager2 = _interopRequireDefault(_stateManager);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
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 defaultProps = exports.defaultProps = {
cacheOptions: false,
defaultOptions: false
};
var makeAsyncSelect = function makeAsyncSelect(SelectComponent) {
var _class, _temp;
return _temp = _class = function (_Component) {
_inherits(Async, _Component);
function Async(props) {
_classCallCheck(this, Async);
var _this = _possibleConstructorReturn(this, (Async.__proto__ || Object.getPrototypeOf(Async)).call(this));
_this.mounted = false;
_this.optionsCache = {};
_this.handleInputChange = function (newValue, actionMeta) {
var _this$props = _this.props,
cacheOptions = _this$props.cacheOptions,
onInputChange = _this$props.onInputChange;
// TODO
var inputValue = (0, _utils.handleInputChange)(newValue, actionMeta, onInputChange);
if (!inputValue) {
delete _this.lastRequest;
_this.setState({
inputValue: '',
loadedInputValue: '',
loadedOptions: [],
isLoading: false,
passEmptyOptions: false
});
return;
}
if (cacheOptions && _this.optionsCache[inputValue]) {
_this.setState({
inputValue: inputValue,
loadedInputValue: inputValue,
loadedOptions: _this.optionsCache[inputValue],
isLoading: false,
passEmptyOptions: false
});
} else {
var request = _this.lastRequest = {};
_this.setState({
inputValue: inputValue,
isLoading: true,
passEmptyOptions: !_this.state.loadedInputValue
}, function () {
_this.loadOptions(inputValue, function (options) {
if (!_this.mounted) return;
if (options) {
_this.optionsCache[inputValue] = options;
}
if (request !== _this.lastRequest) return;
delete _this.lastRequest;
_this.setState({
isLoading: false,
loadedInputValue: inputValue,
loadedOptions: options || [],
passEmptyOptions: false
});
});
});
}
return inputValue;
};
_this.state = {
defaultOptions: Array.isArray(props.defaultOptions) ? props.defaultOptions : undefined,
inputValue: '',
isLoading: props.defaultOptions === true ? true : false,
loadedOptions: [],
passEmptyOptions: false
};
return _this;
}
_createClass(Async, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
this.mounted = true;
var defaultOptions = this.props.defaultOptions;
if (defaultOptions === true) {
this.loadOptions('', function (options) {
if (!_this2.mounted) return;
var isLoading = !!_this2.lastRequest;
_this2.setState({ defaultOptions: options || [], isLoading: isLoading });
});
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
// if the cacheOptions prop changes, clear the cache
if (nextProps.cacheOptions !== this.props.cacheOptions) {
this.optionsCache = {};
}
if (nextProps.defaultOptions !== this.props.defaultOptions) {
this.setState({
defaultOptions: Array.isArray(nextProps.defaultOptions) ? nextProps.defaultOptions : undefined
});
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.mounted = false;
}
}, {
key: 'focus',
value: function focus() {
this.select.focus();
}
}, {
key: 'blur',
value: function blur() {
this.select.blur();
}
}, {
key: 'loadOptions',
value: function loadOptions(inputValue, callback) {
var loadOptions = this.props.loadOptions;
if (!loadOptions) return callback();
var loader = loadOptions(inputValue, callback);
if (loader && typeof loader.then === 'function') {
loader.then(callback, function () {
return callback();
});
}
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var _props = this.props,
loadOptions = _props.loadOptions,
props = _objectWithoutProperties(_props, ['loadOptions']);
var _state = this.state,
defaultOptions = _state.defaultOptions,
inputValue = _state.inputValue,
isLoading = _state.isLoading,
loadedInputValue = _state.loadedInputValue,
loadedOptions = _state.loadedOptions,
passEmptyOptions = _state.passEmptyOptions;
var options = passEmptyOptions ? [] : inputValue && loadedInputValue ? loadedOptions : defaultOptions || [];
return (
// $FlowFixMe
_react2.default.createElement(SelectComponent, _extends({}, props, {
filterOption: this.props.filterOption || null,
ref: function ref(_ref) {
_this3.select = _ref;
},
options: options,
isLoading: isLoading,
onInputChange: this.handleInputChange
}))
);
}
}]);
return Async;
}(_react.Component), _class.defaultProps = defaultProps, _temp;
};
exports.makeAsyncSelect = makeAsyncSelect;
exports.default = makeAsyncSelect((0, _stateManager2.default)(_Select2.default));