-
Notifications
You must be signed in to change notification settings - Fork 22
Fire off number updates eagerly #3282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fakemonster
wants to merge
1
commit into
main
Choose a base branch
from
fix/eager-number-input
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import { fireEvent, render } from '@testing-library/react' | ||
| import { useState } from 'react' | ||
| import * as R from 'remeda' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { NumberInput } from './NumberInput' | ||
|
|
||
| type Props = React.ComponentProps<typeof NumberInput> | ||
|
|
||
| function Controlled({ onChange, ...props }: Props) { | ||
| const [value, setValue] = useState<number>(props.value ?? NaN) | ||
| return ( | ||
| <NumberInput | ||
| aria-label="test" | ||
| formatOptions={{ useGrouping: false }} | ||
| {...props} | ||
| value={value} | ||
| onChange={(v) => { | ||
| setValue(v) | ||
| onChange?.(v) | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| const getInput = (container: HTMLElement) => container.querySelector('input')! | ||
|
|
||
| describe('NumberInput', () => { | ||
| it('fires onChange per keystroke with the parsed number', () => { | ||
| const onChange = vi.fn() | ||
| const { container } = render(<Controlled onChange={onChange} />) | ||
| const input = getInput(container) | ||
|
|
||
| fireEvent.change(input, { target: { value: '1' } }) | ||
| expect(onChange).toHaveBeenLastCalledWith(1) | ||
|
|
||
| fireEvent.change(input, { target: { value: '12' } }) | ||
| expect(onChange).toHaveBeenLastCalledWith(12) | ||
| }) | ||
|
|
||
| it('fires onChange with NaN when the input is cleared', () => { | ||
| const onChange = vi.fn() | ||
| const { container } = render(<Controlled value={1} onChange={onChange} />) | ||
| const input = getInput(container) | ||
|
|
||
| fireEvent.change(input, { target: { value: '' } }) | ||
| expect(onChange).toHaveBeenLastCalledWith(NaN) | ||
| }) | ||
|
|
||
| it('clamps typed values above maxValue', () => { | ||
| const onChange = vi.fn() | ||
| const { container } = render( | ||
| <Controlled value={5} maxValue={100} onChange={onChange} /> | ||
| ) | ||
| const input = getInput(container) | ||
|
|
||
| fireEvent.change(input, { target: { value: '150' } }) | ||
| expect(onChange).toHaveBeenLastCalledWith(100) | ||
| expect(input.value).toBe('100') | ||
| }) | ||
|
|
||
| it('clamps typed values below minValue', () => { | ||
| const onChange = vi.fn() | ||
| const { container } = render(<Controlled minValue={1} value={5} onChange={onChange} />) | ||
| const input = getInput(container) | ||
|
|
||
| fireEvent.change(input, { target: { value: '0' } }) | ||
| expect(onChange).toHaveBeenLastCalledWith(1) | ||
| expect(input.value).toBe('1') | ||
| }) | ||
|
|
||
| it('only simplifies numbers on blur', () => { | ||
| const onChange = vi.fn() | ||
| const { container } = render(<Controlled onChange={onChange} />) | ||
| const input = getInput(container) | ||
|
|
||
| fireEvent.change(input, { target: { value: '0' } }) | ||
| expect(onChange).toHaveBeenCalledTimes(1) | ||
| expect(onChange).toHaveBeenLastCalledWith(0) | ||
| expect(input.value).toBe('0') | ||
|
|
||
| R.times(6, (precision) => { | ||
| const value = `1.${'0'.repeat(precision)}` // 1., 1.0, etc. | ||
| fireEvent.change(input, { target: { value } }) | ||
| expect(onChange).toHaveBeenCalledTimes(1) | ||
| expect(input.value).toBe(value) | ||
| }) | ||
|
|
||
| fireEvent.blur(input) | ||
| expect(onChange).toHaveBeenCalledTimes(2) | ||
| expect(onChange).toHaveBeenLastCalledWith(1) | ||
| expect(input.value).toBe('1') | ||
| }) | ||
|
|
||
| it('still controls the displayed value when onChange causes no re-render', () => { | ||
| const onChange = vi.fn() | ||
| const { container } = render(<Controlled maxValue={1023} onChange={onChange} />) | ||
| const input = getInput(container) | ||
|
|
||
| fireEvent.change(input, { target: { value: '1099' } }) | ||
| expect(onChange).toHaveBeenLastCalledWith(1023) | ||
| expect(input.value).toBe('1023') | ||
|
|
||
| fireEvent.change(input, { target: { value: '10239' } }) | ||
| expect(onChange).toHaveBeenLastCalledWith(1023) | ||
| expect(input.value).toBe('1023') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| * Copyright Oxide Computer Company | ||
| */ | ||
| import cn from 'classnames' | ||
| import { useRef, type Ref } from 'react' | ||
| import { useEffect, useRef, type Ref } from 'react' | ||
| import { | ||
| useButton, | ||
| useLocale, | ||
|
|
@@ -31,6 +31,20 @@ export function NumberInput(props: NumberInputProps) { | |
| const { groupProps, inputProps, incrementButtonProps, decrementButtonProps } = | ||
| useNumberField(props, state, inputRef) | ||
|
|
||
| // react-aria only fires props.onChange on commit (blur / Enter / stepper), | ||
| // but we want form state to update as soon as it would produce a different | ||
| // field value. Committing whenever state.inputValue changes to an | ||
| // unambiguous number lets react-aria keep controlling parsing, clamping | ||
| // etc., but forces it to be more eager. | ||
| // | ||
| // Context: https://github.com/adobe/react-spectrum/issues/7984 | ||
| useEffect(() => { | ||
| if (String(Number(state.inputValue)) === state.inputValue || state.inputValue === '') { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this in a function and unit test it. |
||
| state.commit() | ||
| } | ||
| // eslint-disable-next-line exhaustive-deps | ||
| }, [state.inputValue]) | ||
|
|
||
| return ( | ||
| <div | ||
| className={cn( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very interesting, I like it