@@ -2,6 +2,7 @@ import * as React from 'react';
2
2
import styles from '@patternfly/react-styles/css/components/Timestamp/timestamp' ;
3
3
import { css } from '@patternfly/react-styles' ;
4
4
import { Tooltip } from '../Tooltip' ;
5
+ import { isValidDate } from '../CalendarMonth' ;
5
6
6
7
export enum TimestampFormat {
7
8
full = 'full' ,
@@ -78,7 +79,7 @@ export const Timestamp: React.FunctionComponent<TimestampProps> = ({
78
79
children,
79
80
className,
80
81
customFormat,
81
- date : dateProp = new Date ( ) ,
82
+ date : dateProp ,
82
83
dateFormat,
83
84
displaySuffix = '' ,
84
85
is12Hour,
@@ -87,22 +88,40 @@ export const Timestamp: React.FunctionComponent<TimestampProps> = ({
87
88
tooltip,
88
89
...props
89
90
} : 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
+
90
109
const hasTimeFormat = timeFormat && ! customFormat ;
91
110
const formatOptions = {
92
111
...( dateFormat && ! customFormat && { dateStyle : dateFormat } ) ,
93
112
...( customFormat && { ...customFormat } ) ,
94
113
...( is12Hour !== undefined && { hour12 : is12Hour } )
95
114
} ;
96
115
97
- const dateAsLocaleString = new Date ( dateProp ) . toLocaleString ( locale , {
116
+ const dateAsLocaleString = new Date ( date ) . toLocaleString ( locale , {
98
117
...formatOptions ,
99
118
...( hasTimeFormat && { timeStyle : timeFormat } )
100
119
} ) ;
101
120
const defaultDisplay = `${ dateAsLocaleString } ${ displaySuffix ? ' ' + displaySuffix : '' } ` ;
102
121
103
122
const utcTimeFormat = timeFormat !== 'short' ? 'medium' : 'short' ;
104
123
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 , {
106
125
...formatOptions ,
107
126
...( hasTimeFormat && { timeStyle : utcTimeFormat } )
108
127
} ) ;
@@ -114,7 +133,7 @@ export const Timestamp: React.FunctionComponent<TimestampProps> = ({
114
133
{ ...( tooltip && { tabIndex : 0 } ) }
115
134
{ ...props }
116
135
>
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 ( ) } >
118
137
{ ! children ? defaultDisplay : children }
119
138
</ time >
120
139
</ span >
0 commit comments