antd
Version:
An enterprise-class UI design language and React-based implementation
112 lines (101 loc) • 4.45 kB
JavaScript
import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
}return t;
};
import * as React from 'react';
import classNames from 'classnames';
import Input from './Input';
import Icon from '../icon';
import Button from '../button';
var Search = function (_React$Component) {
_inherits(Search, _React$Component);
function Search() {
_classCallCheck(this, Search);
var _this = _possibleConstructorReturn(this, (Search.__proto__ || Object.getPrototypeOf(Search)).apply(this, arguments));
_this.onSearch = function (e) {
var onSearch = _this.props.onSearch;
if (onSearch) {
onSearch(_this.input.input.value, e);
}
_this.input.focus();
};
_this.saveInput = function (node) {
_this.input = node;
};
return _this;
}
_createClass(Search, [{
key: 'focus',
value: function focus() {
this.input.focus();
}
}, {
key: 'blur',
value: function blur() {
this.input.blur();
}
}, {
key: 'getButtonOrIcon',
value: function getButtonOrIcon() {
var _props = this.props,
enterButton = _props.enterButton,
prefixCls = _props.prefixCls,
size = _props.size,
disabled = _props.disabled;
var enterButtonAsElement = enterButton;
var node = void 0;
if (!enterButton) {
node = React.createElement(Icon, { className: prefixCls + '-icon', type: 'search', key: 'searchIcon' });
} else if (enterButtonAsElement.type === Button || enterButtonAsElement.type === 'button') {
node = React.cloneElement(enterButtonAsElement, enterButtonAsElement.type === Button ? {
className: prefixCls + '-button',
size: size
} : {});
} else {
node = React.createElement(
Button,
{ className: prefixCls + '-button', type: 'primary', size: size, disabled: disabled, key: 'enterButton' },
enterButton === true ? React.createElement(Icon, { type: 'search' }) : enterButton
);
}
return React.cloneElement(node, {
onClick: this.onSearch
});
}
}, {
key: 'render',
value: function render() {
var _classNames;
var _a = this.props,
className = _a.className,
prefixCls = _a.prefixCls,
inputPrefixCls = _a.inputPrefixCls,
size = _a.size,
suffix = _a.suffix,
enterButton = _a.enterButton,
others = __rest(_a, ["className", "prefixCls", "inputPrefixCls", "size", "suffix", "enterButton"]);
delete others.onSearch;
var buttonOrIcon = this.getButtonOrIcon();
var searchSuffix = suffix ? [suffix, buttonOrIcon] : buttonOrIcon;
var inputClassName = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-enter-button', !!enterButton), _defineProperty(_classNames, prefixCls + '-' + size, !!size), _classNames));
return React.createElement(Input, _extends({ onPressEnter: this.onSearch }, others, { size: size, className: inputClassName, prefixCls: inputPrefixCls, suffix: searchSuffix, ref: this.saveInput }));
}
}]);
return Search;
}(React.Component);
export default Search;
Search.defaultProps = {
inputPrefixCls: 'ant-input',
prefixCls: 'ant-input-search',
enterButton: false
};