ukelli-ui
Version:
Base on React's UI lib. Make frontend's dev simpler and faster.
104 lines (103 loc) • 3.74 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Component } from 'react';
import { Call } from 'basic-helper';
/**
* 基础的输入框验证 class
*
* @export
* @class InputVerifyClass
* @extends {Component}
*/
var InputVerifyClass = /** @class */ (function (_super) {
__extends(InputVerifyClass, _super);
function InputVerifyClass(props) {
var _this = _super.call(this, props) || this;
_this.checkLen = function (val, lenRange) {
var _val = val.toString();
if (lenRange) {
var s = lenRange[0], e = lenRange[1], _a = lenRange[2], isSlice = _a === void 0 ? true : _a;
if (_val.length > e) {
if (isSlice) {
_val = _val.slice(0, e);
}
}
}
return _val;
};
var _a = props.defaultValue, defaultValue = _a === void 0 ? '' : _a, value = props.value;
_this.isControl = _this.checkProps('value');
_this.isMatchLen = _this.checkProps('lenRange');
_this.isMatchNumbRangeMode = _this.checkProps('numRange');
_this.isPass = _this.checkProps('lenRange');
_this.value = !_this.isControl ? defaultValue : value;
_this.state = {
value: defaultValue,
matchLen: true,
matchRange: true,
};
return _this;
}
InputVerifyClass.prototype.checkProps = function (field) {
return this.props.hasOwnProperty(field);
};
InputVerifyClass.prototype.getValue = function () {
return this.isControl ? this.props.value : this.state.value;
};
InputVerifyClass.prototype._onChange = function (val, props) {
if (props === void 0) { props = this.props; }
var onChange = props.onChange, lenRange = props.lenRange, numRange = props.numRange;
var _val = val;
_val = this.checkLen(_val, lenRange);
_val = this.checkNum(_val, numRange);
this.setState({
value: _val
});
this.isPass = this.isMatchLen && this.isMatchNumbRangeMode;
this.value = _val;
Call(onChange, _val);
};
InputVerifyClass.prototype._onFocus = function (val, event) {
var onFocus = this.props.onFocus;
Call(onFocus, val, event);
};
InputVerifyClass.prototype._onBlur = function (e) {
Call(this.props.onBlur, e);
};
InputVerifyClass.prototype.onClear = function () {
this._onChange('');
};
InputVerifyClass.prototype.checkNum = function (val, numRange) {
var _val = +(val);
var matchRange = this.state.matchRange;
if (this.isMatchNumbRangeMode) {
var isMatch = false;
var s = numRange[0], e = numRange[1];
if (_val > e)
_val = e;
if (_val < s) {
isMatch = false;
}
else {
isMatch = true;
}
matchRange !== isMatch && this.setState({
matchRange: isMatch
});
}
return _val;
};
return InputVerifyClass;
}(Component));
export default InputVerifyClass;