mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(Stepper): incorrect value when format minus value (#6238)
This commit is contained in:
parent
3e231f1ab5
commit
e61aa29118
@ -1,6 +1,6 @@
|
||||
// Utils
|
||||
import { formatNumber } from './utils';
|
||||
import { preventDefault } from '../utils/dom/event';
|
||||
import { formatNumber } from '../utils/format/number';
|
||||
import { resetScroll } from '../utils/dom/reset-scroll';
|
||||
import {
|
||||
createNamespace,
|
||||
|
@ -1,16 +0,0 @@
|
||||
export function formatNumber(value: string, allowDot: boolean) {
|
||||
if (allowDot) {
|
||||
const dotIndex = value.indexOf('.');
|
||||
|
||||
if (dotIndex > -1) {
|
||||
value =
|
||||
value.slice(0, dotIndex + 1) + value.slice(dotIndex).replace(/\./g, '');
|
||||
}
|
||||
} else {
|
||||
value = value.split('.')[0];
|
||||
}
|
||||
|
||||
const regExp = allowDot ? /[^0-9.]/g : /\D/g;
|
||||
|
||||
return value.replace(regExp, '');
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
import { createNamespace, isDef, addUnit } from '../utils';
|
||||
import { resetScroll } from '../utils/dom/reset-scroll';
|
||||
import { preventDefault } from '../utils/dom/event';
|
||||
import { formatNumber } from '../utils/format/number';
|
||||
import { FieldMixin } from '../mixins/field';
|
||||
import { formatNumber } from '../field/utils';
|
||||
|
||||
const [createComponent, bem] = createNamespace('stepper');
|
||||
|
||||
|
@ -1,3 +1,27 @@
|
||||
export function range(num: number, min: number, max: number): number {
|
||||
return Math.min(Math.max(num, min), max);
|
||||
}
|
||||
|
||||
function trimExtraChar(value: string, char: string, regExp: RegExp) {
|
||||
const index = value.indexOf(char);
|
||||
|
||||
if (index > -1) {
|
||||
return value.slice(0, index + 1) + value.slice(index).replace(regExp, '');
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function formatNumber(value: string, allowDot?: boolean) {
|
||||
if (allowDot) {
|
||||
value = trimExtraChar(value, '.', /\./g);
|
||||
} else {
|
||||
value = value.split('.')[0];
|
||||
}
|
||||
|
||||
value = trimExtraChar(value, '-', /-/g);
|
||||
|
||||
const regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g;
|
||||
|
||||
return value.replace(regExp, '');
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import { isMobile } from '../validate/mobile';
|
||||
import { isNumeric } from '../validate/number';
|
||||
import { isAndroid } from '../validate/system';
|
||||
import { camelize } from '../format/string';
|
||||
import { formatNumber } from '../format/number';
|
||||
|
||||
test('deepClone', () => {
|
||||
const a = { foo: 0 };
|
||||
@ -95,3 +96,21 @@ test('isNumeric', () => {
|
||||
expect(isNumeric('abc')).toBeFalsy();
|
||||
expect(isNumeric('1b2')).toBeFalsy();
|
||||
});
|
||||
|
||||
test('formatNumber', () => {
|
||||
expect(formatNumber('abc')).toEqual('');
|
||||
expect(formatNumber('1.2')).toEqual('1');
|
||||
expect(formatNumber('abc1.2')).toEqual('1');
|
||||
expect(formatNumber('123.4.')).toEqual('123');
|
||||
|
||||
// with dot
|
||||
expect(formatNumber('abc', true)).toEqual('');
|
||||
expect(formatNumber('1.2', true)).toEqual('1.2');
|
||||
expect(formatNumber('abc1.2', true)).toEqual('1.2');
|
||||
expect(formatNumber('123.4.', true)).toEqual('123.4');
|
||||
|
||||
// minus
|
||||
expect(formatNumber('-1.2')).toEqual('-1');
|
||||
expect(formatNumber('-1.2', true)).toEqual('-1.2');
|
||||
expect(formatNumber('-1.2-', true)).toEqual('-1.2');
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user