Skip to content

Commit

Permalink
- Provide numberType to the validation option. Validation numbe…
Browse files Browse the repository at this point in the history
…r type, options are ['decimal', 'int']. Handy when the validation type is number.
  • Loading branch information
Edward Xiao committed Jun 19, 2019
1 parent 8b80f77 commit 4885fe1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
21 changes: 16 additions & 5 deletions lib/components/Textbox.js
Expand Up @@ -79,6 +79,7 @@ var reactInputsValidationCss = {
};
var TYPE = 'textbox';
var VALIDATE_OPTION_TYPE_LIST = ['string', 'number'];
var VALIDATE_NUMBER_TYPE_LIST = ['decimal', 'int'];
var DEFAULT_MAX_LENGTH = 524288; // Default value is 524288

var DEFAULT_AUTO_COMPLETE = 'on'; // Default value is on
Expand All @@ -88,6 +89,7 @@ var getDefaultValidationOption = function getDefaultValidationOption(obj) {
min = obj.min,
max = obj.max,
type = obj.type,
numberType = obj.numberType,
name = obj.name,
check = obj.check,
length = obj.length,
Expand All @@ -104,6 +106,7 @@ var getDefaultValidationOption = function getDefaultValidationOption(obj) {
min = typeof min !== 'undefined' ? min : 0;
max = typeof max !== 'undefined' ? max : 0;
type = typeof type !== 'undefined' ? type : 'string';
numberType = typeof numberType !== 'undefined' ? numberType : 'string';
name = typeof name !== 'undefined' ? name : '';
check = typeof check !== 'undefined' ? check : true;
showMsg = typeof showMsg !== 'undefined' ? showMsg : true;
Expand All @@ -119,6 +122,7 @@ var getDefaultValidationOption = function getDefaultValidationOption(obj) {
min: min,
max: max,
type: type,
numberType: numberType,
name: name,
check: check,
length: length,
Expand Down Expand Up @@ -150,7 +154,7 @@ var getDefaultAsyncObj = function getDefaultAsyncObj(obj) {
};
};

var autoFormatNumber = function autoFormatNumber(v) {
var autoFormatNumber = function autoFormatNumber(v, numberType) {
var DOT = '.';
var res = '';
var hasDot = false;
Expand All @@ -159,15 +163,21 @@ var autoFormatNumber = function autoFormatNumber(v) {

if (charCode >= 48 && charCode <= 57 || charCode === 46 && !hasDot) {
if (charCode === 46) {
if (numberType === VALIDATE_NUMBER_TYPE_LIST[1]) {
return;
}

hasDot = true;
}

res += i;
}
});

if (res.length && res[0] === DOT) {
res = "0".concat(res);
if (numberType === VALIDATE_NUMBER_TYPE_LIST[0]) {
if (res.length && res[0] === DOT) {
res = "0".concat(res);
}
}

return res;
Expand Down Expand Up @@ -289,10 +299,11 @@ var component = function component(_ref) {
}
}

var type = option.type;
var type = option.type,
numberType = option.numberType;

if (type === VALIDATE_OPTION_TYPE_LIST[1]) {
v = String(autoFormatNumber(v));
v = String(autoFormatNumber(v, VALIDATE_NUMBER_TYPE_LIST.indexOf(numberType) >= 0 ? numberType : VALIDATE_NUMBER_TYPE_LIST[0]));
}

setInternalValue(v);
Expand Down

0 comments on commit 4885fe1

Please sign in to comment.