wix-style-react
Version:
wix-style-react
182 lines (146 loc) • 6.71 kB
JavaScript
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 _class, _temp;
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 _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; }
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import InputWithOptions from '../InputWithOptions';
import SearchIcon from 'wix-ui-icons-common/Search';
import WixComponent from '../BaseComponents/WixComponent';
import { StringUtils } from '../utils/StringUtils';
import styles from './Search.scss';
/**
* Search component with suggestions based on input value listed in dropdown
*/
var Search = (_temp = _class = function (_WixComponent) {
_inherits(Search, _WixComponent);
function Search(props) {
_classCallCheck(this, Search);
var _this = _possibleConstructorReturn(this, (Search.__proto__ || Object.getPrototypeOf(Search)).call(this, props));
_this._onChange = function (e) {
if (_this._isControlled) {
_this.props.onChange(e);
} else {
_this.setState({
inputValue: e.target.value
});
}
};
_this._onClear = function () {
var onClear = _this.props.onClear;
if (!_this.state.collapsed && _this.props.expandable) {
_this.setState({
collapsed: true
});
}
onClear && onClear();
};
_this._currentValue = function () {
var value = void 0;
if (_this._isControlled) {
value = _this.props.value;
} else {
value = _this.state.inputValue;
}
return value;
};
_this._onBlur = function () {
var onBlur = _this.props.onBlur;
if (!_this.state.collapsed && _this.props.expandable) {
var value = _this._currentValue();
if (value === '') {
_this.setState({
collapsed: true
});
}
}
onBlur && onBlur();
};
_this._onWrapperClick = function () {
if (_this.props.expandable && _this.state.collapsed) {
_this.searchInput.input.focus();
_this.setState({ collapsed: false });
}
};
_this._onWrapperMouseDown = function (e) {
// We need to capture mouse down and prevent it's event if the input
// is already open
if (_this.props.expandable && !_this.state.collapsed) {
var value = _this._currentValue();
if (value === '') {
e.preventDefault();
}
}
};
var initialValue = !_this._isControlled && props.defaultValue || '';
_this.state = {
inputValue: initialValue,
collapsed: props.expandable && initialValue === '' && !props.autoFocus
};
return _this;
}
_createClass(Search, [{
key: 'render',
value: function render() {
var _classNames,
_this2 = this;
var wrapperClasses = classNames((_classNames = {}, _defineProperty(_classNames, styles.expandableStyles, this.props.expandable), _defineProperty(_classNames, styles.collapsed, this.state.collapsed && this.props.expandable), _defineProperty(_classNames, styles.expanded, !this.state.collapsed && this.props.expandable), _classNames));
return React.createElement(
'div',
{
className: wrapperClasses,
onClick: this._onWrapperClick,
onMouseDown: this._onWrapperMouseDown
},
React.createElement(InputWithOptions, _extends({}, this.props, {
ref: function ref(r) {
return _this2.searchInput = r;
},
roundInput: true,
prefix: React.createElement(
'div',
{ className: styles.leftIcon },
React.createElement(SearchIcon, null)
),
menuArrow: false,
clearButton: true,
closeOnSelect: true,
showOptionsIfEmptyInput: false,
options: this._filteredOptions,
onClear: this._onClear,
onChange: this._onChange,
onBlur: this._onBlur,
highlight: true
}))
);
}
}, {
key: '_isControlled',
get: function get() {
return 'value' in this.props && 'onChange' in this.props;
}
}, {
key: '_filteredOptions',
get: function get() {
var _props = this.props,
options = _props.options,
value = _props.value;
var searchText = this._isControlled ? value : this.state.inputValue;
return options.filter(function (option) {
return searchText && searchText.length ? StringUtils.includesCaseInsensitive(option.value, searchText.trim()) : true;
});
}
}]);
return Search;
}(WixComponent), _class.displayName = 'Search', _class.propTypes = _extends({}, InputWithOptions.propTypes, {
placeholder: PropTypes.string,
expandable: PropTypes.bool
}), _class.defaultProps = _extends({}, InputWithOptions.defaultProps, {
placeholder: 'Search',
expandable: false
}), _temp);
export { Search as default };