ssc-refer
Version:
React refer component for SSC 3.0
203 lines (169 loc) • 5.71 kB
JavaScript
'use strict';
exports.__esModule = true;
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _lodash = require('lodash');
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
/**
* Created by Tiger on 17/2/22.
*/
var DEFAULT_DELAY_MS = 200;
/**
* HoC that encapsulates common behavior and functionality for doing
* asynchronous searches, including:
*
* - Debouncing user input
* - Query caching (optional)
* - Search prompt and empty results behaviors
*/
var referContainer = function referContainer(Refers) {
var _cache = {};
return _react2['default'].createClass({
propTypes: {
/**
* Delay, in milliseconds, before performing search.
*/
delay: _react.PropTypes.number,
/**
* Callback to perform when the search is executed.
*/
onSearch: _react.PropTypes.func.isRequired,
/**
* Options to be passed to the typeahead. Will typically be the query
* results, but can also be initial default options.
*/
options: _react.PropTypes.array,
/**
* Text displayed in the menu when there is no user input.
*/
promptText: _react.PropTypes.string,
/**
* Text displayed in the menu while the request is pending.
*/
searchText: _react.PropTypes.string,
/**
* Whether or not the component should cache query results.
*/
useCache: _react.PropTypes.bool
},
getDefaultProps: function getDefaultProps() {
return {
delay: DEFAULT_DELAY_MS,
minLength: 2,
options: [],
promptText: 'Type to search...',
searchText: 'Searching...',
useCache: true
};
},
getInitialState: function getInitialState() {
return {
hasSelection: false,
query: '',
requestPending: false
};
},
componentWillMount: function componentWillMount() {
this._handleSearchDebounced = (0, _lodash.debounce)(this._handleSearch, this.props.delay);
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
var options = nextProps.options,
useCache = nextProps.useCache;
var _state = this.state,
query = _state.query,
requestPending = _state.requestPending;
if (!requestPending) {
return;
}
if (useCache) {
_cache[query] = options;
}
this.setState({ requestPending: false });
},
componentWillUnmount: function componentWillUnmount() {
_cache = {};
},
render: function render() {
var _this = this;
var _props = this.props,
useCache = _props.useCache,
props = (0, _objectWithoutProperties3['default'])(_props, ['useCache']);
var cachedQuery = _cache[this.state.query];
return _react2['default'].createElement(Refers, (0, _extends3['default'])({}, props, {
emptyLabel: this._getEmptyLabel(),
isLoading: this.state.requestPending,
onChange: this._handleChange,
onInputChange: this._handleInputChange,
options: useCache && cachedQuery ? cachedQuery : this.props.options,
ref: function ref(instance) {
return _this._instance = instance;
}
}));
},
/**
* Make the component instance available.
*/
getInstance: function getInstance() {
return this._instance.getInstance();
},
_getEmptyLabel: function _getEmptyLabel() {
var _props2 = this.props,
emptyLabel = _props2.emptyLabel,
promptText = _props2.promptText,
searchText = _props2.searchText,
useCache = _props2.useCache;
var _state2 = this.state,
hasSelection = _state2.hasSelection,
query = _state2.query,
requestPending = _state2.requestPending;
if (!query.length || hasSelection) {
return promptText;
}
if (requestPending || useCache && !_cache[query]) {
return searchText;
}
return emptyLabel;
},
_handleChange: function _handleChange(selected) {
this.props.onChange && this.props.onChange(selected);
this.setState({ hasSelection: !!selected.length });
},
_handleInputChange: function _handleInputChange(query) {
this.props.onInputChange && this.props.onInputChange(query);
this._handleSearchDebounced(query);
},
_handleSearch: function _handleSearch(initialQuery) {
var _props3 = this.props,
caseSensitive = _props3.caseSensitive,
minLength = _props3.minLength,
onSearch = _props3.onSearch,
useCache = _props3.useCache;
var query = initialQuery.trim();
if (!caseSensitive) {
query = query.toLowerCase();
}
this.setState({ query: query });
if (!query || minLength && query.length < minLength) {
return;
}
// Use cached results, if available.
if (useCache && _cache[query]) {
return;
}
// Only perform a search on user input, not selection.
if (this.state.hasSelection) {
return;
}
// Perform the async search.
this.setState({ requestPending: true }, function () {
return onSearch(query);
});
}
});
};
exports['default'] = referContainer;
module.exports = exports['default'];