Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/@react-aria/numberfield/src/useNumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export function useNumberField(props: AriaNumberFieldProps, state: NumberFieldSt
let inputId = useId(id);
let {focusProps} = useFocus({
onBlur() {
commitAndAnnounce();
// Only commit if the input value has actually changed from the state's input value.
// This prevents validation from being reset when the user focuses and blurs without editing.
if (inputRef.current?.value !== inputValue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need to track the previous input value from the last commit, or increment/decrement instead of inputValue, since by the time the user blurs the inputValue will always be the inputRef's current value

commitAndAnnounce();
}
}
});

Expand Down
38 changes: 37 additions & 1 deletion packages/react-aria-components/test/NumberField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
jest.mock('@react-aria/live-announcer');
import {act, pointerMap, render} from '@react-spectrum/test-utils-internal';
import {announce} from '@react-aria/live-announcer';
import {Button, FieldError, Group, Input, Label, NumberField, NumberFieldContext, Text} from '../';
import {Button, FieldError, Form, Group, Input, Label, NumberField, NumberFieldContext, Text} from '../';
import React from 'react';
import userEvent from '@testing-library/user-event';

Expand Down Expand Up @@ -250,4 +250,40 @@ describe('NumberField', () => {
await user.keyboard('{Enter}');
expect(input).toHaveValue('200');
});

it('should not reset validation errors on blur when value has not changed', async () => {
let {getByRole} = render(
<Form validationErrors={{testNumber: 'This field has an error.'}}>
<NumberField name="testNumber" defaultValue={5}>
<Label>Test Number</Label>
<Group>
<Button slot="decrement">-</Button>
<Input />
<Button slot="increment">+</Button>
</Group>
<FieldError />
</NumberField>
</Form>
);

let input = getByRole('textbox');
let numberfield = input.closest('.react-aria-NumberField');

// Validation error should be displayed
expect(numberfield).toHaveAttribute('data-invalid');
expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby').split(' ')[0])).toHaveTextContent('This field has an error.');

// Focus the field without changing the value
act(() => { input.focus(); });
expect(numberfield).toHaveAttribute('data-invalid');

// Blur the field without changing the value
act(() => { input.blur(); });

// Validation error should still be displayed because the value didn't change
expect(numberfield).toHaveAttribute('data-invalid');
expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby').split(' ')[0])).toHaveTextContent('This field has an error.');
});
});