Skip to content

Commit

Permalink
feat: loading state and tooltip for specific dates in calendar compon…
Browse files Browse the repository at this point in the history
…ent (#166)

add: loading state and tooltip for specific dates in calendar component
  • Loading branch information
mahakmakharia authored Oct 28, 2024
1 parent 9330aae commit 8277062
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
6 changes: 6 additions & 0 deletions apps/www/content/primitives/components/calendar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ links:

</Flex>
</Preview>

## Loader

<Preview>
<Calendar loadingData={true} />
</Preview>
67 changes: 56 additions & 11 deletions packages/raystack/calendar/calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { DayPicker, DayPickerProps, DropdownProps } from "react-day-picker";
import { cva } from "class-variance-authority";
import {
dateLib,
DayPicker,
DayPickerProps,
DropdownProps,
} from 'react-day-picker';
import { cva } from 'class-variance-authority';
import {
ChevronRightIcon,
ChevronLeftIcon,
ChevronUpIcon,
ChevronDownIcon,
} from "@radix-ui/react-icons";
import styles from "./calendar.module.css";
import { Select } from "~/select";
import { ChangeEvent, useEffect, useState } from "react";
import { Flex } from "~/flex/flex";
} from '@radix-ui/react-icons';
import styles from './calendar.module.css';
import { Select } from '~/select';
import { ChangeEvent, useEffect, useState } from 'react';
import { Flex } from '~/flex/flex';
import { Tooltip } from '~/tooltip';
import Skeleton from 'react-loading-skeleton';

interface OnDropdownOpen {
onDropdownOpen?: VoidFunction;
}

export type CalendarProps = DayPickerProps & OnDropdownOpen;
interface CalendarPropsExtended {
showTooltip?: boolean;
tooltipMessages?: { [key: string]: any };
loadingData?: boolean;
}

export type CalendarProps = DayPickerProps &
OnDropdownOpen &
CalendarPropsExtended;

const root = cva(styles.calendarRoot);

Expand Down Expand Up @@ -54,7 +69,7 @@ function DropDown({
</Select.Trigger>
<Select.Content className={styles.dropdown_content}>
<Select.ScrollUpButton asChild>
<Flex justify={"center"}>
<Flex justify={'center'}>
<ChevronUpIcon />
</Flex>
</Select.ScrollUpButton>
Expand All @@ -73,7 +88,7 @@ function DropDown({
))}
</Select.Viewport>
<Select.ScrollDownButton asChild>
<Flex justify={"center"}>
<Flex justify={'center'}>
<ChevronDownIcon />
</Flex>
</Select.ScrollDownButton>
Expand All @@ -87,21 +102,51 @@ export const Calendar = function ({
classNames,
showOutsideDays = true,
onDropdownOpen,
showTooltip = false,
tooltipMessages = {},
loadingData = false,
...props
}: CalendarProps) {
return (
<DayPicker
showOutsideDays={showOutsideDays}
components={{
Chevron: (props) => {
if (props.orientation === "left") {
if (props.orientation === 'left') {
return <ChevronLeftIcon {...props} />;
}
return <ChevronRightIcon {...props} />;
},
Dropdown: (props: DropdownProps) => (
<DropDown {...props} onDropdownOpen={onDropdownOpen} />
),
DayButton: (props) => {
const { day, ...buttonProps } = props;
const message =
tooltipMessages[dateLib.format(day.date, 'dd-MM-yyyy')];
return (
<Tooltip
side="top"
disabled={loadingData || !showTooltip || !message}
message={message}
>
<button {...buttonProps} />
</Tooltip>
);
},
MonthGrid: (props) =>
loadingData ? (
<Skeleton
count={6}
height={'12px'}
width={'252px'}
style={{ marginBottom: 'var(--space-5)' }}
highlightColor="var(--background-base)"
baseColor="var(--background-base-hover)"
/>
) : (
<table {...props} />
),
}}
classNames={{
caption_label: styles.caption_label,
Expand Down

0 comments on commit 8277062

Please sign in to comment.