react-select-v1
Version:
A Select control built with and for ReactJS
271 lines (213 loc) • 9.29 kB
JavaScript
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 _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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 _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _Select = require('./Select');
var _Select2 = _interopRequireDefault(_Select);
var _stripDiacritics = require('./utils/stripDiacritics');
var _stripDiacritics2 = _interopRequireDefault(_stripDiacritics);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 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
loadOptions: _propTypes2.default.func.isRequired, // callback to load options asynchronously; (inputValue: string, callback: Function): ?Promise
loadingPlaceholder: _propTypes2.default.oneOfType([// replaces the placeholder while options are loading
_propTypes2.default.string, _propTypes2.default.node]),
multi: _propTypes2.default.bool, // multi-value input
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) {}
onInputChange: _propTypes2.default.func, // optional for keeping track of what is being typed
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]),
searchPromptText: _propTypes2.default.oneOfType([// label to prompt for search input
_propTypes2.default.string, _propTypes2.default.node]),
value: _propTypes2.default.any // initial field value
};
var defaultCache = {};
var defaultChildren = function defaultChildren(props) {
return _react2.default.createElement(_Select2.default, props);
};
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);
var _this = _possibleConstructorReturn(this, (Async.__proto__ || Object.getPrototypeOf(Async)).call(this, props, context));
_this._cache = props.cache === defaultCache ? {} : props.cache;
_this.state = {
inputValue: '',
isLoading: false,
options: props.options
};
_this.onInputChange = _this.onInputChange.bind(_this);
return _this;
}
_createClass(Async, [{
key: 'componentDidMount',
value: function componentDidMount() {
var autoload = this.props.autoload;
if (autoload) {
this.loadOptions('');
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.options !== this.props.options) {
this.setState({
options: nextProps.options
});
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this._callback = null;
}
}, {
key: 'loadOptions',
value: function loadOptions(inputValue) {
var _this2 = this;
var loadOptions = this.props.loadOptions;
var cache = this._cache;
if (cache && Object.prototype.hasOwnProperty.call(cache, inputValue)) {
this._callback = null;
this.setState({
isLoading: false,
options: cache[inputValue]
});
return;
}
var callback = function callback(error, data) {
var options = data && data.options || [];
if (cache) {
cache[inputValue] = options;
}
if (callback === _this2._callback) {
_this2._callback = null;
_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
});
}
}
}, {
key: 'onInputChange',
value: function onInputChange(inputValue) {
var _props = this.props,
ignoreAccents = _props.ignoreAccents,
ignoreCase = _props.ignoreCase,
onInputChange = _props.onInputChange;
var newInputValue = inputValue;
if (onInputChange) {
var value = onInputChange(newInputValue);
// Note: != used deliberately here to catch undefined and null
if (value != null && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object') {
newInputValue = '' + value;
}
}
var transformedInputValue = newInputValue;
if (ignoreAccents) {
transformedInputValue = (0, _stripDiacritics2.default)(transformedInputValue);
}
if (ignoreCase) {
transformedInputValue = transformedInputValue.toLowerCase();
}
this.setState({ inputValue: newInputValue });
this.loadOptions(transformedInputValue);
// Return new input value, but without applying toLowerCase() to avoid modifying the user's view case of the input while typing.
return newInputValue;
}
}, {
key: 'noResultsText',
value: function noResultsText() {
var _props2 = this.props,
loadingPlaceholder = _props2.loadingPlaceholder,
noResultsText = _props2.noResultsText,
searchPromptText = _props2.searchPromptText;
var _state = this.state,
inputValue = _state.inputValue,
isLoading = _state.isLoading;
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,
children = _props3.children,
loadingPlaceholder = _props3.loadingPlaceholder,
placeholder = _props3.placeholder;
var _state2 = this.state,
isLoading = _state2.isLoading,
options = _state2.options;
var props = {
noResultsText: this.noResultsText(),
placeholder: isLoading ? loadingPlaceholder : placeholder,
options: isLoading && loadingPlaceholder ? [] : options,
ref: function ref(_ref) {
return _this3.select = _ref;
}
};
return children(_extends({}, this.props, props, {
isLoading: isLoading,
onInputChange: this.onInputChange
}));
}
}]);
return Async;
}(_react.Component);
exports.default = Async;
Async.propTypes = propTypes;
Async.defaultProps = defaultProps;
;