Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,14 @@ export function useNumberFieldState(
}

clampedValue = numberParser.parse(format(clampedValue));
let shouldValidate = clampedValue !== numberValue;
setNumberValue(clampedValue);

// in a controlled state, the numberValue won't change, so we won't go back to our old input without help
setInputValue(format(value === undefined ? clampedValue : numberValue));
validation.commitValidation();
if (shouldValidate) {
validation.commitValidation();
}
};

let safeNextStep = (operation: '+' | '-', minMax: number = 0) => {
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.');
});
});
Loading