Skip to content

Commit 4b4bfa5

Browse files
authored
feat(VNumberInput): do not clamp value on mounted (#21826)
1 parent 707f458 commit 4b4bfa5

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/vuetify/src/components/VNumberInput/VNumberInput.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useLocale } from '@/composables/locale'
1515
import { useProxiedModel } from '@/composables/proxiedModel'
1616

1717
// Utilities
18-
import { computed, nextTick, onMounted, ref, shallowRef, toRef, watch, watchEffect } from 'vue'
18+
import { computed, nextTick, ref, shallowRef, toRef, watch, watchEffect } from 'vue'
1919
import { clamp, escapeForRegex, extractNumber, genericComponent, omit, propsFactory, useRender } from '@/util'
2020

2121
// Types
@@ -200,10 +200,6 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({
200200
watch(() => props.precision, () => formatInputValue())
201201
watch(() => props.minFractionDigits, () => formatInputValue())
202202

203-
onMounted(() => {
204-
clampModel()
205-
})
206-
207203
function inferPrecision (value: number | null) {
208204
if (value == null) return 0
209205
const str = value.toString()

packages/vuetify/src/components/VNumberInput/__tests__/VNumberInput.spec.browser.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,52 @@ describe('VNumberInput', () => {
157157
})
158158

159159
describe('native number input quirks', () => {
160-
it('should not bypass min', async () => {
160+
it('should auto-clamp after interaction', async () => {
161161
const model = ref(1)
162162
render(() =>
163163
<VNumberInput min={ 5 } max={ 15 } v-model={ model.value } />
164164
)
165165

166+
await expect.element(screen.getByCSS('input')).toHaveValue('1')
167+
expect(model.value).toBe(1)
168+
169+
await userEvent.click(screen.getByCSS('input'))
170+
await userEvent.tab()
171+
166172
await expect.element(screen.getByCSS('input')).toHaveValue('5')
167173
expect(model.value).toBe(5)
168174
})
169175

170-
it('should not bypass max', async () => {
176+
it('should apply increments within the range', async () => {
171177
const model = ref(20)
172178
render(() =>
173179
<VNumberInput min={ 5 } max={ 15 } v-model={ model.value } />
174180
)
175181

176-
await expect.element(screen.getByCSS('input')).toHaveValue('15')
177-
expect(model.value).toBe(15)
182+
await expect.element(screen.getByCSS('input')).toHaveValue('20')
183+
expect(model.value).toBe(20)
184+
185+
await userEvent.click(screen.getByCSS('input'))
186+
await userEvent.keyboard('{arrowDown}')
187+
188+
await expect.element(screen.getByCSS('input')).toHaveValue('14')
189+
expect(model.value).toBe(14)
190+
})
191+
192+
it('should auto-correct when incrementing against the limit', async () => {
193+
const model = ref(-33)
194+
render(() =>
195+
<VNumberInput min={ -10 } max={ 20 } v-model={ model.value } precision={ 2 } step={ 0.5 } />
196+
)
197+
198+
await expect.element(screen.getByCSS('input')).toHaveValue('-33.00')
199+
expect(model.value).toBe(-33)
200+
201+
await userEvent.click(screen.getByCSS('input'))
202+
await userEvent.keyboard('{arrowDown}')
203+
204+
await expect.element(screen.getByCSS('input')).toHaveValue('-10')
205+
expect(model.value).toBe(-10)
178206
})
179207

180208
it('supports decimal step', async () => {

0 commit comments

Comments
 (0)