Skip to content

Commit 36967e4

Browse files
author
Edward Xiao
committed
no message
1 parent b8079c5 commit 36967e4

File tree

9 files changed

+44
-12
lines changed

9 files changed

+44
-12
lines changed

docs/v4-doc.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
|**validationOption.required** |**Opt**|**Bool**|**To determin if it is a required field.**|**true** |
5151
|**validationOption.type** |**Opt**|**Str** |**Validation type, options are ['string', 'number', 'alphanumeric', 'alpha'~~, 'phone'~~].**|**"string"**|
5252
|**validationOption.numberType** |**Opt**|**Str** |**Validation number type, options are ['decimal', 'int']. Handy when the validation type is number.**|**"decimal"**|
53+
|**validationOption.mantissa** |**Opt**|**Num** |**Validation number precision. Handy when the validation type is number.**|**-1**|
5354
|**validationOption.showMsg** |**Opt**|**Bool**|**To determin display the error message or not.**|**true** |
5455
|**validationOption.min** |**Opt**|**Num**|**Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number.**|**0** |
5556
|**validationOption.max** |**Opt**|**Num**|**Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number.**|**0** |
@@ -106,6 +107,7 @@ import 'react-inputs-validation/lib/react-inputs-validation.min.css';
106107
required: true, //Optional.[Bool].Default: true. To determin if it is a required field.
107108
// type: 'string', //Optional.[String].Default: "string". Validation type, options are ['string', 'number', 'alphanumeric', 'alpha'].
108109
// numberType: 'decimal', // Optional.[String].Default: "decimal". Validation number type, options are ['decimal', 'int']. Handy when the validation type is number.
110+
// mantissa: 2, // Optional.[Number].Default: -1. Number precision.
109111
// showMsg: true, //Optional.[Bool].Default: true. To determin display the error message or not.
110112
// min: 2, //Optional.[Number].Default: 0. Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number.
111113
// max: 10, //Optional.[Number].Default: 0. Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number.

