@jdcfe/yep-react
Version:
一套移动端的React组件库
280 lines (241 loc) • 9.5 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import classNames from 'classnames';
import Input from './Input';
import noop from '../_utils/noop';
import omit from 'lodash/omit';
import CloseCircleOutlined from '@jdcfe/icons-react/CloseCircleOutlined';
var InputItem = /*#__PURE__*/function (_React$PureComponent) {
_inherits(InputItem, _React$PureComponent);
var _super = _createSuper(InputItem);
function InputItem(props) {
var _this;
_classCallCheck(this, InputItem);
_this = _super.call(this, props);
_this.handleOnChange = function (value) {
var isMutated = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var onChange = _this.props.onChange;
if (!('value' in _this.props)) {
_this.setState({
value: value
});
} else {
_this.setState({
value: _this.props.value
});
}
if (onChange) {
isMutated ? setTimeout(function () {
return onChange(value);
}) : onChange(value);
}
};
_this.onInputChange = function (e) {
var value = e.target.value;
var type = _this.props.type;
var newValue = value;
switch (type) {
case 'phone':
newValue = value.replace(/\D/g, '').substring(0, 11);
var valueLen = newValue.length;
if (valueLen > 3 && valueLen < 8) {
newValue = "".concat(newValue.substr(0, 3), " ").concat(newValue.substr(3));
} else if (valueLen >= 8) {
newValue = "".concat(newValue.substr(0, 3), " ").concat(newValue.substr(3, 4), " ").concat(newValue.substr(7));
}
break;
case 'number':
newValue = value.replace(/\D/g, '');
break;
case 'text':
case 'password':
default:
break;
}
_this.handleOnChange(newValue, newValue !== value);
};
_this.onInputFocus = function (value) {
if (_this.debounceTimeout) {
clearTimeout(_this.debounceTimeout);
_this.debounceTimeout = null;
}
_this.setState({
focus: true
});
if (_this.props.onFocus) {
_this.props.onFocus(value);
}
};
_this.onInputBlur = function (value) {
if (_this.inputRef) {
// this.inputRef may be null if customKeyboard unmount
_this.debounceTimeout = window.setTimeout(function () {
if (document.activeElement !== (_this.inputRef && _this.inputRef.inputRef)) {
_this.setState({
focus: false
});
}
}, 200);
}
if (_this.props.onBlur) {
_this.props.onBlur(value);
}
};
_this.clearInput = function () {
if (_this.props.type !== 'password' && _this.props.updatePlaceholder) {
_this.setState({
placeholder: _this.props.value
});
}
_this.setState({
value: ''
});
if (_this.props.onChange) {
_this.props.onChange('');
}
_this.focus();
}; // this is instance method for user to use
_this.focus = function () {
if (_this.inputRef) {
_this.inputRef.focus();
}
};
_this.state = {
value: props.defaultValue || props.value || '',
placeholder: props.placeholder
};
return _this;
}
_createClass(InputItem, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if ('placeholder' in nextProps && !nextProps.updatePlaceholder) {
this.setState({
placeholder: nextProps.placeholder
});
}
if ('value' in nextProps) {
this.setState({
value: nextProps.value
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.debounceTimeout) {
window.clearTimeout(this.debounceTimeout);
this.debounceTimeout = null;
}
}
}, {
key: "render",
value: function render() {
var _classNames,
_this2 = this;
var _a = this.props,
className = _a.className,
prefixCls = _a.prefixCls,
prefixListCls = _a.prefixListCls,
style = _a.style,
type = _a.type,
name = _a.name,
defaultValue = _a.defaultValue,
disabled = _a.disabled,
editable = _a.editable,
maxLength = _a.maxLength,
error = _a.error,
labelNumber = _a.labelNumber,
children = _a.children,
extra = _a.extra,
clear = _a.clear,
clearIcon = _a.clearIcon,
onExtraClick = _a.onExtraClick,
onErrorClick = _a.onErrorClick,
restProps = __rest(_a, ["className", "prefixCls", "prefixListCls", "style", "type", "name", "defaultValue", "disabled", "editable", "maxLength", "error", "labelNumber", "children", "extra", "clear", "clearIcon", "onExtraClick", "onErrorClick"]);
var _this$state = this.state,
value = _this$state.value,
focus = _this$state.focus,
placeholder = _this$state.placeholder;
var wrapCls = classNames("".concat(prefixListCls, "-item"), "".concat(prefixCls, "-item"), "".concat(prefixListCls, "-item-middle"), className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-error"), error), _defineProperty(_classNames, "".concat(prefixCls, "-focus"), focus), _defineProperty(_classNames, "".concat(prefixCls, "-android"), focus), _classNames));
var labelCls = classNames("".concat(prefixCls, "-label"), "".concat(prefixCls, "-label-").concat(labelNumber));
return /*#__PURE__*/React.createElement("div", {
className: wrapCls
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixListCls, "-line")
}, children ? /*#__PURE__*/React.createElement("div", {
className: labelCls
}, children) : null, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-control")
}, /*#__PURE__*/React.createElement(Input, _extends({}, omit(restProps, 'updatePlaceholder'), {
ref: function ref(el) {
return _this2.inputRef = el;
},
value: value,
defaultValue: undefined,
style: style,
type: type,
maxLength: maxLength,
name: name,
placeholder: placeholder,
readOnly: !editable,
disabled: disabled,
onChange: this.onInputChange,
onFocus: this.onInputFocus,
onBlur: this.onInputBlur
}))), clear && editable && !disabled && value && "".concat(value).length > 0 ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-clear"),
onClick: this.clearInput
}, /*#__PURE__*/React.isValidElement(clearIcon) ? clearIcon : /*#__PURE__*/React.createElement(CloseCircleOutlined, {
style: {
color: '#999BAA'
}
})) : null, error ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-error-extra"),
onClick: onErrorClick
}) : null, extra !== '' ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-extra"),
onClick: onExtraClick
}, extra) : null));
}
}]);
return InputItem;
}(React.PureComponent);
InputItem.defaultProps = {
prefixCls: 'Yep-input',
prefixListCls: 'Yep-list',
style: {},
type: 'text',
editable: true,
disabled: false,
placeholder: '',
clear: false,
onChange: noop,
onBlur: noop,
onFocus: noop,
extra: '',
onExtraClick: noop,
error: false,
onErrorClick: noop,
labelNumber: 5,
updatePlaceholder: false
};
export default InputItem;