Skip to content

Commit 5670b2a

Browse files
authored
fix: correctly format date conversion to a string for native date input (#9076)
* fix: correctly format date conversion to a string for native date input * fix
1 parent ae28784 commit 5670b2a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

packages/react-aria-components/src/HiddenDateInput.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313

14-
import {CalendarDate, CalendarDateTime, parseDate, parseDateTime} from '@internationalized/date';
14+
import {CalendarDate, CalendarDateTime, parseDate, parseDateTime, toCalendarDate, toCalendarDateTime, toLocalTimeZone} from '@internationalized/date';
1515
import {DateFieldState, DatePickerState, DateSegmentType} from 'react-stately';
1616
import React, {ReactNode} from 'react';
1717
import {useVisuallyHidden} from 'react-aria';
@@ -65,8 +65,15 @@ export function useHiddenDateInput(props: HiddenDateInputProps, state: DateField
6565
} else if (state.granularity === 'hour') {
6666
inputStep = 3600;
6767
}
68-
69-
let dateValue = state.value == null ? '' : state.value.toString();
68+
69+
let dateValue = '';
70+
if (state.value) {
71+
if (state.granularity === 'day') {
72+
dateValue = toCalendarDate(state.value).toString();
73+
} else {
74+
dateValue = toCalendarDateTime('timeZone' in state.value ? toLocalTimeZone(state.value) : state.value).toString();
75+
}
76+
}
7077

7178
let inputType = state.granularity === 'day' ? 'date' : 'datetime-local';
7279

packages/react-aria-components/stories/DateField.stories.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ export const DateFieldAutoFill = (props) => (
9494
{...props}
9595
name="bday"
9696
autoComplete="bday"
97-
defaultValue={parseAbsoluteToLocal('2021-04-07T18:45:22Z')
98-
}
97+
defaultValue={parseAbsoluteToLocal('2021-04-07T18:45:22Z')}
9998
data-testid="date-field-example">
10099
<Label style={{display: 'block'}}>Date</Label>
101100
<DateInput className={styles.field} data-testid2="date-input">

packages/react-aria-components/stories/DatePicker.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {action} from '@storybook/addon-actions';
1414
import {Button, Calendar, CalendarCell, CalendarGrid, DateInput, DatePicker, DateRangePicker, DateSegment, Dialog, Form, Group, Heading, Input, Label, Popover, RangeCalendar, TextField} from 'react-aria-components';
1515
import clsx from 'clsx';
1616
import {Meta, StoryFn} from '@storybook/react';
17+
import {parseAbsoluteToLocal} from '@internationalized/date';
1718
import React from 'react';
1819
import styles from '../example/index.css';
1920
import './styles.css';
@@ -211,7 +212,7 @@ export const DatePickerAutofill = (props) => (
211212
<Label>Name</Label>
212213
<Input name="firstName" type="name" id="name" autoComplete="name" />
213214
</TextField>
214-
<DatePicker data-testid="date-picker-example" name="bday" autoComplete="bday" {...props}>
215+
<DatePicker data-testid="date-picker-example" name="bday" autoComplete="bday" defaultValue={parseAbsoluteToLocal('2021-04-07T18:45:22Z')} {...props}>
215216
<Label style={{display: 'block'}}>Date</Label>
216217
<Group style={{display: 'inline-flex'}}>
217218
<DateInput className={styles.field}>

0 commit comments

Comments
 (0)