example/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import { Textbox } from 'react-inputs-validation';
8585
type: 'number', // Optional.[String].Default: "string". Validation type, options are ['string', 'number', 'alphanumeric', 'alpha'].
8686
min: 10, // Optional.[Number].Default: 0. Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number.
8787
max: 100 // Optional.[Number].Default: 0. Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number.
88+
// mantissa: 2, // Optional.[Number].Default: -1. Number precision.
8889
}}
8990
/>
9091
\`\`\`
@@ -652,6 +653,7 @@ class Index extends Component {
652653
type: 'number', // Optional.[String].Default: "string". Validation type, options are ['string', 'number', 'alphanumeric', 'alpha'].
653654
min: 10, // Optional.[Number].Default: 0. Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number.
654655
max: 100, // Optional.[Number].Default: 0. Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number.
656+
// mantissa: 2, // Optional.[Number].Default: -1. Number precision.
655657
}}
656658
/>
657659
<br />
@@ -870,6 +872,7 @@ class Index extends Component {
870872
type: 'number',
871873
// type: 'string', // Optional.[String].Default: "string". Validation type, options are ['string', 'number', 'alphanumeric', 'alpha'].
872874
// numberType: 'decimal', // Optional.[String].Default: "decimal". Validation number type, options are ['decimal', 'int']. Handy when the validation type is number.
875+
// mantissa: 2, // Optional.[Number].Default: -1. Number precision.
873876
// showMsg: true, // Optional.[Bool].Default: true. To determin display the error message or not.
874877
// min: 2, // Optional.[Number].Default: 0. Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number.
875878
// max: 10, // Optional.[Number].Default: 0. Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number.

lib/components/Textbox.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ var getDefaultValidationOption = function getDefaultValidationOption(obj) {
9191
max = obj.max,
9292
type = obj.type,
9393
numberType = obj.numberType,
94+
mantissa = obj.mantissa,
9495
name = obj.name,
9596
check = obj.check,
9697
length = obj.length,
@@ -108,6 +109,7 @@ var getDefaultValidationOption = function getDefaultValidationOption(obj) {
108109
max = typeof max !== 'undefined' ? max : 0;
109110
type = typeof type !== 'undefined' ? type : 'string';
110111
numberType = typeof numberType !== 'undefined' ? numberType : 'string';
112+
mantissa = typeof mantissa !== 'undefined' ? mantissa : -1;
111113
name = typeof name !== 'undefined' ? name : '';
112114
check = typeof check !== 'undefined' ? check : true;
113115
showMsg = typeof showMsg !== 'undefined' ? showMsg : true;
@@ -124,6 +126,7 @@ var getDefaultValidationOption = function getDefaultValidationOption(obj) {
124126
max: max,
125127
type: type,
126128
numberType: numberType,
129+
mantissa: mantissa,
127130
name: name,
128131
check: check,
129132
length: length,
@@ -155,7 +158,7 @@ var getDefaultAsyncObj = function getDefaultAsyncObj(obj) {
155158
};
156159
};
157160

158-
var autoFormatNumber = function autoFormatNumber(v, numberType) {
161+
var autoFormatNumber = function autoFormatNumber(v, numberType, mantissa) {
159162
var DOT = '.';
160163
var res = '';
161164
var hasDot = false;
@@ -175,6 +178,17 @@ var autoFormatNumber = function autoFormatNumber(v, numberType) {
175178
}
176179
});
177180

181+
if (hasDot && mantissa >= 0) {
182+
var resArr = res.split('.');
183+
184+
if (mantissa === 0) {
185+
res = resArr[0];
186+
} else {
187+
resArr[1] = resArr[1].slice(0, mantissa);
188+
res = resArr.join('.');
189+
}
190+
}
191+
178192
if (numberType === VALIDATE_NUMBER_TYPE_LIST[0]) {
179193
if (res.length && res[0] === DOT) {
180194
res = "0".concat(res);
@@ -293,10 +307,11 @@ var component = function component(_ref) {
293307
}
294308

295309
var type = option.type,
296-
numberType = option.numberType;
310+
numberType = option.numberType,
311+
mantissa = option.mantissa;
297312

298313
if (type === VALIDATE_OPTION_TYPE_LIST[1]) {
299-
v = String(autoFormatNumber(v, VALIDATE_NUMBER_TYPE_LIST.indexOf(numberType) >= 0 ? numberType : VALIDATE_NUMBER_TYPE_LIST[0]));
314+
v = String(autoFormatNumber(v, VALIDATE_NUMBER_TYPE_LIST.indexOf(numberType) >= 0 ? numberType : VALIDATE_NUMBER_TYPE_LIST[0], mantissa));
300315
}
301316

302317
if (type === VALIDATE_OPTION_TYPE_LIST[2]) {

lib/react-inputs-validation.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/react-inputs-validation.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/react-inputs-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/react-inputs-validation.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-inputs-validation",
3-
"version": "4.0.4",
3+
"version": "4.1.0",
44
"description": "React form input validation components",
55
"main": "index.js",
66
"repository": {

src/js/Inputs/Textbox.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface DefaultValidationOption {
1515
max?: number;
1616
type?: string;
1717
numberType?: string;
18+
mantissa?: number;
1819
name?: string;
1920
check?: boolean;
2021
showMsg?: boolean;
@@ -27,13 +28,14 @@ interface DefaultValidationOption {
2728
customFunc?: Function | undefined;
2829
}
2930
const getDefaultValidationOption = (obj: DefaultValidationOption) => {
30-
let { reg, min, max, type, numberType, name, check, length, regMsg, compare, required, showMsg, locale, msgOnError, msgOnSuccess, customFunc } = obj;
31+
let { reg, min, max, type, numberType, mantissa, name, check, length, regMsg, compare, required, showMsg, locale, msgOnError, msgOnSuccess, customFunc } = obj;
3132
locale = typeof locale !== 'undefined' ? locale : DEFAULT_LOCALE;
3233
reg = typeof reg !== 'undefined' ? reg : '';
3334
min = typeof min !== 'undefined' ? min : 0;
3435
max = typeof max !== 'undefined' ? max : 0;
3536
type = typeof type !== 'undefined' ? type : 'string';
3637
numberType = typeof numberType !== 'undefined' ? numberType : 'string';
38+
mantissa = typeof mantissa !== 'undefined' ? mantissa : -1;
3739
name = typeof name !== 'undefined' ? name : '';
3840
check = typeof check !== 'undefined' ? check : true;
3941
showMsg = typeof showMsg !== 'undefined' ? showMsg : true;
@@ -50,6 +52,7 @@ const getDefaultValidationOption = (obj: DefaultValidationOption) => {
5052
max,
5153
type,
5254
numberType,
55+
mantissa,
5356
name,
5457
check,
5558
length,
@@ -106,7 +109,7 @@ interface Props {
106109
onKeyUp?: (e: React.KeyboardEvent<HTMLElement>) => void;
107110
validationCallback?: (res: boolean) => void;
108111
}
109-
const autoFormatNumber = (v: number | string, numberType: string) => {
112+
const autoFormatNumber = (v: number | string, numberType: string, mantissa: number) => {
110113
const DOT = '.';
111114
let res = '';
112115
let hasDot = false;
@@ -124,6 +127,15 @@ const autoFormatNumber = (v: number | string, numberType: string) => {
124127
res += i;
125128
}
126129
});
130+
if (hasDot && mantissa >= 0) {
131+
const resArr = res.split('.');
132+
if (mantissa === 0) {
133+
res = resArr[0];
134+
} else {
135+
resArr[1] = resArr[1].slice(0, mantissa);
136+
res = resArr.join('.');
137+
}
138+
}
127139
if (numberType === VALIDATE_NUMBER_TYPE_LIST[0]) {
128140
if (res.length && res[0] === DOT) {
129141
res = `0${res}`;
@@ -206,9 +218,9 @@ const component: React.FC<Props> = ({
206218
}
207219
}
208220
}
209-
const { type, numberType } = option;
221+
const { type, numberType, mantissa } = option;
210222
if (type === VALIDATE_OPTION_TYPE_LIST[1]) {
211-
v = String(autoFormatNumber(v, VALIDATE_NUMBER_TYPE_LIST.indexOf(numberType) >= 0 ? numberType : VALIDATE_NUMBER_TYPE_LIST[0]));
223+
v = String(autoFormatNumber(v, VALIDATE_NUMBER_TYPE_LIST.indexOf(numberType) >= 0 ? numberType : VALIDATE_NUMBER_TYPE_LIST[0], mantissa));
212224
}
213225
if (type === VALIDATE_OPTION_TYPE_LIST[2]) {
214226
v = utils.getAlphanumeric(v);

0 commit comments

Comments
 (0)