Skip to content

Commit

Permalink
X2-9688 added loading state for Arrival picker dates (#349)
Browse files Browse the repository at this point in the history
* added loading state for Arrival picker dates

* fix error

* resolve feedback

* fix crush

* fix lint

* fix loading status

* resolve feedback
  • Loading branch information
SemenStruchev authored Oct 1, 2024
1 parent ff00cfe commit fae0453
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/components/DatePicker/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const DatePicker = ({
value,
getDayContent,
disabledDays = [],
loadingDays = [],
shouldShowYearPicker = false,
onChange,
onMonthChange,
Expand Down Expand Up @@ -107,6 +108,18 @@ export const DatePicker = ({
return disabledDays(date);
};

const isLoading = (date) => {
if (isArray(loadingDays)) {
return loadingDays.some((_date) => isSame(now(_date, timezoneName), date, "day"));
}

if (isFunction(loadingDays)) {
return loadingDays(date);
}

return loadingDays(date);
};

const handleRelativeRangeChanged = (rangeName, range) => {
setCurrentMonth(range.from);
setStartMonth(range.from);
Expand Down Expand Up @@ -204,11 +217,13 @@ export const DatePicker = ({
const renderDay = (date) => {
const tooltipContent = getTooltip?.(date);
const disabled = isDisabled(date);
const loading = isLoading(date);

return tooltipContent ? (
<Tooltip placement="top" content={tooltipContent}>
<Day
disabled={disabled}
isLoading={loading}
selectedDate={value}
date={date}
getContent={getDayContent}
Expand All @@ -218,6 +233,7 @@ export const DatePicker = ({
) : (
<Day
disabled={disabled}
isLoading={loading}
selectedDate={value}
date={date}
getContent={getDayContent}
Expand Down
20 changes: 19 additions & 1 deletion src/components/DatePicker/Day.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ import clsx from "clsx";
import dayjs from "dayjs";
import PropTypes from "prop-types";
import React from "react";
import { Dot } from "../Dot/Dot";

export const Day = ({ selectedDate, date, getContent, currentMonth, disabled = false }) => {
export const Day = ({ selectedDate, date, getContent, currentMonth, isLoading = false, disabled = false }) => {
const isSameMonth = dayjs(currentMonth).isSame(date, "month");

if (getContent && isSameMonth) {
if (isLoading) {
return (
<div className="mr-2 flex animate-pulse items-center justify-center">
<Dot className="ui-day-content pointer-events-none !cursor-not-allowed" color="secondary" />
</div>
);
}

return <DayContent selectedDate={selectedDate} date={date} getContent={getContent} />;
}

if (isLoading) {
return (
<div className="mr-2 flex animate-pulse items-center justify-center">
<Dot className="pointer-events-none !cursor-not-allowed" color="secondary" size="xlarge" />
</div>
);
}

const isSameDay = selectedDate && dayjs(selectedDate).isSame(date, "day");

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/Dot/Dot.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const sizes = {
small: "h-1 w-1",
medium: "h-1.5 w-1.5",
large: "h-2 w-2",
xlarge: "h-8 w-8",
};

export const Dot = ({ color = "primary", size = "medium", className, ...rest }) => {
Expand Down

0 comments on commit fae0453

Please sign in to comment.