Skip to content

Commit 20c24d3

Browse files
committed
fix: correctly format date conversion to a string for native date input
1 parent bd354fa commit 20c24d3

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ export function useHiddenDateInput(props: HiddenDateInputProps, state: DateField
6666
inputStep = 3600;
6767
}
6868

69-
let dateValue = state.value == null ? '' : state.value.toString();
69+
let dateValue = '';
70+
if (state.value != null && 'toAbsoluteString' in state.value) {
71+
dateValue = state.value.toAbsoluteString().replace('Z', '');
72+
} else if (state.value != null) {
73+
dateValue = state.value.toString();
74+
}
7075

7176
let inputType = state.granularity === 'day' ? 'date' : 'datetime-local';
7277

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)