Skip to content

Commit 0297d34

Browse files
committed
refactor(calendar): lazy compute tooltip title. HVUIKIT-5642
1 parent f22859e commit 0297d34

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/core/src/Calendar/SingleCalendar/CalendarCell.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useRef } from "react";
22
import PropTypes from "prop-types";
33
import clsx from "clsx";
4+
import useComputation from "../../utils/useComputation";
45
import {
56
getDateISO,
67
getFormattedDate,
@@ -27,6 +28,7 @@ const HvCalendarCell = ({
2728
...others
2829
}) => {
2930
const buttonEl = useRef(null);
31+
const [title, computeTitle] = useComputation(() => getFormattedDate(value, locale));
3032
const { startDate, endDate } = calendarValue;
3133
const isCellToday = isSameDay(value, today);
3234
const isCellSelected = isSameDay(calendarValue, value);
@@ -81,7 +83,8 @@ const HvCalendarCell = ({
8183
<HvTooltip
8284
key={getDateISO(value)}
8385
enterDelay={600}
84-
title={<HvTypography noWrap>{getFormattedDate(value, locale)}</HvTypography>}
86+
onOpen={computeTitle}
87+
title={title ? <HvTypography noWrap>{title}</HvTypography> : ""}
8588
>
8689
<div
8790
className={clsx(classes.dateWrapper, {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useState, useRef } from "react";
2+
3+
export default function useComputation(valueFn, valueFallback) {
4+
const computed = useRef(null);
5+
const [value, setValue] = useState(valueFallback);
6+
7+
const computeValue = () => {
8+
if (!computed.current) {
9+
setValue(valueFn?.());
10+
computed.current = true;
11+
}
12+
};
13+
14+
return [value, computeValue];
15+
}

0 commit comments

Comments
 (0)