Skip to content

Commit 8277062

Browse files
feat: loading state and tooltip for specific dates in calendar component (#166)
add: loading state and tooltip for specific dates in calendar component
1 parent 9330aae commit 8277062

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

apps/www/content/primitives/components/calendar.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ links:
3131

3232
</Flex>
3333
</Preview>
34+
35+
## Loader
36+
37+
<Preview>
38+
<Calendar loadingData={true} />
39+
</Preview>

packages/raystack/calendar/calendar.tsx

+56-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import { DayPicker, DayPickerProps, DropdownProps } from "react-day-picker";
2-
import { cva } from "class-variance-authority";
1+
import {
2+
dateLib,
3+
DayPicker,
4+
DayPickerProps,
5+
DropdownProps,
6+
} from 'react-day-picker';
7+
import { cva } from 'class-variance-authority';
38
import {
49
ChevronRightIcon,
510
ChevronLeftIcon,
611
ChevronUpIcon,
712
ChevronDownIcon,
8-
} from "@radix-ui/react-icons";
9-
import styles from "./calendar.module.css";
10-
import { Select } from "~/select";
11-
import { ChangeEvent, useEffect, useState } from "react";
12-
import { Flex } from "~/flex/flex";
13+
} from '@radix-ui/react-icons';
14+
import styles from './calendar.module.css';
15+
import { Select } from '~/select';
16+
import { ChangeEvent, useEffect, useState } from 'react';
17+
import { Flex } from '~/flex/flex';
18+
import { Tooltip } from '~/tooltip';
19+
import Skeleton from 'react-loading-skeleton';
1320

1421
interface OnDropdownOpen {
1522
onDropdownOpen?: VoidFunction;
1623
}
1724

18-
export type CalendarProps = DayPickerProps & OnDropdownOpen;
25+
interface CalendarPropsExtended {
26+
showTooltip?: boolean;
27+
tooltipMessages?: { [key: string]: any };
28+
loadingData?: boolean;
29+
}
30+
31+
export type CalendarProps = DayPickerProps &
32+
OnDropdownOpen &
33+
CalendarPropsExtended;
1934

2035
const root = cva(styles.calendarRoot);
2136

@@ -54,7 +69,7 @@ function DropDown({
5469
</Select.Trigger>
5570
<Select.Content className={styles.dropdown_content}>
5671
<Select.ScrollUpButton asChild>
57-
<Flex justify={"center"}>
72+
<Flex justify={'center'}>
5873
<ChevronUpIcon />
5974
</Flex>
6075
</Select.ScrollUpButton>
@@ -73,7 +88,7 @@ function DropDown({
7388
))}
7489
</Select.Viewport>
7590
<Select.ScrollDownButton asChild>
76-
<Flex justify={"center"}>
91+
<Flex justify={'center'}>
7792
<ChevronDownIcon />
7893
</Flex>
7994
</Select.ScrollDownButton>
@@ -87,21 +102,51 @@ export const Calendar = function ({
87102
classNames,
88103
showOutsideDays = true,
89104
onDropdownOpen,
105+
showTooltip = false,
106+
tooltipMessages = {},
107+
loadingData = false,
90108
...props
91109
}: CalendarProps) {
92110
return (
93111
<DayPicker
94112
showOutsideDays={showOutsideDays}
95113
components={{
96114
Chevron: (props) => {
97-
if (props.orientation === "left") {
115+
if (props.orientation === 'left') {
98116
return <ChevronLeftIcon {...props} />;
99117
}
100118
return <ChevronRightIcon {...props} />;
101119
},
102120
Dropdown: (props: DropdownProps) => (
103121
<DropDown {...props} onDropdownOpen={onDropdownOpen} />
104122
),
123+
DayButton: (props) => {
124+
const { day, ...buttonProps } = props;
125+
const message =
126+
tooltipMessages[dateLib.format(day.date, 'dd-MM-yyyy')];
127+
return (
128+
<Tooltip
129+
side="top"
130+
disabled={loadingData || !showTooltip || !message}
131+
message={message}
132+
>
133+
<button {...buttonProps} />
134+
</Tooltip>
135+
);
136+
},
137+
MonthGrid: (props) =>
138+
loadingData ? (
139+
<Skeleton
140+
count={6}
141+
height={'12px'}
142+
width={'252px'}
143+
style={{ marginBottom: 'var(--space-5)' }}
144+
highlightColor="var(--background-base)"
145+
baseColor="var(--background-base-hover)"
146+
/>
147+
) : (
148+
<table {...props} />
149+
),
105150
}}
106151
classNames={{
107152
caption_label: styles.caption_label,

0 commit comments

Comments
 (0)