diff --git a/src/field/utils.ts b/src/field/utils.ts index d37981cd0..d9fe3de4d 100644 --- a/src/field/utils.ts +++ b/src/field/utils.ts @@ -6,6 +6,8 @@ export function formatNumber(value: string, allowDot: boolean) { value = value.slice(0, dotIndex + 1) + value.slice(dotIndex).replace(/\./g, ''); } + } else { + value = value.split('.')[0]; } const regExp = allowDot ? /[^0-9.]/g : /\D/g; diff --git a/src/form/test/__snapshots__/demo.spec.js.snap b/src/form/test/__snapshots__/demo.spec.js.snap index a0f5a6bd8..f1a25ca54 100644 --- a/src/form/test/__snapshots__/demo.spec.js.snap +++ b/src/form/test/__snapshots__/demo.spec.js.snap @@ -120,7 +120,7 @@ exports[`renders demo correctly 1`] = `
-
+
diff --git a/src/stepper/index.js b/src/stepper/index.js index 5769d9201..12fa1ea48 100644 --- a/src/stepper/index.js +++ b/src/stepper/index.js @@ -2,6 +2,7 @@ import { createNamespace, isDef, addUnit } from '../utils'; import { resetScroll } from '../utils/dom/reset-scroll'; import { preventDefault } from '../utils/dom/event'; import { FieldMixin } from '../mixins/field'; +import { formatNumber } from '../field/utils'; const [createComponent, bem] = createNamespace('stepper'); @@ -142,19 +143,13 @@ export default createComponent({ } }, - // filter illegal characters - filter(value) { - value = String(value).replace(/[^0-9.-]/g, ''); - - if (this.integer && value.indexOf('.') !== -1) { - value = value.split('.')[0]; - } - - return value; + // formatNumber illegal characters + formatNumber(value) { + return formatNumber(String(value), !this.integer); }, format(value) { - value = this.filter(value); + value = this.formatNumber(value); // format range value = value === '' ? 0 : +value; @@ -171,12 +166,7 @@ export default createComponent({ onInput(event) { const { value } = event.target; - // allow input to be empty - if (value === '') { - return; - } - - let formatted = this.filter(value); + let formatted = this.formatNumber(value); // limit max decimal length if (isDef(this.decimalLength) && formatted.indexOf('.') !== -1) { @@ -290,15 +280,15 @@ export default createComponent({ {...createListeners('minus')} />
基础用法
-
+
步长设置
-
+
限制输入范围
-
+
@@ -29,31 +29,31 @@ exports[`renders demo correctly 1`] = `
禁用状态
-
+
禁用输入框
-
+
固定小数位数
-
+
自定义大小
-
+
异步变更
-
+
diff --git a/src/stepper/test/__snapshots__/index.spec.js.snap b/src/stepper/test/__snapshots__/index.spec.js.snap index eeebc24c6..74e6df5e0 100644 --- a/src/stepper/test/__snapshots__/index.spec.js.snap +++ b/src/stepper/test/__snapshots__/index.spec.js.snap @@ -1,11 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`button-size prop 1`] = `
`; +exports[`button-size prop 1`] = `
`; -exports[`disable stepper input 1`] = `
`; +exports[`disable stepper input 1`] = `
`; -exports[`disabled stepper 1`] = `
`; +exports[`disabled stepper 1`] = `
`; -exports[`input-width prop 1`] = `
`; +exports[`input-width prop 1`] = `
`; -exports[`show-plus & show-minus props 1`] = `
`; +exports[`show-plus & show-minus props 1`] = `
`; diff --git a/src/stepper/test/index.spec.js b/src/stepper/test/index.spec.js index 83a6d007b..b45bfdbd2 100644 --- a/src/stepper/test/index.spec.js +++ b/src/stepper/test/index.spec.js @@ -115,17 +115,17 @@ test('filter value during user input', () => { const input = wrapper.find('.van-stepper__input'); input.element.value = ''; input.trigger('input'); - expect(wrapper.emitted('input')).toBeFalsy(); + expect(wrapper.emitted('input')[0][0]).toEqual(''); input.element.value = 'a'; input.trigger('input'); expect(input.element.value).toEqual(''); - expect(wrapper.emitted('input')).toBeFalsy(); + expect(wrapper.emitted('input')[1]).toBeFalsy(); input.element.value = '2'; input.trigger('input'); expect(input.element.value).toEqual('2'); - expect(wrapper.emitted('input')[0][0]).toEqual('2'); + expect(wrapper.emitted('input')[1][0]).toEqual('2'); }); test('shoud watch value and format it', () => { @@ -179,9 +179,10 @@ test('stepper blur', () => { const input = wrapper.find('input'); input.element.value = ''; input.trigger('input'); - input.trigger('blur'); + expect(wrapper.emitted('input')[0][0]).toEqual(''); - expect(wrapper.emitted('input')[0][0]).toEqual(3); + input.trigger('blur'); + expect(wrapper.emitted('input')[1][0]).toEqual(3); expect(wrapper.emitted('blur')).toBeTruthy(); });