UNPKG

jimu-mobile

Version:

积木组件库助力移动端开发

217 lines (177 loc) 8.13 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _reactComposition = require('react-composition'); var _reactComposition2 = _interopRequireDefault(_reactComposition); var _getLocale = require('../localeprovider/getLocale'); var _zhCN = require('../localeprovider/zh-CN'); var _zhCN2 = _interopRequireDefault(_zhCN); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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; } /* eslint-disable no-script-url */ var Search = (_temp = _class = function (_React$Component) { _inherits(Search, _React$Component); _createClass(Search, null, [{ key: 'stopPropagation', value: function stopPropagation(e) { e.preventDefault(); e.stopPropagation(); } }]); function Search(props) { _classCallCheck(this, Search); var _this = _possibleConstructorReturn(this, (Search.__proto__ || Object.getPrototypeOf(Search)).call(this, props)); _this.state = { text: '', focus: false, isShowAction: false }; _this.focusHandle = _this.focusHandle.bind(_this); _this.blurHandle = _this.blurHandle.bind(_this); _this.changeHandle = _this.changeHandle.bind(_this); _this.clearHandle = _this.clearHandle.bind(_this); _this.cancelHandle = _this.cancelHandle.bind(_this); return _this; } _createClass(Search, [{ key: 'changeHandle', value: function changeHandle(e) { var text = e.target.value; if (e.reactComposition.composition === false && this.props.onChangeHandle) { // 解决中文输入法多次触发问题 this.props.onChangeHandle(text, e); } this.setState({ text: text }); } }, { key: 'cancelHandle', value: function cancelHandle(e) { Search.stopPropagation(e); this.setState({ focus: false, isShowAction: false, text: '' }); if (this.props.onCancelHandle) this.props.onCancelHandle(e); } }, { key: 'clearHandle', value: function clearHandle(e) { this.setState({ text: '' }); if (this.props.onClearHandle) this.props.onClearHandle(e); this.searchInput.focus(); } }, { key: 'blurHandle', value: function blurHandle() { if (this.state.text === '') { this.setState({ focus: false }); } this.props.onBlurHandle && this.props.onBlurHandle(); } }, { key: 'focusHandle', value: function focusHandle() { var onCancelHandle = this.props.onCancelHandle; this.setState({ focus: true, isShowAction: !!onCancelHandle }); this.props.onFocusHandle && this.props.onFocusHandle(); } }, { key: 'render', value: function render() { var _this2 = this; var _props = this.props, className = _props.className, onCancelHandle = _props.onCancelHandle, others = _objectWithoutProperties(_props, ['className', 'onCancelHandle']); var _state = this.state, focus = _state.focus, text = _state.text, isShowAction = _state.isShowAction; var locale = (0, _getLocale.getComponentLocale)(this.props, this.context, 'Search', function () { return _zhCN2.default.Search; }); var cancelText = locale.cancelText, placeholder = locale.placeholder; var cls = (0, _classnames2.default)(_defineProperty({ 'jimu-search-wrapper': true, 'jimu-search-layout-row': true, 'jimu-search-layout-row-center': true }, className, className)); var clsSearchWrapper = (0, _classnames2.default)({ 'jimu-search-input-wrapper': true, 'jimu-search-layout-row': true, 'jimu-search-layout-row-center': true, 'jimu-search-item-grow-1': true, 'jimu-search-focusing': focus }); return _react2.default.createElement( 'form', { action: 'javascript: void(0);' }, _react2.default.createElement( 'div', { className: cls }, _react2.default.createElement( 'div', { className: clsSearchWrapper }, _react2.default.createElement('input', _extends({ ref: function ref(n) { _this2.searchInput = n; }, type: 'search', className: 'jimu-search-input jimu-search-item-grow-1', onFocus: this.focusHandle, onBlur: this.blurHandle }, (0, _reactComposition2.default)({ onChange: this.changeHandle }), { value: text }, others, { placeholder: placeholder, autoComplete: 'off' })), text && _react2.default.createElement('span', { className: 'jimu-search-close icon-del', onClick: this.clearHandle }) ), isShowAction && _react2.default.createElement( 'a', { className: 'jimu-search-action', onClick: this.cancelHandle }, cancelText ) ) ); } }]); return Search; }(_react2.default.Component), _class.propTypes = { name: _propTypes2.default.string, onChangeHandle: _propTypes2.default.func, onClearHandle: _propTypes2.default.func, onSubmitHandle: _propTypes2.default.func, onFocusHandle: _propTypes2.default.func, onBlurHandle: _propTypes2.default.func, onCancelHandle: _propTypes2.default.func }, _class.defaultProps = { name: 'jimu-search', onChangeHandle: null, onClearHandle: null, onCancelHandle: null, onFocusHandle: null, onBlurHandle: null, onSubmitHandle: null }, _temp); Search.contextTypes = { jimuLocale: _propTypes2.default.object }; exports.default = Search;