Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update theme for Calendar component #218

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/form/Calendar/Calendar.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ export const Simple = () => {
);
};

export const Today = () => {
const [date, setDate] = useState<Date>();

return (
<Card>
<Calendar
showToday
showDayOfWeek
value={date}
onChange={(date: Date) => setDate(date)}
/>
<Divider />
<Stack inline={false} justifyContent="center">
{date?.toLocaleDateString() ?? 'No date selected'}
</Stack>
</Card>
);
};

export const Disabled = () => {
const [date, setDate] = useState<Date>();

Expand Down Expand Up @@ -121,6 +140,7 @@ export const Range = () => {
onChange={val => setRange(val as [Date, Date | undefined])}
isRange
showDayOfWeek
showToday
/>
<Divider />
<Stack justifyContent="center">
Expand Down
19 changes: 13 additions & 6 deletions src/form/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export interface CalendarProps {
*/
showDayOfWeek?: boolean;

/**
* Whether to highlight the today.
*/
showToday?: boolean;

/**
* Whether to animate the calendar.
*/
Expand Down Expand Up @@ -101,9 +106,10 @@ export const Calendar: FC<CalendarProps> = ({
value,
disabled,
isRange,
previousArrow = '',
nextArrow = '',
previousArrow = '',
nextArrow = '',
showDayOfWeek,
showToday,
animated = true,
onChange,
onViewChange,
Expand Down Expand Up @@ -221,7 +227,7 @@ export const Calendar: FC<CalendarProps> = ({
variant="text"
disabled={disabled}
onClick={previousClickHandler}
className={twMerge(theme.header.prev)}
className={theme.header.prev}
disablePadding
>
{previousArrow}
Expand All @@ -230,11 +236,11 @@ export const Calendar: FC<CalendarProps> = ({
disabled={disabled}
variant="text"
onClick={headerClickHandler}
className={twMerge(theme.header.mid)}
className={theme.header.mid}
disablePadding
fullWidth
>
<SmallHeading disableMargins className={twMerge(theme.title)}>
<SmallHeading disableMargins className={theme.title}>
{view === 'days' && format(viewValue, 'MMMM')}
{view === 'months' && <>{yearValue}</>}
{view === 'years' && (
Expand All @@ -248,7 +254,7 @@ export const Calendar: FC<CalendarProps> = ({
variant="text"
disabled={disabled}
onClick={nextClickHandler}
className={twMerge(theme.header.next)}
className={theme.header.next}
disablePadding
>
{nextArrow}
Expand Down Expand Up @@ -277,6 +283,7 @@ export const Calendar: FC<CalendarProps> = ({
isRange={isRange}
current={isRange ? [rangeStart, rangeEnd] : value}
showDayOfWeek={showDayOfWeek}
showToday={showToday}
xAnimation={xAnimation}
animated={animated}
onChange={dateChangeHandler}
Expand Down
43 changes: 26 additions & 17 deletions src/form/Calendar/CalendarDays/CalendarDays.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, useCallback, useMemo, useState } from 'react';
import { isAfter, isBefore } from 'date-fns';
import { isAfter, isBefore, isToday } from 'date-fns';
import { Button } from '@/elements';
import {
daysOfWeek,
Expand All @@ -9,7 +9,7 @@ import {
isPreviousWeekEmpty
} from '@/form/Calendar/utils';
import { AnimatePresence, motion } from 'framer-motion';
import { useComponentTheme } from '@/utils';
import { cn, useComponentTheme } from '@/utils';
import { CalendarTheme } from '@/form/Calendar/CalendarTheme';
import { twMerge } from 'tailwind-merge';

Expand Down Expand Up @@ -64,6 +64,11 @@ export interface CalendarDaysProps {
*/
showDayOfWeek?: boolean;

/**
* Whether to highlight the today.
*/
showToday?: boolean;

/**
* Customize the labels for the days of the week.
*/
Expand Down Expand Up @@ -116,6 +121,7 @@ export const CalendarDays: FC<CalendarDaysProps> = ({
animated,
xAnimation = 0,
showDayOfWeek,
showToday,
dayOfWeekLabels = daysOfWeek,
hidePrevMonthDays,
hideNextMonthDays,
Expand Down Expand Up @@ -187,17 +193,19 @@ export const CalendarDays: FC<CalendarDaysProps> = ({
return (
<Button
key={day.formattedDate}
className={twMerge(
days.day,
!isActive &&
(day.isNextMonth || day.isPreviousMonth) &&
days.outside,
isRangeMiddle && days.range,
isRange && isRangeStart && !isRangeEnd && days.startRangeDate,
isRange && !rangeConnectsBottom && days.cornerStartDateBottom,
isRange && isRangeEnd && !isRangeStart && days.endRangeDate,
isRange && !rangeConnectsTop && days.cornerEndDateTop
)}
className={cn(days.day, {
[days.outside]:
!isActive && (day.isNextMonth || day.isPreviousMonth),
[days.today]: showToday && isToday(day.date),
[days.selected]: isActive,
[days.hover]: day.date === currentHover,
[days.range]: isRangeMiddle,
[days.startRangeDate]: isRange && isRangeStart && !isRangeEnd,
[days.cornerStartDateBottom]:
isRange && isActive && !rangeConnectsBottom,
[days.endRangeDate]: isRange && isRangeEnd && !isRangeStart,
[days.cornerEndDateTop]: isRange && isActive && !rangeConnectsTop
})}
onMouseEnter={() => handleHover(day.date)}
onMouseLeave={() => handleHover(null)}
variant={buttonVariant}
Expand All @@ -223,7 +231,8 @@ export const CalendarDays: FC<CalendarDaysProps> = ({
hoveringDate,
days,
hideNextMonthDays,
hidePrevMonthDays
hidePrevMonthDays,
showToday
]
);

Expand All @@ -239,16 +248,16 @@ export const CalendarDays: FC<CalendarDaysProps> = ({
}}
>
{showDayOfWeek && (
<div className={twMerge(days.header)}>
<div className={days.header}>
{dayOfWeekLabels.map(day => (
<div key={`day-${day}`} className={twMerge(days.dayOfWeek)}>
<div key={`day-${day}`} className={days.dayOfWeek}>
{day.substring(0, 2)}
</div>
))}
</div>
)}
{weeks.map((week, i) => (
<div key={`week-${i}`} className={twMerge(days.week)}>
<div key={`week-${i}`} className={days.week}>
{week.map(renderDay)}
</div>
))}
Expand Down
6 changes: 3 additions & 3 deletions src/form/Calendar/CalendarMonths/CalendarMonths.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FC } from 'react';
import { Button } from '@/elements';
import { monthNames } from '@/form/Calendar/utils';
import { useComponentTheme } from '@/utils';
import { cn, theme, useComponentTheme } from '@/utils';
import { CalendarTheme } from '@/form/Calendar/CalendarTheme';
import { twMerge } from 'tailwind-merge';

Expand Down Expand Up @@ -30,11 +30,11 @@ export const CalendarMonths: FC<CalendarMonthsProps> = ({
const { months }: CalendarTheme = useComponentTheme('calendar', customTheme);

return (
<div className={twMerge(months.root)}>
<div className={months.root}>
{monthNames.map((month, i) => (
<Button
key={month}
className={twMerge(months.month)}
className={cn(months.month, { [months.selected]: value === i })}
color={value === i ? 'primary' : 'default'}
variant={value === i ? 'filled' : 'text'}
disableMargins
Expand Down
15 changes: 7 additions & 8 deletions src/form/Calendar/CalendarRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { CalendarProps } from './Calendar';
import { CalendarDays } from './CalendarDays';
import { SmallHeading } from '@/typography';
import { Divider, Stack } from '@/layout';
import { twMerge } from 'tailwind-merge';
import { useComponentTheme } from '@/utils';
import { CalendarRangeTheme } from './CalendarRangeTheme';

Expand Down Expand Up @@ -140,14 +139,14 @@ export const CalendarRange: FC<CalendarRangeProps> = ({
}, [scrollDirection]);

return (
<div className={twMerge(theme.base)}>
<header className={twMerge(theme.header.base)}>
<div className={theme.base}>
<header className={theme.header.base}>
<Stack>
<Button
variant="text"
disabled={disabled}
onClick={previousYearClickHandler}
className={twMerge(theme.header.prev)}
className={theme.header.prev}
disablePadding
>
{previousYearArrow}
Expand All @@ -156,13 +155,13 @@ export const CalendarRange: FC<CalendarRangeProps> = ({
variant="text"
disabled={disabled}
onClick={previousClickHandler}
className={twMerge(theme.header.mid)}
className={theme.header.mid}
disablePadding
>
{previousArrow}
</Button>
</Stack>
<SmallHeading className={twMerge(theme.title)} disableMargins>
<SmallHeading className={theme.title} disableMargins>
{displayMonths.map(i => (
<span key={addMonths(viewValue, showPast ? -i : i).toDateString()}>
{format(addMonths(viewValue, showPast ? -i : i), 'MMMM')}
Expand All @@ -174,7 +173,7 @@ export const CalendarRange: FC<CalendarRangeProps> = ({
variant="text"
disabled={disabled}
onClick={nextClickHandler}
className={twMerge(theme.header.next)}
className={theme.header.next}
disablePadding
>
{nextArrow}
Expand All @@ -201,7 +200,7 @@ export const CalendarRange: FC<CalendarRangeProps> = ({
scale: { type: animated ? 'tween' : false }
}}
>
<div className={twMerge(theme.content)}>
<div className={theme.content}>
{displayMonths.map((offset, idx) => (
<Fragment key={`calendar-${offset}`}>
<CalendarDays
Expand Down
Loading
Loading