cpui-components
Version:
201 lines (164 loc) • 6.97 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AutoComplete = undefined;
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 _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactAutosuggest = require('react-autosuggest');
var _reactAutosuggest2 = _interopRequireDefault(_reactAutosuggest);
var _Button = require('./../Button');
var _Button2 = _interopRequireDefault(_Button);
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 AutoComplete = exports.AutoComplete = function (_Component) {
_inherits(AutoComplete, _Component);
function AutoComplete() {
_classCallCheck(this, AutoComplete);
var _this = _possibleConstructorReturn(this, (AutoComplete.__proto__ || Object.getPrototypeOf(AutoComplete)).call(this));
_this.onChange = function (event, _ref) {
var newValue = _ref.newValue,
reason = _ref.reason;
_this.setState({ inputValue: newValue });
};
_this.onSuggestionSelectedWrapped = function (event, content) {
_this.props.onSuggestionSelected(content.suggestion);
if (_this.props.clearAfterSelected) {
_this.setState({ inputValue: '' });
}
};
_this.onSuggestionsFetchRequested = function (_ref2) {
var value = _ref2.value,
reason = _ref2.reason;
if (reason !== 'input-focused' && reason !== 'suggestion-selected') {
_this.setState({ inputValue: value });
_this.props.getSuggestions(value);
}
};
_this.clearSearch = function () {
_this.setState({ inputValue: '' });
_this.searchInput.input.focus();
};
_this.getInputProps = function () {
var inputProps = {
placeholder: _this.props.inputPlaceholder,
value: _this.state.inputValue,
onChange: _this.onChange,
autoFocus: _this.props.autoFocus
};
if (_this.props.disabled === true) {
inputProps.disabled = true;
}
return inputProps;
};
_this.render = function () {
var autosuggestCss = {
input: { width: '100%' },
suggestionHighlighted: {
background: '#efeee9',
border: 'none',
borderBottom: '1px solid transparent',
borderTop: '1px solid transparent',
borderRadius: 0,
margin: '0 !important'
}
};
var searchButton = _this.state.inputValue === '' ? _react2.default.createElement(
_Button2.default,
{ noStyles: true, id: 'searchButton', title: 'Search',
className: 'cp-AutoSearch-button' },
_react2.default.createElement(
'svg',
null,
_react2.default.createElement('use', { xlinkHref: '#iconSearch' })
)
) : _react2.default.createElement(
_Button2.default,
{ noStyles: true, id: 'clearSearch', title: 'Clear Search',
onClick: _this.clearSearch,
className: 'cp-AutoSearch-button',
style: { height: 42 } },
_react2.default.createElement(
'svg',
null,
_react2.default.createElement('use', { xlinkHref: '#iconClose' })
)
);
return _react2.default.createElement(
'div',
{ className: "cp-SearchBar" },
_react2.default.createElement(_reactAutosuggest2.default, { ref: function ref(searchInput) {
_this.searchInput = searchInput;
},
alwaysRenderSuggestions: true,
inputProps: _this.getInputProps(),
theme: autosuggestCss,
getSuggestionValue: _this.props.getSuggestionValue,
onSuggestionSelected: _this.onSuggestionSelectedWrapped,
onSuggestionsFetchRequested: _this.onSuggestionsFetchRequested,
renderSuggestion: _this.props.renderSuggestion,
suggestions: _this.state.suggestions.length > 0 && _this.state.inputValue !== '' ? _this.state.suggestions : []
}),
searchButton
);
};
_this.state = {
suggestions: [],
inputValue: ''
};
_this.onChange = _this.onChange.bind(_this);
_this.onSuggestionSelectedWrapped = _this.onSuggestionSelectedWrapped.bind(_this);
_this.onSuggestionsFetchRequested = _this.onSuggestionsFetchRequested.bind(_this);
_this.clearSearch = _this.clearSearch.bind(_this);
return _this;
}
_createClass(AutoComplete, [{
key: 'componentWillMount',
value: function componentWillMount() {
this.setState({ suggestions: this.props.suggestions });
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
this.setState({ suggestions: nextProps.suggestions });
}
// wrap so that consumer only has to worry about selected suggestion
}]);
return AutoComplete;
}(_react.Component);
AutoComplete.propTypes = {
inputPlaceholder: _propTypes2.default.string,
onSuggestionSelected: _propTypes2.default.func.isRequired,
getSuggestions: _propTypes2.default.func.isRequired,
getSuggestionValue: _propTypes2.default.func,
renderSuggestion: _propTypes2.default.func,
autoFocus: _propTypes2.default.bool,
suggestions: _propTypes2.default.array,
clearAfterSelected: _propTypes2.default.bool
};
AutoComplete.defaultProps = {
inputPlaceholder: 'Search',
renderSuggestion: function renderSuggestion(suggestion) {
return _react2.default.createElement(
'div',
null,
' ',
JSON.stringify(suggestion),
' '
);
},
// usually this is used for picking something and the action will be obvious --
// default behavior will be to clear, but is open to change.
getSuggestionValue: function getSuggestionValue(suggestion) {
return '';
},
autoFocus: false,
clearAfterSelected: true,
suggestions: []
};
exports.default = AutoComplete;
;