Skip to content

Commit

Permalink
fix: Date Range Picker behavior (calcom#17914)
Browse files Browse the repository at this point in the history
* fix: Date Range Picker behaviour

* remove console log

* update

* update

* update css

---------

Co-authored-by: Peer Richelsen <[email protected]>
  • Loading branch information
anikdhabal and PeerRich authored Dec 2, 2024
1 parent 58b77b2 commit 0aa58c1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
13 changes: 5 additions & 8 deletions apps/web/playwright/out-of-office.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ test.describe("Out of office", () => {

await page.getByTestId("add_entry_ooo").click();

await page.locator('[id="date"]').click();
await page.locator('[data-testid="date-range"]').click();

await selectToAndFromDates(page, "13", "22", true);

Expand Down Expand Up @@ -247,7 +247,7 @@ test.describe("Out of office", () => {

await page.getByTestId("add_entry_ooo").click();

await page.locator('[id="date"]').click();
await page.locator('[data-testid="date-range"]').click();

await selectToAndFromDates(page, "13", "22");

Expand All @@ -258,7 +258,7 @@ test.describe("Out of office", () => {
// add another entry
await page.getByTestId("add_entry_ooo").click();

await page.locator('[id="date"]').click();
await page.locator('[data-testid="date-range"]').click();

await selectToAndFromDates(page, "11", "24");

Expand All @@ -277,7 +277,7 @@ test.describe("Out of office", () => {

await page.getByTestId("add_entry_ooo").click();

await page.locator('[id="date"]').click();
await page.locator('[data-testid="date-range"]').click();

await selectToAndFromDates(page, "13", "22");

Expand All @@ -288,7 +288,7 @@ test.describe("Out of office", () => {
// add another entry
await page.getByTestId("add_entry_ooo").click();

await page.locator('[id="date"]').click();
await page.locator('[data-testid="date-range"]').click();

await selectToAndFromDates(page, "13", "22");

Expand All @@ -305,9 +305,6 @@ async function saveAndWaitForResponse(page: Page, expectedStatusCode = 200) {
}

async function selectToAndFromDates(page: Page, fromDate: string, toDate: string, isRangeInPast = false) {
// deselects by default selected range start day
await page.locator(`button[name="day"].rdp-day_range_start`).click();

const month = isRangeInPast ? "previous" : "next";

await page.locator(`button[name="${month}-month"]`).click();
Expand Down
19 changes: 11 additions & 8 deletions packages/features/insights/filters/DateSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from "react";

import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { DateRangePicker } from "@calcom/ui";
Expand All @@ -22,10 +23,10 @@ export const DateSelect = () => {
const { filter, setConfigFilters } = useFilterContext();
const currentDate = dayjs();
const [initialStartDate, initialEndDate, range] = filter?.dateRange || [null, null, null];
const [startDate, setStartDate] = useState(initialStartDate);
const [endDate, setEndDate] = useState(initialEndDate);
const startValue = startDate?.toDate() || null;
const endValue = endDate?.toDate() || null;
const [startDate, setStartDate] = useState<Dayjs>(initialStartDate);
const [endDate, setEndDate] = useState<Dayjs | undefined>(initialEndDate);
const startValue = startDate.toDate();
const endValue = endDate?.toDate();
const [selectedPreset, setSelectedPreset] = useState(presetOptions.find((c) => c.value === range));

const updateDateRange = (val: string | null) => {
Expand Down Expand Up @@ -77,11 +78,13 @@ export const DateSelect = () => {
maxDate={currentDate.toDate()}
disabled={false}
onDatesChange={({ startDate, endDate }) => {
setConfigFilters({
dateRange: [dayjs(startDate), dayjs(endDate), null],
});
if (startDate && endDate) {
setConfigFilters({
dateRange: [dayjs(startDate), dayjs(endDate), null],
});
}
setStartDate(dayjs(startDate));
setEndDate(dayjs(endDate));
setEndDate(endDate ? dayjs(endDate) : endDate);
setSelectedPreset(presetOptions.find((c) => c.value === null));
}}
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/components/form/date-range-picker/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function Calendar({
buttonClasses({ color: "minimal" }),
"w-8 h-8 md:h-11 md:w-11 p-0 text-sm font-medium aria-selected:opacity-100 inline-flex items-center justify-center"
),
day_range_end: "day-range-end",
day_range_end: "hover:!bg-inverted hover:!text-inverted",
day_range_start: "hover:!bg-inverted hover:!text-inverted",
day_selected: "bg-inverted text-inverted",
day_today: "",
day_outside: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import * as Popover from "@radix-ui/react-popover";
import { format } from "date-fns";
import * as React from "react";
import type { DateRange } from "react-day-picker";

import { classNames as cn } from "@calcom/lib";

Expand All @@ -26,12 +25,13 @@ export function DatePickerWithRange({
onDatesChange,
disabled,
}: React.HTMLAttributes<HTMLDivElement> & DatePickerWithRangeProps) {
// Even though this is uncontrolled we need to do a bit of logic to improve the UX when selecting dates
function _onDatesChange(onChangeValues: DateRange | undefined) {
if (onChangeValues?.from && !onChangeValues?.to) {
onDatesChange({ startDate: onChangeValues.from, endDate: onChangeValues.from });
function handleDayClick(date: Date) {
if (dates?.endDate) {
onDatesChange({ startDate: date, endDate: undefined });
} else {
onDatesChange({ startDate: onChangeValues?.from, endDate: onChangeValues?.to });
const startDate = date < dates.startDate ? date : dates.startDate;
const endDate = date < dates.startDate ? dates.startDate : date;
onDatesChange({ startDate, endDate });
}
}
const fromDate = minDate ?? new Date();
Expand All @@ -41,7 +41,7 @@ export function DatePickerWithRange({
<Popover.Root>
<Popover.Trigger asChild>
<Button
id="date"
data-testid="date-range"
color="secondary"
EndIcon="calendar"
className={cn("justify-between text-left font-normal", !dates && "text-subtle")}>
Expand All @@ -51,7 +51,7 @@ export function DatePickerWithRange({
{format(dates.startDate, "LLL dd, y")} - {format(dates.endDate, "LLL dd, y")}
</>
) : (
format(dates.startDate, "LLL dd, y")
<>{format(dates.startDate, "LLL dd, y")} - End</>
)
) : (
<span>Pick a date</span>
Expand All @@ -70,7 +70,7 @@ export function DatePickerWithRange({
mode="range"
defaultMonth={dates?.startDate}
selected={{ from: dates?.startDate, to: dates?.endDate }}
onSelect={(values) => _onDatesChange(values)}
onDayClick={(day) => handleDayClick(day)}
numberOfMonths={1}
disabled={disabled}
/>
Expand Down

0 comments on commit 0aa58c1

Please sign in to comment.