Skip to content

Commit 8ed62c6

Browse files
author
Eric Olkowski
committed
fix(Timestamp): updated logic for rendering datetime attribute
1 parent 44af730 commit 8ed62c6

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/react-core/src/components/Timestamp/Timestamp.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import styles from '@patternfly/react-styles/css/components/Timestamp/timestamp';
33
import { css } from '@patternfly/react-styles';
44
import { Tooltip } from '../Tooltip';
5+
import { isValidDate } from '../CalendarMonth';
56

67
export enum TimestampFormat {
78
full = 'full',
@@ -78,7 +79,7 @@ export const Timestamp: React.FunctionComponent<TimestampProps> = ({
7879
children,
7980
className,
8081
customFormat,
81-
date: dateProp = new Date(),
82+
date: dateProp,
8283
dateFormat,
8384
displaySuffix = '',
8485
is12Hour,
@@ -87,22 +88,40 @@ export const Timestamp: React.FunctionComponent<TimestampProps> = ({
8788
tooltip,
8889
...props
8990
}: TimestampProps) => {
91+
const [date, setDate] = React.useState(() => {
92+
const initDate = new Date(dateProp);
93+
if (isValidDate(initDate)) {
94+
return initDate;
95+
}
96+
97+
return new Date();
98+
});
99+
100+
React.useEffect(() => {
101+
const dateFromProp = new Date(dateProp);
102+
if (isValidDate(dateFromProp) && dateFromProp.toString() !== new Date(date).toString()) {
103+
setDate(dateFromProp);
104+
} else if (!dateProp) {
105+
setDate(new Date());
106+
}
107+
}, [dateProp]);
108+
90109
const hasTimeFormat = timeFormat && !customFormat;
91110
const formatOptions = {
92111
...(dateFormat && !customFormat && { dateStyle: dateFormat }),
93112
...(customFormat && { ...customFormat }),
94113
...(is12Hour !== undefined && { hour12: is12Hour })
95114
};
96115

97-
const dateAsLocaleString = new Date(dateProp).toLocaleString(locale, {
116+
const dateAsLocaleString = new Date(date).toLocaleString(locale, {
98117
...formatOptions,
99118
...(hasTimeFormat && { timeStyle: timeFormat })
100119
});
101120
const defaultDisplay = `${dateAsLocaleString}${displaySuffix ? ' ' + displaySuffix : ''}`;
102121

103122
const utcTimeFormat = timeFormat !== 'short' ? 'medium' : 'short';
104123
const convertToUTCString = (date: Date) => new Date(date).toUTCString().slice(0, -3);
105-
const utcDateString = new Date(convertToUTCString(dateProp)).toLocaleString(locale, {
124+
const utcDateString = new Date(convertToUTCString(date)).toLocaleString(locale, {
106125
...formatOptions,
107126
...(hasTimeFormat && { timeStyle: utcTimeFormat })
108127
});
@@ -114,7 +133,7 @@ export const Timestamp: React.FunctionComponent<TimestampProps> = ({
114133
{...(tooltip && { tabIndex: 0 })}
115134
{...props}
116135
>
117-
<time className="pf-c-timestamp__text" dateTime={props.dateTime || new Date(dateProp).toISOString()}>
136+
<time className="pf-c-timestamp__text" dateTime={props.dateTime || new Date(date).toISOString()}>
118137
{!children ? defaultDisplay : children}
119138
</time>
120139
</span>

0 commit comments

Comments
 (0)