Skip to content

Commit 915d68a

Browse files
authored
Skip eager number updates that cause field rewrites (#3373)
When we resolved the number field committing in #3282 to not be so lazy, it became not lazy enough! In particular, min and max values would be applied while a user was still typing, which was far too intrusive. The solution is to check if react-aria would cause any changes to the input value on commit, and only force a commit when it's a true no-op on the input string. Resolves #3372
1 parent fb92992 commit 915d68a

3 files changed

Lines changed: 56 additions & 32 deletions

File tree

‎app/ui/lib/NumberInput.browser.spec.tsx‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,58 @@ test('fires onChange with NaN when the input is cleared', async () => {
5757
await expect.element(screen.getByText('Changes: NaN')).toBeVisible()
5858
})
5959

60-
test('clamps typed values above maxValue', async () => {
60+
test('clamps typed values above maxValue on blur', async () => {
6161
const screen = await render(<NumberInputHarness value={5} maxValue={100} />)
6262
const input = screen.getByRole('textbox', { name: 'Test number' })
6363

64+
// out-of-range values are left alone while editing, not clamped mid-keystroke
6465
await input.fill('150')
66+
await expect.element(input).toHaveValue('150')
6567

68+
await userEvent.tab()
6669
await expect.element(screen.getByText('Changes: 100')).toBeVisible()
6770
await expect.element(input).toHaveValue('100')
6871
})
6972

70-
test('clamps typed values below minValue', async () => {
73+
test('clamps typed values below minValue on blur', async () => {
7174
const screen = await render(<NumberInputHarness minValue={1} value={5} />)
7275
const input = screen.getByRole('textbox', { name: 'Test number' })
7376

7477
await input.fill('0')
78+
await expect.element(input).toHaveValue('0')
7579

80+
await userEvent.tab()
7681
await expect.element(screen.getByText('Changes: 1')).toBeVisible()
7782
await expect.element(input).toHaveValue('1')
7883
})
7984

85+
test('does not clamp intermediate input while typing', async () => {
86+
const screen = await render(<NumberInputHarness minValue={10} />)
87+
const input = screen.getByRole('textbox', { name: 'Test number' })
88+
89+
await input.click()
90+
// if clamping happened mid-typing, this would be 10 after hitting 2, then 100 after hitting 0
91+
await userEvent.type(input, '20')
92+
await expect.element(input).toHaveValue('20')
93+
94+
await userEvent.tab()
95+
await expect.element(input).toHaveValue('20')
96+
})
97+
98+
test('does not step intermediate input while typing', async () => {
99+
const screen = await render(<NumberInputHarness step={1} minValue={10} />)
100+
const input = screen.getByRole('textbox', { name: 'Test number' })
101+
102+
await input.click()
103+
await userEvent.type(input, '20')
104+
await expect.element(input).toHaveValue('20')
105+
await userEvent.type(input, '.1')
106+
await expect.element(input).toHaveValue('20.1')
107+
108+
await userEvent.tab()
109+
await expect.element(input).toHaveValue('20')
110+
})
111+
80112
test('only simplifies numbers on blur', async () => {
81113
const screen = await render(<NumberInputHarness />)
82114
const input = screen.getByRole('textbox', { name: 'Test number' })
@@ -102,8 +134,10 @@ test('still controls the displayed value when onChange causes no re-render', asy
102134
const input = screen.getByRole('textbox', { name: 'Test number' })
103135

104136
await input.fill('1099')
137+
await userEvent.tab()
105138
await expect.element(input).toHaveValue('1023')
106139

107140
await input.fill('10239')
141+
await userEvent.tab()
108142
await expect.element(input).toHaveValue('1023')
109143
})

‎app/ui/lib/NumberInput.spec.tsx‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎app/ui/lib/NumberInput.tsx‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
useButton,
1212
useLocale,
1313
useNumberField,
14+
useNumberFormatter,
1415
type AriaButtonProps,
1516
type AriaNumberFieldProps,
1617
} from 'react-aria'
@@ -23,8 +24,6 @@ type NumberInputProps = AriaNumberFieldProps & {
2324
ref?: Ref<HTMLInputElement>
2425
}
2526

26-
export const isCanonicalNumberString = (value: string) => String(Number(value)) === value
27-
2827
export function NumberInput(props: NumberInputProps) {
2928
const { locale } = useLocale()
3029
const state = useNumberFieldState({ ...props, locale })
@@ -36,14 +35,30 @@ export function NumberInput(props: NumberInputProps) {
3635
// react-aria only fires props.onChange on commit (blur / Enter / stepper),
3736
// but we want form state to update as soon as it would produce a different
3837
// field value. Committing whenever state.inputValue changes to an
39-
// unambiguous number lets react-aria keep controlling parsing, clamping
40-
// etc., but forces it to be more eager.
38+
// unambiguous number lets react-aria keep control, but forces it to be more
39+
// eager.
4140
//
4241
// Context: https://github.com/adobe/react-spectrum/issues/7984
42+
//
43+
// HOWEVER! This eagerness should only apply if it doesn't get in the user's
44+
// way, and so we check whether the bounds/stepping/formatting would rewrite
45+
// the field post-commit before going ahead.
46+
//
47+
// Context: https://github.com/adobe/react-spectrum/blob/4682b6d6eee6f2d5d0b63c5e340c21a27f4d5ec1/packages/react-stately/src/numberfield/useNumberFieldState.ts#L88
48+
const formatter = useNumberFormatter(props.formatOptions)
4349
useEffect(() => {
44-
if (isCanonicalNumberString(state.inputValue) || state.inputValue === '') {
50+
if (state.inputValue === '') {
4551
state.commit()
52+
return
4653
}
54+
55+
const { numberValue } = state
56+
if (Number.isNaN(numberValue)) return
57+
if (state.minValue != null && numberValue < state.minValue) return
58+
if (state.maxValue != null && numberValue > state.maxValue) return
59+
if (props.step && (numberValue - (state.minValue ?? 0)) % props.step !== 0) return
60+
if (formatter.format(numberValue) !== state.inputValue) return
61+
state.commit()
4762
// eslint-disable-next-line exhaustive-deps
4863
}, [state.inputValue])
4964

0 commit comments

Comments
 (0)