UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

350 lines (294 loc) 11.6 kB
'use strict'; /** @jsx createElement */ 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 _rax = require('rax'); var _nukeIcon = require('../../Icon/index.js'); var _nukeIcon2 = _interopRequireDefault(_nukeIcon); var _nukeDom = require('../../Dom/index.js'); var _nukeDom2 = _interopRequireDefault(_nukeDom); var _nukeNormalInput = require('../../NormalInput/index.js'); var _nukeNormalInput2 = _interopRequireDefault(_nukeNormalInput); var _nukeView = require('../../View/index.js'); var _nukeView2 = _interopRequireDefault(_nukeView); var _nukeText = require('../../Text/index.js'); var _nukeText2 = _interopRequireDefault(_nukeText); var _nukeTouchable = require('../../Touchable/index.js'); var _nukeTouchable2 = _interopRequireDefault(_nukeTouchable); var _nukeEnv = require('../../Env/index.js'); var _nukeThemeProvider = require('../../ThemeProvider/index.js'); var _styles = require('../styles/index.js'); var _styles2 = _interopRequireDefault(_styles); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; } var SearchBar = function (_Component) { _inherits(SearchBar, _Component); function SearchBar(props) { _classCallCheck(this, SearchBar); var _this = _possibleConstructorReturn(this, (SearchBar.__proto__ || Object.getPrototypeOf(SearchBar)).call(this, props)); var value = ''; if ('defaultValue' in props) { value = props.defaultValue; } else if ('defaultValue' in props) { value = props.value; } _this.state = { focus: false, value: value }; _this.userInputValue = value || ''; var events = ['focusHandler', 'blurHandler', 'changeHandler', 'inputHandler', 'searchHandler', 'returnHandler', 'textSearchHandler']; events.forEach(function (m) { _this[m] = _this[m].bind(_this); }); return _this; } // calcRem _createClass(SearchBar, [{ key: 'componentDidMount', value: function componentDidMount() { var _this2 = this; var centered = this.props.centered; if (!centered) return; _nukeDom2.default.getRect('parentDOM').then(function (wrap) { _this2.setState({ strentchStyle: { width: _nukeEnv.isWeb ? wrap.width + 'px' : wrap.width // web 端 offset 的单位是 px } }); }, function () {}); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('value' in nextProps) { this.setState({ value: nextProps.value }); this.userInputValue = nextProps.value; } } }, { key: 'render', value: function render() { var _props = this.props, value = _props.value, disabled = _props.disabled, defaultValue = _props.defaultValue, placeholder = _props.placeholder, onInput = _props.onInput, onChange = _props.onChange, onSearch = _props.onSearch, inputStyle = _props.inputStyle, _props$style = _props.style, style = _props$style === undefined ? {} : _props$style, styles = _props.themeStyle, others = _objectWithoutProperties(_props, ['value', 'disabled', 'defaultValue', 'placeholder', 'onInput', 'onChange', 'onSearch', 'inputStyle', 'style', 'themeStyle']); var searchbarWrapStyle = Object.assign({}, styles.wrap, style); return (0, _rax.createElement)( _nukeView2.default, _extends({ clsName: 'search-bar-wrap', style: searchbarWrapStyle }, others), (0, _rax.createElement)( _nukeView2.default, { clsName: 'body', ref: 'parentDOM', style: [styles.body, inputStyle] }, this.renderPlaceholder(styles), this.renderInput() ), this.renderSearchButton(styles) ); } // 输入框以及placeholder }, { key: 'renderInput', value: function renderInput() { var _this3 = this; var _props2 = this.props, maxLength = _props2.maxLength, disabled = _props2.disabled, styles = _props2.themeStyle; return (0, _rax.createElement)(_nukeNormalInput2.default, { ref: function ref(_ref) { _this3._input = _ref; }, style: styles['input-wrap'], type: 'search', returnKeyType: 'search', materialDesign: false, maxLength: maxLength, value: this.state.value, onFocus: this.focusHandler, onBlur: this.blurHandler, onReturn: this.returnHandler, disabled: disabled, inputStyle: styles['input-ele'], onInput: this.inputHandler, onChange: this.changeHandler }); } // 模拟的 placeholder }, { key: 'renderPlaceholder', value: function renderPlaceholder(styles) { var _props3 = this.props, placeholder = _props3.placeholder, centered = _props3.centered, placeholderColor = _props3.placeholderColor; var focus = this.state.focus; var element = void 0; var textStyle = {}; var TextAttrArr = ['color', 'fontSize', 'fontStyle', 'fontWeight']; TextAttrArr.forEach(function (item) { if (styles.placeholder[item]) { textStyle[item] = styles.placeholder[item]; } }); if (_nukeEnv.isWeb) { element = placeholder && !this.userInputValue && !focus ? (0, _rax.createElement)( _nukeText2.default, { style: [textStyle, { color: placeholderColor }] }, placeholder ) : null; } else { element = placeholder && !this.userInputValue && !focus ? (0, _rax.createElement)( _nukeText2.default, { style: [textStyle, { color: placeholderColor }] }, placeholder ) : null; } var iconDOM = (0, _rax.createElement)(_nukeIcon2.default, { style: [styles.icon, { color: placeholderColor }], name: 'search' }); var placeholderStyle = centered && !focus && !this.userInputValue ? _extends({}, styles['body-centered'], this.state.strentchStyle) : {}; return (0, _rax.createElement)( _nukeView2.default, { role: 'placeholder-wrap', ref: 'placeholderDOM', style: [styles.placeholder, placeholderStyle] }, iconDOM, element ); } // 搜索按钮 }, { key: 'renderSearchButton', value: function renderSearchButton(styles) { if (!this.props.showSearchButton) return null; return (0, _rax.createElement)( _nukeTouchable2.default, { onPress: this.searchHandler, style: styles.button }, (0, _rax.createElement)( _nukeText2.default, { onPress: this.textSearchHandler, style: styles['button-text'] }, this.props.locale.search ) ); } }, { key: 'focusHandler', value: function focusHandler(e) { this.setState({ focus: true }); this.trigger('onFocus', e); } }, { key: 'trigger', value: function trigger(fn) { if (typeof fn === 'string') fn = this.props[fn]; if (!(typeof fn === 'function')) return; for (var _len = arguments.length, attrs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { attrs[_key - 1] = arguments[_key]; } return fn.apply(this, attrs); } }, { key: 'blurHandler', value: function blurHandler(e) { this.setState({ focus: false }); this.trigger('onBlur', e); } }, { key: 'inputHandler', value: function inputHandler(e) { this.userInputValue = e.value; this.trigger('onInput', e); } }, { key: 'changeHandler', value: function changeHandler(value, e) { this.setState({ value: value }); this.userInputValue = value; this.trigger('onChange', value, e); } }, { key: 'returnHandler', value: function returnHandler(e) { var value = e.value; this.setState({ value: value }); this.searchHandler(); } }, { key: 'searchHandler', value: function searchHandler(e) { // alert(this.userInputValue); this._input.wrappedInstance.blur(); this.props.onSearch && this.props.onSearch({ value: this.userInputValue }); } }, { key: 'textSearchHandler', value: function textSearchHandler(e) { if (_nukeEnv.isWeb) { e.stopPropagation(); } this.searchHandler(); } }]); return SearchBar; }(_rax.Component); SearchBar.propTypes = { onCancel: _rax.PropTypes.func, onChange: _rax.PropTypes.func, onSearch: _rax.PropTypes.func, value: _rax.PropTypes.string, defaultValue: _rax.PropTypes.string, locale: _rax.PropTypes.object, disabled: _rax.PropTypes.bool, showSearchButton: _rax.PropTypes.bool, centered: _rax.PropTypes.bool, placeholderColor: _rax.PropTypes.string, inputStyle: _rax.PropTypes.any, style: _rax.PropTypes.any, placeholder: _rax.PropTypes.string, onInput: _rax.PropTypes.func }; SearchBar.defaultProps = { locale: { search: '搜索' }, onSearch: function onSearch() {}, showSearchButton: true, disabled: false, centered: false, defaultValue: null, onCancel: function onCancel() {}, inputStyle: {}, onChange: function onChange() {}, onInput: function onInput() {}, placeholderColor: '#cccccc', placeholder: '', style: {} }; SearchBar.displayName = 'SearchBar'; var StyledSearchBar = (0, _nukeThemeProvider.connectStyle)(_styles2.default, { withRef: true })(SearchBar); exports.default = StyledSearchBar; module.exports = exports['default'];