Skip to content

Commit ae9c63b

Browse files
set the default time to now when no dateTime is passed
1 parent c4aec36 commit ae9c63b

File tree

1 file changed

+11
-23
lines changed
  • packages/sdk-components-react/src

1 file changed

+11
-23
lines changed

packages/sdk-components-react/src/time.tsx

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -374,38 +374,31 @@ export const Time = forwardRef<ElementRef<"time">, TimeProps>(
374374
dateStyle = DEFAULT_DATE_STYLE,
375375
timeStyle = DEFAULT_TIME_STYLE,
376376
// native html attribute in react style
377-
dateTime = INITIAL_DATE_STRING,
377+
dateTime,
378378
...props
379379
},
380380
ref
381381
) => {
382-
const [clientSideDate, setClientSideDate] = useState<string | Date | null>(
383-
null
382+
const [currentDate, setCurrentDate] = useState<string | Date | null>(
383+
dateTime ?? INITIAL_DATE_STRING
384384
);
385385
const locale = `${languageOrDefault(language)}-${countryOrDefault(
386386
country
387387
)}`;
388388

389-
const options: Intl.DateTimeFormatOptions = {
390-
dateStyle: dateStyleOrUndefined(dateStyle),
391-
timeStyle: timeStyleOrUndefined(timeStyle),
392-
};
393-
394389
useEffect(() => {
395-
if (dateTime === INITIAL_DATE_STRING) {
396-
setClientSideDate(new Date());
390+
if (dateTime === undefined) {
391+
setCurrentDate(new Date());
397392
}
398393
}, [dateTime]);
399394

400-
const dateToUse =
401-
typeof window !== "undefined" &&
402-
clientSideDate !== null &&
403-
dateTime === INITIAL_DATE_STRING
404-
? clientSideDate
405-
: dateTime;
395+
const options: Intl.DateTimeFormatOptions = {
396+
dateStyle: dateStyleOrUndefined(dateStyle),
397+
timeStyle: timeStyleOrUndefined(timeStyle),
398+
};
406399

407400
const datetimeString =
408-
dateToUse === null ? INVALID_DATE_STRING : dateToUse.toString();
401+
currentDate === null ? INVALID_DATE_STRING : currentDate.toString();
409402

410403
const date = parseDate(datetimeString);
411404
let formattedDate = datetimeString;
@@ -419,12 +412,7 @@ export const Time = forwardRef<ElementRef<"time">, TimeProps>(
419412
}
420413

421414
return (
422-
<time
423-
ref={ref}
424-
dateTime={datetimeString}
425-
{...props}
426-
suppressHydrationWarning
427-
>
415+
<time ref={ref} dateTime={datetimeString} {...props}>
428416
{formattedDate}
429417
</time>
430418
);

0 commit comments

Comments
 (0)