UNPKG

antd-search-box

Version:
224 lines (172 loc) 6.86 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _style3 = require('antd/lib/input/style'); var _input = require('antd/lib/input'); var _input2 = _interopRequireDefault(_input); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _style4 = require('antd/lib/icon/style'); var _icon = require('antd/lib/icon'); var _icon2 = _interopRequireDefault(_icon); var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _omit = require('omit.js'); var _omit2 = _interopRequireDefault(_omit); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } var SearchBox = function (_Component) { (0, _inherits3["default"])(SearchBox, _Component); function SearchBox(props) { (0, _classCallCheck3["default"])(this, SearchBox); var _this = (0, _possibleConstructorReturn3["default"])(this, _Component.call(this, props)); var defaultValue = props.defaultValue, value = props.value; var currentValue = ''; if (value !== undefined) { currentValue = value; } else if (defaultValue !== undefined) { currentValue = defaultValue; } _this.state = { value: currentValue, isFocus: false, isClearing: false }; _this.handleFocus = _this.handleFocus.bind(_this); _this.handleBlur = _this.handleBlur.bind(_this); _this.handleInputChange = _this.handleInputChange.bind(_this); _this.handleMouseDown = _this.handleMouseDown.bind(_this); _this.handleMouseLeave = _this.handleMouseLeave.bind(_this); _this.handleClick = _this.handleClick.bind(_this); _this.handlePressEnter = _this.handlePressEnter.bind(_this); return _this; } SearchBox.prototype.getClearButton = function getClearButton() {}; SearchBox.prototype.handleFocus = function handleFocus(e) { if (!this.state.isClearing) { this.setState({ isFocus: true }); var onFocus = this.props.onFocus; if (onFocus) onFocus(e); } }; SearchBox.prototype.handleBlur = function handleBlur(e) { if (!this.state.isClearing) { this.setState({ isFocus: false }); var onBlur = this.props.onBlur; if (onBlur) onBlur(e); } }; SearchBox.prototype.handleInputChange = function handleInputChange(e) { var _this2 = this; var value = e.target.value; this.setState({ value: value }, function () { var onChange = _this2.props.onChange; if (onChange) onChange(value); }); }; SearchBox.prototype.handleMouseDown = function handleMouseDown() { this.setState({ isClearing: true }); }; SearchBox.prototype.handleMouseLeave = function handleMouseLeave() { this.antdInput.refs.input.focus(); this.setState({ isClearing: false }); }; SearchBox.prototype.handleClick = function handleClick() { var _this3 = this; this.setState({ value: '' }, function () { var onChange = _this3.props.onChange; if (onChange) onChange(''); _this3.antdInput.refs.input.focus(); _this3.setState({ isClearing: false }); }); }; SearchBox.prototype.handlePressEnter = function handlePressEnter() { var onSearch = this.props.onSearch; if (onSearch) onSearch(this.state.value); }; SearchBox.prototype.render = function render() { var _classNames, _this4 = this; var _state = this.state, value = _state.value, isFocus = _state.isFocus; var _props = this.props, prefixCls = _props.prefixCls, className = _props.className, style = _props.style, size = _props.size; var cls = (0, _classnames2["default"])((_classNames = {}, (0, _defineProperty3["default"])(_classNames, prefixCls, 1), (0, _defineProperty3["default"])(_classNames, prefixCls + '-sm', size === 'small'), (0, _defineProperty3["default"])(_classNames, prefixCls + '-lg', size === 'large'), (0, _defineProperty3["default"])(_classNames, className, !!className), _classNames)); var clearButton = void 0; if (value && isFocus) { clearButton = _react2["default"].createElement( 'span', { ref: function ref(el) { return _this4.clearButton = el; }, onMouseDown: this.handleMouseDown, onMouseLeave: this.handleMouseLeave, onClick: this.handleClick }, _react2["default"].createElement(_icon2["default"], { type: 'close-circle' }) ); } else { this.clearButton = null; } var inputProps = (0, _omit2["default"])((0, _extends3["default"])({}, this.props, { value: value, ref: function ref(el) { return _this4.antdInput = el; }, prefix: _react2["default"].createElement(_icon2["default"], { type: 'search' }), suffix: clearButton, onFocus: this.handleFocus, onBlur: this.handleBlur, onChange: this.handleInputChange, onPressEnter: this.handlePressEnter }), ['prefixCls', 'className', 'style', 'onSearch']); return _react2["default"].createElement( 'span', { className: cls, style: style }, _react2["default"].createElement(_input2["default"], inputProps) ); }; return SearchBox; }(_react.Component); SearchBox.propTypes = { prefixCls: _react.PropTypes.string, className: _react.PropTypes.string, id: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]), name: _react.PropTypes.string, style: _react.PropTypes.object, defaultValue: _react.PropTypes.string, value: _react.PropTypes.string, placeholder: _react.PropTypes.string, size: _react.PropTypes.oneOf(['small', 'default', 'large']), disabled: _react.PropTypes.bool, readOnly: _react.PropTypes.bool, autoFocus: _react.PropTypes.bool, autoComplete: _react.PropTypes.oneOf(['on', 'off']), onFocus: _react.PropTypes.func, onBlur: _react.PropTypes.func, onChange: _react.PropTypes.func, onSearch: _react.PropTypes.func }; SearchBox.defaultProps = { prefixCls: 'antd-search-box', style: {}, size: 'default' }; exports["default"] = SearchBox; module.exports = exports['default'];