From f08060803d6152acc9a6e8bbba149f0ca18c2a58 Mon Sep 17 00:00:00 2001 From: Boyan Bogdanov Date: Tue, 22 Nov 2022 16:27:17 +0200 Subject: [PATCH 1/3] adds weekNumberContent prop to MonthView to render custom content in place of week numbers, much like tileContent --- src/Calendar.jsx | 3 +++ src/MonthView.jsx | 5 ++++- src/MonthView/WeekNumber.jsx | 21 +++++++++++++++++---- src/MonthView/WeekNumbers.jsx | 12 ++++++++++-- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Calendar.jsx b/src/Calendar.jsx index f8f67bcd..c74643fc 100644 --- a/src/Calendar.jsx +++ b/src/Calendar.jsx @@ -521,6 +521,7 @@ export default class Calendar extends Component { showFixedNumberOfWeeks, showNeighboringMonth, showWeekNumbers, + weekNumberContent, } = this.props; const { onMouseLeave } = this; @@ -540,6 +541,7 @@ export default class Calendar extends Component { } showNeighboringMonth={showNeighboringMonth} showWeekNumbers={showWeekNumbers} + weekNumberContent={weekNumberContent} {...commonProps} /> ); @@ -698,6 +700,7 @@ Calendar.propTypes = { showNavigation: PropTypes.bool, showNeighboringMonth: PropTypes.bool, showWeekNumbers: PropTypes.bool, + weekNumberContent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), tileClassName: PropTypes.oneOfType([PropTypes.func, isClassName]), tileContent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), tileDisabled: PropTypes.func, diff --git a/src/MonthView.jsx b/src/MonthView.jsx index ccef7ada..4303a857 100644 --- a/src/MonthView.jsx +++ b/src/MonthView.jsx @@ -25,6 +25,7 @@ export default function MonthView(props) { formatWeekday, onClickWeekNumber, showWeekNumbers, + weekNumberContent, ...childProps } = props; @@ -41,7 +42,7 @@ export default function MonthView(props) { } function renderWeekNumbers() { - if (!showWeekNumbers) { + if (!showWeekNumbers && !weekNumberContent) { return null; } @@ -52,6 +53,7 @@ export default function MonthView(props) { onClickWeekNumber={onClickWeekNumber} onMouseLeave={onMouseLeave} showFixedNumberOfWeeks={showFixedNumberOfWeeks} + weekNumberContent={weekNumberContent} /> ); } @@ -95,4 +97,5 @@ MonthView.propTypes = { onMouseLeave: PropTypes.func, showFixedNumberOfWeeks: PropTypes.bool, showWeekNumbers: PropTypes.bool, + weekNumberContent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), }; diff --git a/src/MonthView/WeekNumber.jsx b/src/MonthView/WeekNumber.jsx index 289d0c0e..90088627 100644 --- a/src/MonthView/WeekNumber.jsx +++ b/src/MonthView/WeekNumber.jsx @@ -1,15 +1,27 @@ -import React from 'react'; +import React, { useMemo } from 'react'; import PropTypes from 'prop-types'; const className = 'react-calendar__tile'; -export default function WeekNumber({ date, onClickWeekNumber, weekNumber, ...otherProps }) { +export default function WeekNumber({ + date, + onClickWeekNumber, + weekNumber, + weekNumberContent, + ...otherProps +}) { const props = { className, ...otherProps, }; - const children = {weekNumber}; + const weekContent = useMemo(() => { + return typeof weekNumberContent === 'function' + ? weekNumberContent({ date, weekNumber }) + : weekNumberContent; + }, [weekNumberContent, weekNumber, date]); + + const children = <>{weekContent ?? weekNumber}; return onClickWeekNumber ? (