UNPKG

weex-nuke

Version:

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

453 lines (400 loc) 15.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 _nukeView = require('nuke-view'); var _nukeView2 = _interopRequireDefault(_nukeView); var _nukeText = require('nuke-text'); var _nukeText2 = _interopRequireDefault(_nukeText); var _nukeImage = require('nuke-image'); var _nukeImage2 = _interopRequireDefault(_nukeImage); var _nukeEnv = require('nuke-env'); var _nukeHelper = require('nuke-helper'); var _nukeBaseInput = require('nuke-base-input'); var _nukeBaseInput2 = _interopRequireDefault(_nukeBaseInput); var _inheritKeys = require('../mods/inherit-keys'); var _inheritKeys2 = _interopRequireDefault(_inheritKeys); var _image = require('../mods/image'); var _image2 = _interopRequireDefault(_image); 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 Input = function (_Component) { _inherits(Input, _Component); function Input(props, context) { _classCallCheck(this, Input); var _this = _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).call(this, props)); var value = null; if ('value' in props) { value = props.value; } else if ('defaultValue' in props) { value = props.defaultValue; } var count = value && value.length || 0; _this.state = { value: value, inputValue: value, count: value && value.length || 0, maxLengthError: count > props.maxLength }; _this.fixedFont = context.commonConfigs && context.commonConfigs.fixedFont; if ('fixedFont' in props) { _this.fixedFont = props.fixedFont; } _this.inputHandler = _this.inputHandler.bind(_this); _this.changeHander = _this.changeHander.bind(_this); _this.clearHandler = _this.clearHandler.bind(_this); _this.focusHandler = _this.focusHandler.bind(_this); _this.blurHandler = _this.blurHandler.bind(_this); _this.getRef = _this.getRef.bind(_this); _this.getValue = _this.getValue.bind(_this); _this.focus = _this.focus.bind(_this); _this.blur = _this.blur.bind(_this); _this.clear = _this.clear.bind(_this); return _this; } _createClass(Input, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('value' in nextProps && (typeof nextProps.value === 'string' || typeof nextProps.value === 'number') && nextProps.value !== this.state.inputValue) { this.setState({ value: nextProps.value, inputValue: nextProps.value, count: nextProps.value.length, maxLengthError: nextProps.value.length > this.props.maxLength }); } } }, { key: 'inputHandler', value: function inputHandler(text, eventObj) { var maxLength = this.props.maxLength; this.setState({ // value: text, inputValue: text, count: text.length, maxLengthError: text.length > maxLength }); this.trigger('onInput', eventObj); } }, { key: 'changeHander', value: function changeHander(text, eventObj) { var maxLength = this.props.maxLength; this.setState({ value: text, inputValue: text, count: text.length, maxLengthError: text.length > maxLength }); this.trigger('onChange', text, eventObj); } }, { key: 'getInput', value: function getInput() { var _this2 = this; var _props = this.props, readOnly = _props.readOnly, disabled = _props.disabled, onInput = _props.onInput, onChange = _props.onChange, hasClear = _props.hasClear, inputStyle = _props.inputStyle, rows = _props.rows, multiple = _props.multiple, style = _props.style, others = _objectWithoutProperties(_props, ['readOnly', 'disabled', 'onInput', 'onChange', 'hasClear', 'inputStyle', 'rows', 'multiple', 'style']); var value = this.state.value; var styles = this.props.themeStyle; var inputAttrs = _extends({}, others, { value: typeof value !== null ? value : null, readOnly: readOnly, multiple: multiple, onInput: this.inputHandler, onChange: this.changeHander, onFocus: this.focusHandler, onBlur: this.blurHandler, disabled: disabled }); if (_nukeEnv.isWeex) { // 如果是多行文本,添加 returnKeyType 会导致无法换换 if (multiple && !this.props.onReturn) { delete inputAttrs.onReturn; delete inputAttrs.returnKeyType; } } else if (!disabled) { delete inputAttrs.disabled; } if (multiple) { inputAttrs.rows = rows || 3; inputAttrs.style = Object.assign({}, styles['multiple-input-ele']); if (inputStyle.height || style.height) { inputAttrs.style.height = inputStyle.height || style.height; } if (inputStyle.lineHeight || style.lineHeight) { inputAttrs.style.lineHeight = inputStyle.lineHeight || style.lineHeight; } } else { inputAttrs.style = Object.assign({}, styles['single-input-ele']); if (inputStyle.height || style.height) { inputAttrs.style.height = inputStyle.height || style.height; } inputAttrs.style.lineHeight = inputStyle.lineHeight || style.lineHeight || inputAttrs.style.height; } _inheritKeys2.default.map(function (item) { if (styles[item]) { inputAttrs.style[item] = styles[item]; } }); return (0, _rax.createElement)(_nukeBaseInput2.default, _extends({ fixedFont: this.fixedFont }, inputAttrs, { ref: function ref(n) { _this2.input = n; } })); } }, { key: 'clear', value: function clear() { this.setState({ value: '', count: 0 }); } }, { key: 'clearHandler', value: function clearHandler(e) { this.setState({ value: this.state.inputValue }); this.setState({ value: '', inputValue: '', count: 0 }); this.focus(); this.trigger('onClear', ''); } }, { 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: 'focus', value: function focus() { this.input.focus(); } }, { key: 'getRef', value: function getRef() { return this.input.getRef(); } }, { key: 'blur', value: function blur() { this.input.blur(); } }, { key: 'getValue', value: function getValue() { return this.input.getValue(); } }, { key: 'renderClear', value: function renderClear(styles, height) { var count = this.state.count; return count ? (0, _rax.createElement)( _nukeView2.default, { style: [styles.clearWrap, { height: height }], onClick: this.clearHandler }, (0, _rax.createElement)(_nukeImage2.default, { source: { uri: _image2.default.clear } // onClick={this.clearHandler} , style: styles.clear }) ) : null; } }, { key: 'renderCustomIcon', value: function renderCustomIcon(styles, height) { var _props$icon = this.props.icon, icon = _props$icon === undefined ? {} : _props$icon; var uri = icon.uri, _icon$onPress = icon.onPress, onPress = _icon$onPress === undefined ? function () {} : _icon$onPress, _icon$style = icon.style, style = _icon$style === undefined ? {} : _icon$style; if (!uri) return null; return (0, _rax.createElement)( _nukeView2.default, { style: [styles.icon, { height: height }, style], onClick: onPress }, (0, _rax.createElement)(_nukeImage2.default, { source: { uri: uri }, style: [styles['icon-image'], style] }) ); } }, { key: 'renderCount', value: function renderCount(styles) { var _props2 = this.props, maxLength = _props2.maxLength, multiple = _props2.multiple, renderCount = _props2.renderCount; var maxLengthError = this.state.maxLengthError; if (!maxLength || !renderCount) return null; var count = this.state.count; return (0, _rax.createElement)( _nukeView2.default, { x: 'count', style: styles['' + (multiple ? 'multiple-count-wrap' : 'single-count-wrap')] }, (0, _rax.createElement)( _nukeText2.default, { fixedFont: this.fixedFont, style: [styles['' + (multiple ? 'multiple-count-text' : 'single-count-text')], maxLengthError ? styles['count-error'] : {}] }, count, ' / ', maxLength ) ); } }, { key: 'render', value: function render() { var _props3 = this.props, readOnly = _props3.readOnly, disabled = _props3.disabled, _props3$style = _props3.style, style = _props3$style === undefined ? {} : _props3$style, renderCount = _props3.renderCount, hideErrorWhenFocus = _props3.hideErrorWhenFocus, hasClear = _props3.hasClear, multiple = _props3.multiple, status = _props3.status, errorMessage = _props3.errorMessage; var styles = this.props.themeStyle; var _state = this.state, focus = _state.focus, maxLengthError = _state.maxLengthError; var inputWrapperStyle = Object.assign({}, styles['input-wrap'], styles['' + (multiple ? 'multiple-wrap' : 'single-wrap')], readOnly ? styles.readonly : {}, status === 'error' ? styles['error-input-wrap'] : {}, focus ? styles['focus-input-wrap'] : {}, disabled ? styles[(multiple ? 'multiple' : 'single') + '-disabled'] : {}, style); var customHeight = inputWrapperStyle.height; // debugger; if (status === 'error' && !hideErrorWhenFocus || maxLengthError) { inputWrapperStyle = Object.assign(inputWrapperStyle, styles['error-input-wrap']); } if (disabled) { inputWrapperStyle = Object.assign(inputWrapperStyle, styles[(multiple ? 'multiple' : 'single') + '-disabled']); } var outerStyle = {}; _nukeHelper.bubbleKeys.map(function (item) { if (item in inputWrapperStyle) { outerStyle[item] = inputWrapperStyle[item]; } }); if (outerStyle.height) { delete outerStyle.height; } if (outerStyle.backgroundColor) { delete outerStyle.backgroundColor; } return (0, _rax.createElement)( _nukeView2.default, { x: 'normal-outter', style: outerStyle }, (0, _rax.createElement)( _nukeView2.default, { x: 'input-wrap', style: inputWrapperStyle }, this.getInput() ), hasClear ? this.renderClear(styles, customHeight) : null, !hasClear ? this.renderCustomIcon(styles, customHeight) : null, status === 'error' && errorMessage || renderCount ? (0, _rax.createElement)( _nukeView2.default, { x: 'help', style: styles.help }, !focus && status === 'error' && errorMessage ? (0, _rax.createElement)( _nukeText2.default, { fixedFont: this.fixedFont, style: styles['error-text'] }, errorMessage ) : null, this.renderCount(styles) ) : null ); } }]); return Input; }(_rax.Component); Input.propTypes = { defaultValue: _rax.PropTypes.any, onFocus: _rax.PropTypes.func, onInput: _rax.PropTypes.func, onBlur: _rax.PropTypes.func, onReturn: _rax.PropTypes.func, placeholder: _rax.PropTypes.string, readOnly: _rax.PropTypes.boolean, disabled: _rax.PropTypes.boolean, style: _rax.PropTypes.any, maxLength: _rax.PropTypes.number, multiple: _rax.PropTypes.boolean, inputStyle: _rax.PropTypes.any, returnKeyType: _rax.PropTypes.any, hasClear: _rax.PropTypes.boolean, rows: _rax.PropTypes.number, maxRows: _rax.PropTypes.number, type: _rax.PropTypes.oneOf(['text', 'url', 'password', 'tel', 'date', 'time', 'email']), status: _rax.PropTypes.oneOf(['success', 'error']), errorMessage: _rax.PropTypes.string, renderCount: _rax.PropTypes.boolean, value: _rax.PropTypes.string, themeStyle: _rax.PropTypes.any, hideErrorWhenFocus: _rax.PropTypes.boolean }; Input.defaultProps = { onFocus: function onFocus() {}, onInput: function onInput() {}, onBlur: function onBlur() {}, onReturn: function onReturn() {}, placeholder: '', readOnly: false, disabled: false, style: {}, multiple: false, inputStyle: {}, hasClear: false, type: 'text', status: 'success', themeStyle: {}, renderCount: false, placeholderColor: '#999999', hideErrorWhenFocus: true }; Input.contextTypes = { androidConfigs: _rax.PropTypes.any, commonConfigs: _rax.PropTypes.any }; exports.default = Input; module.exports = exports['default'];