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: dayperiod segment no longer resets when hour deleted #7506

Merged
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
19 changes: 18 additions & 1 deletion packages/@react-spectrum/datepicker/test/TimeField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import {act, fireEvent, pointerMap, render as render_, within} from '@react-spectrum/test-utils-internal';
import {Button} from '@react-spectrum/button';
import {Form} from '@react-spectrum/form';
import {parseZonedDateTime, Time} from '@internationalized/date';
import {parseTime, parseZonedDateTime, Time} from '@internationalized/date';
import {Provider} from '@react-spectrum/provider';
import React from 'react';
import {theme} from '@react-spectrum/theme-default';
Expand Down Expand Up @@ -167,6 +167,23 @@ describe('TimeField', function () {
expect(onFocusSpy).toHaveBeenCalledTimes(1);
});

it('should keep dayPeriod the same when hour segment that has a value >= 12 is cleared', async function () {
let {getAllByRole} = render(<TimeField label="Time" defaultValue={parseTime('20:24')} />);
let segments = getAllByRole('spinbutton');

await user.tab();
expect(segments[0]).toHaveFocus();
expect(segments[0]).toHaveAttribute('aria-valuetext', '8 PM');
expect(segments[2].getAttribute('aria-label')).toBe('AM/PM, ');
expect(within(segments[2]).getByText('PM')).toBeInTheDocument();

fireEvent.keyDown(document.activeElement, {key: 'Backspace'});
fireEvent.keyUp(document.activeElement, {key: 'Backspace'});
expect(segments[0]).toHaveAttribute('aria-valuetext', 'Empty');
expect(segments[2].getAttribute('aria-label')).toBe('AM/PM, ');
expect(within(segments[2]).getByText('PM')).toBeInTheDocument();
});

it('should trigger right arrow key event for segment navigation', async function () {
let {getAllByRole} = render(<TimeField label="Time" onKeyDown={onKeyDownSpy} onKeyUp={onKeyUpSpy} />);
let segments = getAllByRole('spinbutton');
Expand Down
2 changes: 2 additions & 0 deletions packages/@react-stately/datepicker/src/useDateFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
} else if (!isPM && shouldBePM) {
value = displayValue.set({hour: displayValue.hour + 12});
}
} else if (part === 'hour' && 'hour' in displayValue && displayValue.hour >= 12 && validSegments.dayPeriod) {
value = displayValue.set({hour: placeholder['hour'] + 12});
} else if (part in displayValue) {
value = displayValue.set({[part]: placeholder[part]});
}
Expand Down