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
35 changes: 35 additions & 0 deletions packages/react-aria-components/test/NumberField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jest.mock('react-aria/src/live-announcer/LiveAnnouncer');
import {act, fireEvent, pointerMap, render} from '@react-spectrum/test-utils-internal';
import {announce} from 'react-aria/private/live-announcer/LiveAnnouncer';
import {Button} from '../src/Button';
import {Calendar, CalendarCell, CalendarGrid} from '../src/Calendar';
import {CalendarDate} from '@internationalized/date';
import {FieldError} from '../src/FieldError';
import {Form} from '../src/Form';
import {Group} from '../src/Group';
Expand Down Expand Up @@ -404,6 +406,39 @@ describe('NumberField', () => {
expect(input).toHaveValue('$1.00', 'Ambiguous value should be parsed using the current locale');
});

it('commits and announces a corrected value when focus moves in an effect', async () => {
let onChange = jest.fn();
let {getByRole, rerender} = render(
<TestNumberField defaultValue={5} maxValue={10} onChange={onChange} />
);
let input = getByRole('textbox');
await user.click(input);
await user.clear(input);
await user.type(input, '20');

using consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
await act(async () => {
rerender(
<>
<TestNumberField defaultValue={5} maxValue={10} onChange={onChange} />
<Calendar
autoFocus
aria-label="Calendar"
defaultFocusedValue={new CalendarDate(2026, 4, 1)}>
<CalendarGrid>{date => <CalendarCell date={date} />}</CalendarGrid>
</Calendar>
</>
);
await Promise.resolve();
});
expect(input).toHaveValue('10');
expect(onChange).toHaveBeenLastCalledWith(10);
expect(announce).toHaveBeenCalledWith('10', 'assertive');
expect(consoleErrorSpy).not.toHaveBeenCalledWith(
expect.stringContaining('flushSync was called from inside a lifecycle method')
);
});

it('should support arabic singular and dual counts', async () => {
let onChange = jest.fn();
let {getByRole} = render(
Expand Down
35 changes: 25 additions & 10 deletions packages/react-aria/src/calendar/useCalendarCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,32 @@ export function useCalendarCell(
// Focus the button in the DOM when the state updates.
useEffect(() => {
if (isFocused && ref.current) {
focusWithoutScrolling(ref.current);
let element = ref.current;
let focus = () => {
if (ref.current !== element || !element.isConnected) {
return;
}

focusWithoutScrolling(element);

// Scroll into view if navigating with a keyboard, otherwise
// try not to shift the view under the user's mouse/finger.
// If in a overlay, scrollIntoViewport will only cause scrolling
// up to the overlay scroll body to prevent overlay shifting.
// Also only scroll into view if the cell actually got focused.
// There are some cases where the cell might be disabled or inside,
// an inert container and we don't want to scroll then.
if (getInteractionModality() !== 'pointer' && getActiveElement() === element) {
scrollIntoViewport(element, {containingElement: getScrollParent(element)});
}
};

// Scroll into view if navigating with a keyboard, otherwise
// try not to shift the view under the user's mouse/finger.
// If in a overlay, scrollIntoViewport will only cause scrolling
// up to the overlay scroll body to prevent overlay shifting.
// Also only scroll into view if the cell actually got focused.
// There are some cases where the cell might be disabled or inside,
// an inert container and we don't want to scroll then.
if (getInteractionModality() !== 'pointer' && getActiveElement() === ref.current) {
scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)});
// Focusing an input blurs it. Defer this when an input owns focus so its blur handler can
// safely use flushSync after the effect completes. Focus within the calendar remains sync.
if (getActiveElement()?.tagName === 'INPUT') {
queueMicrotask(focus);
} else {
focus();
}
}
}, [isFocused, ref]);
Expand Down