fix(Field): disallow minus when using digit type (#7114)

This commit is contained in:
neverland
2020-09-05 14:54:05 +08:00
committed by GitHub
parent b62c0e32cf
commit e7deea7195
3 changed files with 26 additions and 17 deletions
+13 -5
View File
@@ -4,26 +4,34 @@ export function range(num: number, min: number, max: number): number {
function trimExtraChar(value: string, char: string, regExp: RegExp) {
const index = value.indexOf(char);
if (index === -1) {
return value;
return value;
}
if (char === '-' && index !== 0) {
return value.slice(0, index);
return value.slice(0, index);
}
return value.slice(0, index + 1) + value.slice(index).replace(regExp, '');
}
export function formatNumber(value: string, allowDot?: boolean) {
export function formatNumber(
value: string,
allowDot = true,
allowMinus = true
) {
if (allowDot) {
value = trimExtraChar(value, '.', /\./g);
} else {
value = value.split('.')[0];
}
value = trimExtraChar(value, '-', /-/g);
if (allowMinus) {
value = trimExtraChar(value, '-', /-/g);
} else {
value = value.replace(/-/, '');
}
const regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g;