Skip to content

Commit ff456f3

Browse files
authored
fix: ensure that clearing a partially entered date doesnt autofill the datefield (#8549)
1 parent 1a2cd52 commit ff456f3

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
264264
setPlaceholderDate(createPlaceholderDate(props.placeholderValue, granularity, calendar, defaultTimeZone));
265265
setValidSegments({});
266266
} else if (
267-
validKeys.length === 0 ||
268-
validKeys.length >= allKeys.length ||
267+
(validKeys.length === 0 && clearedSegment.current == null) ||
268+
validKeys.length >= allKeys.length ||
269269
(validKeys.length === allKeys.length - 1 && allSegments.dayPeriod && !validSegments.dayPeriod && clearedSegment.current !== 'dayPeriod')
270270
) {
271271
// If the field was empty (no valid segments) or all segments are completed, commit the new value.
@@ -286,8 +286,8 @@ export function useDateFieldState<T extends DateValue = DateValue>(props: DateFi
286286
};
287287

288288
let dateValue = useMemo(() => displayValue.toDate(timeZone), [displayValue, timeZone]);
289-
let segments = useMemo(() =>
290-
processSegments(dateValue, validSegments, dateFormatter, resolvedOptions, displayValue, calendar, locale, granularity),
289+
let segments = useMemo(() =>
290+
processSegments(dateValue, validSegments, dateFormatter, resolvedOptions, displayValue, calendar, locale, granularity),
291291
[dateValue, validSegments, dateFormatter, resolvedOptions, displayValue, calendar, locale, granularity]);
292292

293293
// When the era field appears, mark it valid if the year field is already valid.
@@ -452,9 +452,9 @@ function processSegments(dateValue, validSegments, dateFormatter, resolvedOption
452452

453453
// There is an issue in RTL languages where time fields render (minute:hour) instead of (hour:minute).
454454
// To force an LTR direction on the time field since, we wrap the time segments in LRI (left-to-right) isolate unicode. See https://www.w3.org/International/questions/qa-bidi-unicode-controls.
455-
// These unicode characters will be added to the array of processed segments as literals and will mark the start and end of the embedded direction change.
455+
// These unicode characters will be added to the array of processed segments as literals and will mark the start and end of the embedded direction change.
456456
if (type === 'hour') {
457-
// This marks the start of the embedded direction change.
457+
// This marks the start of the embedded direction change.
458458
processedSegments.push({
459459
type: 'literal',
460460
text: '\u2066',
@@ -487,7 +487,7 @@ function processSegments(dateValue, validSegments, dateFormatter, resolvedOption
487487
isEditable: false
488488
});
489489
} else {
490-
// We only want to "wrap" the unicode around segments that are hour, minute, or second. If they aren't, just process as normal.
490+
// We only want to "wrap" the unicode around segments that are hour, minute, or second. If they aren't, just process as normal.
491491
processedSegments.push(dateSegment);
492492
}
493493
}

packages/react-aria-components/test/DateField.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,30 @@ describe('DateField', () => {
427427
let input = getByRole('group');
428428
expect(input).toHaveTextContent('5/30/2000');
429429
});
430+
431+
it('should reset to placeholders when deleting a partially filled DateField', async () => {
432+
let {getAllByRole} = render(
433+
<DateField>
434+
<Label>Date</Label>
435+
<DateInput>
436+
{segment => <DateSegment segment={segment} />}
437+
</DateInput>
438+
</DateField>
439+
);
440+
441+
let segements = getAllByRole('spinbutton');
442+
let monthSegment = segements[0];
443+
expect(monthSegment).toHaveTextContent('mm');
444+
await user.click(monthSegment);
445+
expect(monthSegment).toHaveFocus();
446+
await user.keyboard('11');
447+
expect(monthSegment).toHaveTextContent('11');
448+
449+
await user.click(monthSegment);
450+
await user.keyboard('{backspace}');
451+
await user.keyboard('{backspace}');
452+
expect(monthSegment).toHaveTextContent('mm');
453+
expect(segements[1]).toHaveTextContent('dd');
454+
expect(segements[2]).toHaveTextContent('yyyy');
455+
});
430456
});

0 commit comments

Comments
 (0)