Skip to content

Commit

Permalink
fix(NumberField): always updateModelValue on blur (#954)
Browse files Browse the repository at this point in the history
* fix(NumberField): always updateModelValue on blur

* chore(NumberField): add test
  • Loading branch information
epr3 committed May 25, 2024
1 parent c2f54bf commit 745fcef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 12 additions & 1 deletion packages/radix-vue/src/NumberField/NumberField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function setup(props?: NumberFieldRootProps) {
}

const kbd = useKbd()
describe('NumberField', () => {
describe('numberField', () => {
it('should pass axe accessibility tests', async () => {
const { root } = setup()
expect(await axe(root)).toHaveNoViolations()
Expand All @@ -42,6 +42,17 @@ describe('NumberField', () => {
expect(input.value).toBe('-10')
})

it('should restart from 0 when clearing the value', async () => {
const { input, increment } = setup({ defaultValue: 5 })

await userEvent.clear(input)

expect(input.value).toBe('')
await userEvent.click(document.body)
await userEvent.click(increment)
expect(input.value).toBe('0')
})

it('should increase and decrease based on default step', async () => {
const { input, increment, decrement } = setup({ defaultValue: 10 })
expect(input.value).toBe('10')
Expand Down
12 changes: 6 additions & 6 deletions packages/radix-vue/src/NumberField/NumberFieldRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ function clampInputValue(val: number) {
}
function applyInputValue(val: string) {
if (!val)
return
const parsedValue = numberParser.parse(val)
modelValue.value = clampInputValue(parsedValue)
// Set to empty state if input value is empty
if (!val.length)
return setInputValue(modelValue.value === undefined ? '' : textValue.value)
if (!val.length) {
return setInputValue(val)
}
// if it failed to parse, then reset input to formatted version of current number
if (isNaN(parsedValue))
return setInputValue(textValue.value)
modelValue.value = clampInputValue(parsedValue)
return setInputValue(textValue.value)
}
Expand Down

0 comments on commit 745fcef

Please sign in to comment.