Skip to content

Commit f83d20d

Browse files
author
Edward Xiao
committed
no message
1 parent 9bdc773 commit f83d20d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

example/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ class Index extends Component {
874874
check: true, // Optional.[Bool].Default: true. To determin if you need to validate.
875875
required: true, // Optional.[Bool].Default: true. To determin if it is a required field.
876876
// type: 'string', // Optional.[String].Default: "string". Validation type, options are ['string', 'number'].
877+
// numberType: 'decimal', // Optional.[String].Default: "decimal". Validation number type, options are ['decimal', 'int']. Handy when the validation type is number.
877878
// showMsg: true, // Optional.[Bool].Default: true. To determin display the error message or not.
878879
// min: 2, // Optional.[Number].Default: 0. Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number.
879880
// max: 10, // Optional.[Number].Default: 0. Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number.

src/js/Inputs/Textbox.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE, DEFAULT_LOCALE, M
77
import reactInputsValidationCss from './react-inputs-validation.css';
88
const TYPE = 'textbox';
99
const VALIDATE_OPTION_TYPE_LIST = ['string', 'number'];
10+
const VALIDATE_NUMBER_TYPE_LIST = ['decimal', 'int'];
1011
const DEFAULT_MAX_LENGTH = 524288; // Default value is 524288
1112
const DEFAULT_AUTO_COMPLETE = 'on'; // Default value is on
1213
interface DefaultValidationOption {
@@ -15,6 +16,7 @@ interface DefaultValidationOption {
1516
min?: number;
1617
max?: number;
1718
type?: string;
19+
numberType?: string;
1820
name?: string;
1921
check?: boolean;
2022
showMsg?: boolean;
@@ -27,12 +29,13 @@ interface DefaultValidationOption {
2729
customFunc?: Function | undefined;
2830
}
2931
const getDefaultValidationOption = (obj: DefaultValidationOption) => {
30-
let { reg, min, max, type, name, check, length, regMsg, compare, required, showMsg, locale, msgOnError, msgOnSuccess, customFunc } = obj;
32+
let { reg, min, max, type, numberType, name, check, length, regMsg, compare, required, showMsg, locale, msgOnError, msgOnSuccess, customFunc } = obj;
3133
locale = typeof locale !== 'undefined' ? locale : DEFAULT_LOCALE;
3234
reg = typeof reg !== 'undefined' ? reg : '';
3335
min = typeof min !== 'undefined' ? min : 0;
3436
max = typeof max !== 'undefined' ? max : 0;
3537
type = typeof type !== 'undefined' ? type : 'string';
38+
numberType = typeof numberType !== 'undefined' ? numberType : 'string';
3639
name = typeof name !== 'undefined' ? name : '';
3740
check = typeof check !== 'undefined' ? check : true;
3841
showMsg = typeof showMsg !== 'undefined' ? showMsg : true;
@@ -48,6 +51,7 @@ const getDefaultValidationOption = (obj: DefaultValidationOption) => {
4851
min,
4952
max,
5053
type,
54+
numberType,
5155
name,
5256
check,
5357
length,
@@ -106,7 +110,7 @@ interface Props {
106110
onKeyUp?: (e: React.KeyboardEvent<HTMLElement>) => void;
107111
validationCallback?: (res: boolean) => void;
108112
}
109-
const autoFormatNumber = (v: number | string) => {
113+
const autoFormatNumber = (v: number | string, numberType: string) => {
110114
const DOT = '.';
111115
let res = '';
112116
let hasDot = false;
@@ -116,13 +120,18 @@ const autoFormatNumber = (v: number | string) => {
116120
const charCode = i.toLowerCase().charCodeAt(0);
117121
if ((charCode >= 48 && charCode <= 57) || (charCode === 46 && !hasDot)) {
118122
if (charCode === 46) {
123+
if (numberType === VALIDATE_NUMBER_TYPE_LIST[1]) {
124+
return;
125+
}
119126
hasDot = true;
120127
}
121128
res += i;
122129
}
123130
});
124-
if (res.length && res[0] === DOT) {
125-
res = `0${res}`;
131+
if (numberType === VALIDATE_NUMBER_TYPE_LIST[0]) {
132+
if (res.length && res[0] === DOT) {
133+
res = `0${res}`;
134+
}
126135
}
127136
return res;
128137
};
@@ -204,9 +213,9 @@ const component: React.FC<Props> = ({
204213
return;
205214
}
206215
}
207-
const { type } = option;
216+
const { type, numberType } = option;
208217
if (type === VALIDATE_OPTION_TYPE_LIST[1]) {
209-
v = String(autoFormatNumber(v));
218+
v = String(autoFormatNumber(v, VALIDATE_NUMBER_TYPE_LIST.indexOf(numberType) >= 0 ? numberType : VALIDATE_NUMBER_TYPE_LIST[0]));
210219
}
211220
setInternalValue(v);
212221
onChange && onChange(v, e);

0 commit comments

Comments
 (0)