From 26dee00753bbc2496cc57fda1660d8c0a9d9b92a Mon Sep 17 00:00:00 2001 From: Alexey Immoreev Date: Sun, 3 Mar 2024 02:07:19 +0300 Subject: [PATCH] Add classNames --- packages/react-calendar/src/Calendar.tsx | 61 ++++++++-- .../src/Calendar/Navigation.tsx | 72 +++++++++-- .../react-calendar/src/CenturyView.spec.tsx | 2 +- packages/react-calendar/src/CenturyView.tsx | 25 +++- .../src/CenturyView/Decade.spec.tsx | 10 +- .../react-calendar/src/CenturyView/Decade.tsx | 34 +++--- .../src/CenturyView/Decades.tsx | 51 ++++++-- .../react-calendar/src/DecadeView.spec.tsx | 2 +- packages/react-calendar/src/DecadeView.tsx | 23 +++- .../src/DecadeView/Year.spec.tsx | 10 +- .../react-calendar/src/DecadeView/Year.tsx | 32 +++-- .../react-calendar/src/DecadeView/Years.tsx | 51 ++++++-- packages/react-calendar/src/Flex.tsx | 9 +- .../react-calendar/src/MonthView.spec.tsx | 2 +- packages/react-calendar/src/MonthView.tsx | 57 +++++++-- .../react-calendar/src/MonthView/Day.spec.tsx | 8 +- packages/react-calendar/src/MonthView/Day.tsx | 40 +++--- .../react-calendar/src/MonthView/Days.tsx | 44 +++++-- .../src/MonthView/WeekNumber.tsx | 14 ++- .../src/MonthView/WeekNumbers.tsx | 9 +- .../react-calendar/src/MonthView/Weekdays.tsx | 30 ++++- packages/react-calendar/src/Tile.spec.tsx | 9 +- packages/react-calendar/src/Tile.tsx | 6 +- packages/react-calendar/src/TileGroup.tsx | 21 ++-- packages/react-calendar/src/YearView.spec.tsx | 2 +- packages/react-calendar/src/YearView.tsx | 23 +++- .../src/YearView/Month.spec.tsx | 10 +- .../react-calendar/src/YearView/Month.tsx | 12 +- .../react-calendar/src/YearView/Months.tsx | 44 +++++-- .../react-calendar/src/shared/propTypes.ts | 15 ++- packages/react-calendar/src/shared/types.ts | 2 +- .../react-calendar/src/shared/utils.spec.ts | 30 ++--- packages/react-calendar/src/shared/utils.ts | 115 ++++++++++++++---- 33 files changed, 646 insertions(+), 229 deletions(-) diff --git a/packages/react-calendar/src/Calendar.tsx b/packages/react-calendar/src/Calendar.tsx index eec48ddf..971936dd 100644 --- a/packages/react-calendar/src/Calendar.tsx +++ b/packages/react-calendar/src/Calendar.tsx @@ -20,7 +20,7 @@ import { isView, rangeOf, } from './shared/propTypes.js'; -import { between } from './shared/utils.js'; +import { between, mergeTileClassNames, type TileClassNames } from './shared/utils.js'; import type { Action, @@ -60,6 +60,15 @@ defaultMinDate.setFullYear(1, 0, 1); defaultMinDate.setHours(0, 0, 0, 0); const defaultMaxDate = new Date(8.64e15); +type CenturyClassNames = React.ComponentProps['classNames']; +type DecadeClassNames = React.ComponentProps['classNames']; +type MonthClassNames = React.ComponentProps['classNames']; +type YearClassNames = React.ComponentProps['classNames']; +type NavigationClassNames = React.ComponentProps['classNames']; + +const localSlotNames = ['base', 'selectRange', 'doubleView', 'viewContainer'] as const; +type LocalSlotName = (typeof localSlotNames)[number]; + export type CalendarProps = { /** * The beginning of a period that shall be displayed. If you wish to use react-calendar in an uncontrolled way, use `defaultActiveStartDate` instead. @@ -87,6 +96,22 @@ export type CalendarProps = { * @example ['class1', 'class2 class3'] */ className?: ClassName; + /** + * Class names that will be added to appropriate slots. + * + * @example {} + */ + classNames?: Partial< + Record & { + decade: DecadeClassNames; + century: CenturyClassNames; + month: MonthClassNames; + year: YearClassNames; + navigation: NavigationClassNames; + tileGroup: ClassName; + tile: TileClassNames; + } + >; /** * The beginning of a period that shall be displayed by default. If you wish to use react-calendar in a controlled way, use `activeStartDate` instead. * @@ -614,6 +639,7 @@ const Calendar = forwardRef(function Calendar(props: CalendarProps, ref) { allowPartialRange, calendarType, className, + classNames = {}, defaultActiveStartDate, defaultValue, defaultView, @@ -1042,6 +1068,10 @@ const Calendar = forwardRef(function Calendar(props: CalendarProps, ref) { ); @@ -1051,13 +1081,25 @@ const Calendar = forwardRef(function Calendar(props: CalendarProps, ref) { ); } case 'year': { return ( - + ); } case 'month': { @@ -1077,6 +1119,10 @@ const Calendar = forwardRef(function Calendar(props: CalendarProps, ref) { } showNeighboringMonth={showNeighboringMonth} showWeekNumbers={showWeekNumbers} + classNames={{ + ...classNames.month, + tile: mergeTileClassNames(classNames.tile, classNames.month?.tile), + }} {...commonProps} /> ); @@ -1115,6 +1161,7 @@ const Calendar = forwardRef(function Calendar(props: CalendarProps, ref) { showDoubleView={showDoubleView} view={view} views={views} + classNames={classNames.navigation} /> ); } @@ -1124,16 +1171,16 @@ const Calendar = forwardRef(function Calendar(props: CalendarProps, ref) { return (
{renderNavigation()}
diff --git a/packages/react-calendar/src/Calendar/Navigation.tsx b/packages/react-calendar/src/Calendar/Navigation.tsx index 2d8a726b..f2d3dcf4 100644 --- a/packages/react-calendar/src/Calendar/Navigation.tsx +++ b/packages/react-calendar/src/Calendar/Navigation.tsx @@ -2,6 +2,7 @@ import React from 'react'; import { getUserLocale } from 'get-user-locale'; +import clsx from 'clsx'; import { getCenturyLabel, @@ -18,11 +19,27 @@ import { formatYear as defaultFormatYear, } from '../shared/dateFormatter.js'; -import type { Action, NavigationLabelFunc, RangeType } from '../shared/types.js'; +import type { Action, ClassName, NavigationLabelFunc, RangeType } from '../shared/types.js'; const className = 'react-calendar__navigation'; +const slotNames = [ + 'base', + 'label', + 'divider', + 'labelText', + 'labelTextFrom', + 'labelTextTo', + 'arrow', + 'prev2Button', + 'prevButton', + 'nextButton', + 'next2Button', +] as const; +type SlotName = (typeof slotNames)[number]; + type NavigationProps = { + classNames?: Partial>; /** * The beginning of a period that shall be displayed. If you wish to use react-calendar in an uncontrolled way, use `defaultActiveStartDate` instead. * @@ -173,6 +190,7 @@ export default function Navigation({ showDoubleView, view, views, + classNames = {}, }: NavigationProps) { const drillUpAvailable = views.indexOf(view) > 0; const shouldShowPrevNext2Buttons = view !== 'century'; @@ -257,19 +275,33 @@ export default function Navigation({