fix(payment): restore accepted text after invalid amount input

This commit is contained in:
chihao.ou
2026-09-16 18:17:14 +08:00
parent 881f320269
commit 98321a054e
2 changed files with 43 additions and 2 deletions
@@ -88,8 +88,12 @@ function selectAmount(amt: number) {
}
function handleInput(e: Event) {
const val = (e.target as HTMLInputElement).value
if (!AMOUNT_PATTERN.test(val)) return
const input = e.target as HTMLInputElement
const val = input.value
if (!AMOUNT_PATTERN.test(val)) {
input.value = customText.value
return
}
customText.value = val
if (val === '') {
emit('update:modelValue', null)
@@ -0,0 +1,37 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { enableAutoUnmount, mount } from '@vue/test-utils'
import AmountInput from '../AmountInput.vue'
vi.mock('vue-i18n', () => ({ useI18n: () => ({ t: (key: string) => key }) }))
enableAutoUnmount(afterEach)
function mountInput(value: number | null = null) {
return mount(AmountInput, { props: { modelValue: value } })
}
describe('recharge amount input', () => {
it.each(['10abc', '10.555', '-10', '1e2'])('restores the accepted amount after rejecting %s', async (value) => {
const wrapper = mountInput(10)
const input = wrapper.get('input')
await input.setValue(value)
expect((input.element as HTMLInputElement).value).toBe('10')
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
})
it('restores the last typed amount rather than a stale prop', async () => {
const wrapper = mountInput()
const input = wrapper.get('input')
await input.setValue('12.50')
await input.setValue('12.500')
expect((input.element as HTMLInputElement).value).toBe('12.50')
expect(wrapper.emitted('update:modelValue')).toEqual([[12.5]])
})
it('preserves decimal editing and allows clearing the amount', async () => {
const wrapper = mountInput()
const input = wrapper.get('input')
for (const value of ['0', '0.', '0.5', '0.50', '']) await input.setValue(value)
expect(wrapper.emitted('update:modelValue')).toEqual([[null], [null], [0.5], [0.5], [null]])
expect((input.element as HTMLInputElement).value).toBe('')
})
})