Skip to content

Commit f22859e

Browse files
committed
refactor(calendar): memo weekDays. HVUIKIT-5642
conditional MonthSelector instantiation
1 parent 72b32de commit f22859e

File tree

4 files changed

+41
-44
lines changed

4 files changed

+41
-44
lines changed

packages/core/src/Calendar/CalendarWeekLabels/CalendarWeekLabel.js renamed to packages/core/src/Calendar/CalendarWeekLabels/CalendarWeekLabels.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import React from "react";
22
import PropTypes from "prop-types";
33
import { withStyles } from "@material-ui/core";
4-
import { getWeekdayNamesList } from "../utils";
5-
import { REPRESENTATION_VALUES } from "../enums";
6-
import HvTypography from "../../Typography";
4+
import { HvTypography } from "../..";
75
import styles from "./styles";
86

9-
const HvCalendarWeekLabel = ({ classes, locale }) => {
10-
const listWeekdayNamesNarrow = getWeekdayNamesList(locale, REPRESENTATION_VALUES.NARROW);
11-
12-
return listWeekdayNamesNarrow.map((dayName, index) => {
7+
const HvCalendarWeekLabel = ({ classes, labels = [] }) => {
8+
return labels.map((dayName, index) => {
139
const key = `${dayName}-${index}`;
1410
return (
1511
<HvTypography variant="highlightText" className={classes.calendarDay} key={key}>
@@ -29,9 +25,9 @@ HvCalendarWeekLabel.propTypes = {
2925
*/
3026
id: PropTypes.string,
3127
/**
32-
* Locale to be used by the calendar.
28+
* Localized day of week labels.
3329
*/
34-
locale: PropTypes.string,
30+
labels: PropTypes.arrayOf(PropTypes.string),
3531
/**
3632
* Callback to define the input date.
3733
*/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default } from "./CalendarWeekLabel";
1+
export { default } from "./CalendarWeekLabels";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default } from "./CalendarWeekLabel";
1+
export { default } from "./CalendarWeekLabels";

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
22

3-
import React, { useState, useContext } from "react";
3+
import React, { useState, useContext, useMemo } from "react";
44
import PropTypes from "prop-types";
55
import { withStyles } from "@material-ui/core";
66
import clsx from "clsx";
77
import isNil from "lodash/isNil";
88
import { isKeypress, KeyboardCodes, setId } from "../../utils";
99
import styles from "./styles";
1010
import { HvFormElementDescriptorsContext } from "../../Forms/FormElement";
11-
import { VIEW_MODE } from "../enums";
12-
import { isRange, isDate } from "../utils";
11+
import { VIEW_MODE, REPRESENTATION_VALUES } from "../enums";
12+
import { isRange, isDate, getWeekdayNamesList } from "../utils";
1313
import { generateCalendarModel } from "../model";
1414
import CalendarCell from "./CalendarCell";
1515
import CalendarHeader from "../CalendarHeader";
@@ -51,6 +51,11 @@ const HvSingleCalendar = ({
5151
const firstDayOfCurrentMonth = new Date(calModel.year, calModel.month - 1, 1);
5252
const firstDayOfCurrentMonthTime = firstDayOfCurrentMonth.getTime();
5353

54+
const listWeekdayNames = useMemo(
55+
() => getWeekdayNamesList(locale, REPRESENTATION_VALUES.NARROW),
56+
[locale]
57+
);
58+
5459
const handleChange = (event, date) => {
5560
event?.preventDefault();
5661
onChange?.(event, date);
@@ -119,34 +124,6 @@ const HvSingleCalendar = ({
119124
);
120125
};
121126

122-
const monthSelector = (
123-
<HvMonthSelector
124-
id={id}
125-
locale={locale}
126-
onChange={onVisibleDateChange}
127-
onViewModeChange={(viewMode) => setCalViewMode(viewMode)}
128-
visibleMonth={visibleMonth || today.getMonth() + 1}
129-
rangeMode={rangeMode}
130-
/>
131-
);
132-
133-
const navigationAndCalendar = (
134-
<div className="calendarWrapper">
135-
<HvComposedNavigation
136-
id={id}
137-
locale={locale}
138-
onChange={onVisibleDateChange}
139-
onViewModeChange={(viewMode) => setCalViewMode(viewMode)}
140-
visibleYear={visibleYear || today.getFullYear()}
141-
visibleMonth={visibleMonth || today.getMonth() + 1}
142-
/>
143-
<div className={classes.calendarGrid} aria-controls={HvCalendarHeader?.[0]?.id}>
144-
<CalendarWeekLabels locale={locale} />
145-
{calModel.dates.map(renderCalendarDate)}
146-
</div>
147-
</div>
148-
);
149-
150127
return (
151128
<div className={clsx(className, classes.calendarContainer)} {...others}>
152129
<div id={id} className={classes.calendarWrapper}>
@@ -157,8 +134,32 @@ const HvSingleCalendar = ({
157134
showEndDate={showEndDate && !isDateSelectionMode}
158135
/>
159136
<>
160-
{calViewMode === VIEW_MODE.CALENDAR && navigationAndCalendar}
161-
{calViewMode === VIEW_MODE.MONTHLY && monthSelector}
137+
{calViewMode === VIEW_MODE.CALENDAR && (
138+
<div>
139+
<HvComposedNavigation
140+
id={id}
141+
locale={locale}
142+
onChange={onVisibleDateChange}
143+
onViewModeChange={(viewMode) => setCalViewMode(viewMode)}
144+
visibleYear={visibleYear || today.getFullYear()}
145+
visibleMonth={visibleMonth || today.getMonth() + 1}
146+
/>
147+
<div className={classes.calendarGrid} aria-controls={HvCalendarHeader?.[0]?.id}>
148+
<CalendarWeekLabels labels={listWeekdayNames} />
149+
{calModel.dates.map(renderCalendarDate)}
150+
</div>
151+
</div>
152+
)}
153+
{calViewMode === VIEW_MODE.MONTHLY && (
154+
<HvMonthSelector
155+
id={id}
156+
locale={locale}
157+
onChange={onVisibleDateChange}
158+
onViewModeChange={(viewMode) => setCalViewMode(viewMode)}
159+
visibleMonth={visibleMonth || today.getMonth() + 1}
160+
rangeMode={rangeMode}
161+
/>
162+
)}
162163
</>
163164
</div>
164165
</div>

0 commit comments

Comments
 (0)