UNPKG

lm-form

Version:

* 作者:liuduan * 邮箱:liuduan.05.05@163.com * 版本:**`1.0.2`**

262 lines (218 loc) 10.3 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _jsxFileName = 'src/input.js'; 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 _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 _autoclear = require('./autoclear'); var _autoclear2 = _interopRequireDefault(_autoclear); require('./index.scss'); 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; } // wrapper for input var Input = function (_Component) { _inherits(Input, _Component); function Input(props) { _classCallCheck(this, Input); var _this = _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).call(this, props)); _this.state = { value: 'value' in props ? props.value : props.defaultValue, xshow: false }; _this.handleChange = _this.handleChange.bind(_this); _this.handleFocus = _this.handleFocus.bind(_this); _this.handleBlur = _this.handleBlur.bind(_this); _this.handleClear = _this.handleClear.bind(_this); _this.timer = null; return _this; } _createClass(Input, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('value' in nextProps) { this.setState({ value: nextProps.value }); } else if ('defaultValue' in nextProps) { this.setState({ value: nextProps.defaultValue }); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { clearTimeout(this.timer); } }, { key: 'handleChange', value: function handleChange(e) { var _this2 = this; e.persist(); // 在React.js中执行去抖动 var value = e.target.value; // type === number, maxlength not work bugfix if (this.props.type === 'number') { value = this.maxLengthCheck(value); } this.setState({ value: value }, function () { _this2.controlXshow(value); // 自动显示clear btn _this2.props.onChange(value); }); } }, { key: 'maxLengthCheck', value: function maxLengthCheck(value) { if (!value) return ''; value = ('' + value).length > this.props.maxLength ? ('' + value).slice(0, this.props.maxLength) : value; value = parseFloat(value, 10); try { return Number.isNaN(value) ? '' : value; } catch (error) { return (/^NaN$/.test(value) ? '' : value ); } } }, { key: 'handleFocus', value: function handleFocus(e) { e.persist(); // 在React.js中执行去抖动 // readonly直接返回 if (this.props.readOnly || this.props.readonly) return; var value = this.state.value; // 如果有值则显示x this.controlXshow(value); // 自动显示clear btn this.props.onFocus(value); } }, { key: 'handleBlur', value: function handleBlur(e) { var _this3 = this; e.persist(); // 在React.js中执行去抖动 // 通过异步操作保证先执行clearbtn中click事件,后执行input的onblur事件,保证clearbtn的存在可以清楚value var xshow = this.state.xshow; clearTimeout(this.timer); this.timer = setTimeout(function () { // 异步执行 需要先判断目标是否被删除 if (!e.target) return; // 自动隐藏clear btn if (xshow) { _this3.controlXshow(''); } }, 20); this.props.onBlur(this.state.value); } // 清空input }, { key: 'handleClear', value: function handleClear() { var _this4 = this; this.setState({ value: '' }, function () { _this4.props.onChange(''); }); } // 控制x号显示隐藏 }, { key: 'controlXshow', value: function controlXshow(value) { this.setState({ xshow: !!('' + value) }); } }, { key: 'render', value: function render() { var _this5 = this; var _props = this.props, className = _props.className, children = _props.children, type = _props.type, defaultValue = _props.defaultValue, value = _props.value, placeholder = _props.placeholder, onChange = _props.onChange, onFocus = _props.onFocus, onBlur = _props.onBlur, err = _props.err, refName = _props.refName, useClear = _props.useClear, others = _objectWithoutProperties(_props, ['className', 'children', 'type', 'defaultValue', 'value', 'placeholder', 'onChange', 'onFocus', 'onBlur', 'err', 'refName', 'useClear']); var xshow = this.state.xshow; var clsPrefix = 'lm-input'; var cls = (0, _classnames2.default)(clsPrefix, _defineProperty({}, clsPrefix + '-err', err), className); return _react2.default.createElement( 'div', _defineProperty({ className: cls, __source: { fileName: _jsxFileName, lineNumber: 162 }, __self: this }, '__self', this), _react2.default.createElement('input', Object.assign({ ref: function ref(el) { return _this5[refName] = el; }, type: type, value: this.state.value || this.state.value === 0 ? this.state.value : '', placeholder: placeholder, onChange: this.handleChange, onFocus: this.handleFocus, onBlur: this.handleBlur }, others, _defineProperty({ __source: { fileName: _jsxFileName, lineNumber: 163 }, __self: this }, '__self', this))), useClear ? _react2.default.createElement(_autoclear2.default, _defineProperty({ show: xshow, err: err, onClear: this.handleClear, __source: { fileName: _jsxFileName, lineNumber: 174 }, __self: this }, '__self', this)) : null, children ); } }]); return Input; }(_react.Component); Input.propTypes = { className: _propTypes2.default.string, // 自定义类名 placeholder: _propTypes2.default.string, // 提示信息 defaultValue: _propTypes2.default.string, // 默认值,用于非控制input value: _propTypes2.default.oneOfType([// 值,配合onChange使用 _propTypes2.default.string, _propTypes2.default.number]), type: _propTypes2.default.string, // input类型 err: _propTypes2.default.bool, // 判断输入是否符合错误,错误:true,正确:false onChange: _propTypes2.default.func, // onchange事件回调 onFocus: _propTypes2.default.func, // onfocus事件回调 onBlur: _propTypes2.default.func, // onblur事件回调 refName: _propTypes2.default.string, // ref属性值 useClear: _propTypes2.default.bool // 是否使用clear组件 }; Input.defaultProps = { placeholder: '请输入', defaultValue: '', type: 'text', err: false, onChange: function onChange() {}, onFocus: function onFocus() {}, onBlur: function onBlur() {}, refName: 'inputRef', useClear: true }; ; exports.default = Input; module.exports = exports['default'];