Skip to content

Commit c4bd19d

Browse files
fix: dayperiod segment no longer resets when hour deleted (#7506)
* fix: dayperiod segment no longer reset * undo prettier * add test --------- Co-authored-by: Yihui Liao <[email protected]>
1 parent fd7075c commit c4bd19d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/@react-spectrum/datepicker/test/TimeField.test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import {act, fireEvent, pointerMap, render as render_, within} from '@react-spectrum/test-utils-internal';
1414
import {Button} from '@react-spectrum/button';
1515
import {Form} from '@react-spectrum/form';
16-
import {parseZonedDateTime, Time} from '@internationalized/date';
16+
import {parseTime, parseZonedDateTime, Time} from '@internationalized/date';
1717
import {Provider} from '@react-spectrum/provider';
1818
import React from 'react';
1919
import {theme} from '@react-spectrum/theme-default';
@@ -167,6 +167,23 @@ describe('TimeField', function () {
167167
expect(onFocusSpy).toHaveBeenCalledTimes(1);
168168
});
169169

170+
it('should keep dayPeriod the same when hour segment that has a value >= 12 is cleared', async function () {
171+
let {getAllByRole} = render(<TimeField label="Time" defaultValue={parseTime('20:24')} />);
172+
let segments = getAllByRole('spinbutton');
173+
174+
await user.tab();
175+
expect(segments[0]).toHaveFocus();
176+
expect(segments[0]).toHaveAttribute('aria-valuetext', '8 PM');
177+
expect(segments[2].getAttribute('aria-label')).toBe('AM/PM, ');
178+
expect(within(segments[2]).getByText('PM')).toBeInTheDocument();
179+
180+
fireEvent.keyDown(document.activeElement, {key: 'Backspace'});
181+
fireEvent.keyUp(document.activeElement, {key: 'Backspace'});
182+
expect(segments[0]).toHaveAttribute('aria-valuetext', 'Empty');
183+
expect(segments[2].getAttribute('aria-label')).toBe('AM/PM, ');
184+
expect(within(segments[2]).getByText('PM')).toBeInTheDocument();
185+
});
186+
170187
it('should trigger right arrow key event for segment navigation', async function () {
171188
let {getAllByRole} = render(<TimeField label="Time" onKeyDown={onKeyDownSpy} onKeyUp={onKeyUpSpy} />);
172189
let segments = getAllByRole('spinbutton');

packages/@react-stately/datepicker/src/useDateFieldState.ts

+2
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
399399
} else if (!isPM && shouldBePM) {
400400
value = displayValue.set({hour: displayValue.hour + 12});
401401
}
402+
} else if (part === 'hour' && 'hour' in displayValue && displayValue.hour >= 12 && validSegments.dayPeriod) {
403+
value = displayValue.set({hour: placeholder['hour'] + 12});
402404
} else if (part in displayValue) {
403405
value = displayValue.set({[part]: placeholder[part]});
404406
}

0 commit comments

Comments
 (0)