Skip to content
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

Fix backspace to shift focus to previous element in useDateSegment #5715

Merged
merged 10 commits into from
Feb 20, 2024
4 changes: 4 additions & 0 deletions packages/@react-aria/datepicker/src/useDateSegment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ export function useDateSegment(segment: DateSegment, state: DateFieldState, ref:
let parser = useMemo(() => new NumberParser(locale, {maximumFractionDigits: 0}), [locale]);

let backspace = () => {
if (segment.text === segment.placeholder) {
focusManager.focusPrevious();
}
if (parser.isValidPartialNumber(segment.text) && !state.isReadOnly && !segment.isPlaceholder) {
let newValue = segment.text.slice(0, -1);
let parsed = parser.parse(newValue);
newValue = parsed === 0 ? '' : newValue;
if (newValue.length === 0 || parsed === 0) {
state.clearSegment(segment.type);
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-aria-components/stories/DateField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import clsx from 'clsx';
import {DateField, DateInput, DateSegment, Label} from 'react-aria-components';
import {parseAbsoluteToLocal} from '@internationalized/date';
import React from 'react';
import styles from '../example/index.css';

Expand All @@ -20,7 +21,7 @@ export default {
};

export const DateFieldExample = () => (
<DateField data-testid="date-field-example">
<DateField data-testid="date-field-example" defaultValue={parseAbsoluteToLocal('2024-01-01T01:01:00Z')}>
<Label style={{display: 'block'}}>Date</Label>
<DateInput className={styles.field} data-testid2="date-input">
{segment => <DateSegment segment={segment} className={clsx(styles.segment, {[styles.placeholder]: segment.isPlaceholder})} />}
Expand Down
31 changes: 31 additions & 0 deletions packages/react-aria-components/test/DateField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,35 @@ describe('DateField', () => {
expect(getDescription()).not.toContain('Constraints not satisfied');
expect(group).not.toHaveAttribute('data-invalid');
});

it('should focus previous segment when backspacing on an empty date segment', async () => {
let {getAllByRole} = render(
<DateField defaultValue={new CalendarDate(2024, 12, 31)}>
<Label>Birth date</Label>
<DateInput>
{segment => <DateSegment segment={segment} />}
</DateInput>
</DateField>
);

let segments = getAllByRole('spinbutton');
await user.click(segments[2]);
expect(document.activeElement).toBe(segments[2]);

// Press backspace to delete '2024'
for (let i = 0; i < 4; i++) {
await user.keyboard('{backspace}');
}
expect(document.activeElement).toBe(segments[2]);
await user.keyboard('{backspace}');
expect(document.activeElement).toBe(segments[1]);

// Press backspace to delete '31'
for (let i = 0; i < 2; i++) {
await user.keyboard('{backspace}');
}
expect(document.activeElement).toBe(segments[1]);
await user.keyboard('{backspace}');
expect(document.activeElement).toBe(segments[0]);
});
});