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

feat: add disabled dates list or callback prop #102

Merged
merged 3 commits into from
Sep 15, 2024

Conversation

mitri-dvp
Copy link
Contributor

Feature: Disable Custom Dates

In support of adding a feature that allows disabling custom dates I've added a prop disabledDates that accepts an array of dates (DateType[]) or a validation function ((date: dayjs.Dayjs) => boolean)

This change is very similar to JHPG PR but with the added compatibility for the disabled dates list inspired by the handling found in Stephy CalendarPicker project and adapted to use existing functionality like the areDatesOnSameDay method

An example of both was added to the example app App.tsx.

Demo (included in the example app comments) - disable weekends

@timdimas
Copy link

timdimas commented Aug 9, 2024

@mitri-dvp I noticed a small bug in your code where if you had minDate or maxDate they wouldn't work and they would be overlapped by disabledDates here is a fix for that:

  if (minDate) {
    disabled = disabled || date < getDate(minDate);
  }
  if (maxDate) {
    disabled = disabled || date > getDate(maxDate);
  }
  if (disabledDates) {
    if (Array.isArray(disabledDates)) {
      const dates = disabledDates.filter((disabledDate) =>
        areDatesOnSameDay(date, disabledDate)
      );
      disabled = disabled || dates.length > 0;
    } else if (disabledDates instanceof Function) {
      disabled = disabled || disabledDates(date);
    }
  }

@mitri-dvp
Copy link
Contributor Author

@timdimas Good catch, I'll update the PR with this fix. Since it's getting a little messy I'll refactor it into a new isDateDisabled helper method, similar to the other ones.

@timdimas
Copy link

timdimas commented Aug 15, 2024

@mitri-dvp Great stuff, also i have been thinking, if you have the mode="range" and you put a range before and after the disabled dates, the disabled dates will be selected even thought they are disabled. Even thought it's a simple fix on the onChange function where you check for your disabled dates and if they overlap return null, i feel like this logic should be applied automatically, when you have disabled dates. I will look into in more and try to help you.

    interface DateRange {
        startDate: DateType;
        endDate: DateType;
    }

    function checkOverlap(range: DateRange, disabledDates: Date[]): boolean {
        const start = dayjs(range.startDate);
        const end = dayjs(range.endDate);

        return disabledDates.some((date) => {
            const disabledDate = dayjs(date);
            return disabledDate <= end && disabledDate >= start;
        });
    }

the code should look like this

Copy link
Owner

@farhoudshapouran farhoudshapouran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach covers previous PR's 👍🏻

@farhoudshapouran farhoudshapouran merged commit f2e47fb into farhoudshapouran:main Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants