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

Allow disabling of specific dates in Date Picker #8415

Closed
melohagan opened this issue Oct 27, 2022 · 4 comments
Closed

Allow disabling of specific dates in Date Picker #8415

melohagan opened this issue Oct 27, 2022 · 4 comments
Labels
enhancement New feature or request medium-term

Comments

@melohagan
Copy link
Collaborator

Describe the feature request
As a user, I want to specify a list of dates that should be disabled in the date picker.
Related discussion: #8402

https://flatpickr.js.org/examples/#disabling-specific-dates

@melohagan melohagan added the enhancement New feature or request label Oct 27, 2022
@melohagan melohagan self-assigned this Dec 15, 2022
@melohagan melohagan removed their assignment Jan 20, 2023
@stale
Copy link

stale bot commented Mar 25, 2023

This issue has been automatically marked as stale because it has not had recent activity.

@stale stale bot added the stale label Mar 25, 2023
@stale stale bot removed the stale label Apr 27, 2023
@daanvdberg12
Copy link

Is this feature still on the roadmap?

@melohagan
Copy link
Collaborator Author

Hey @daanvdberg12 at some stage we are going to replace the current date picker component, but there is no timeline on this for now.

Closing in favour of: #11894

@melohagan melohagan closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2023
@Gtchik
Copy link

Gtchik commented Dec 20, 2024

Hey,

I really needed this feature in my project, and since it isn't currently supported natively, I came up with a workaround inspired by the discussion mentioned here #6839

Admittedly, the solution is not the cleanest, but it gets the job done for my use case. Here's the code I integrated into an embed block:
image

<img style="display: none;" src onerror="
  if (document.readyState === 'complete' || document.readyState === 'interactive') {
    initializeObserver();
  } else {
    document.addEventListener('DOMContentLoaded', initializeObserver);
  }

  function initializeObserver() {
    const targetDates = ['Friday, December 13, 2024', 'Tuesday, December 17, 2024'];  // List of dates to disable

    const observer = new MutationObserver(() => {
      // Detect DOM mutations and reapply disabled dates
      resetDisabledDates();
      disableDates(targetDates);
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true, // Observe child elements as well
    });

    // Apply disabled dates on initial load
    resetDisabledDates();
    disableDates(targetDates);
  }

  function disableDates(targetDates) {
    targetDates.forEach((targetTitle) => {
      const cells = document.querySelectorAll(`td[title='${targetTitle}']`);

      cells.forEach((cell) => {
        if (!cell.classList.contains('disabled-date')) {
          // Disable the cell by adding a custom class and styles
          cell.classList.add('disabled-date');
          cell.setAttribute('aria-disabled', 'true');
          cell.style.opacity = '0.5';  // Make the cell visually faded
          cell.style.pointerEvents = 'none';  // Disable user interactions

          // Strike through the date text
          const dateSpan = cell.querySelector('.spectrum-Calendar-date');
          if (dateSpan) {
            dateSpan.style.textDecoration = 'line-through';
          }
        }
      });
    });
  }

  function resetDisabledDates() {
    // Reset all previously disabled cells by removing styles and attributes
    const disabledCells = document.querySelectorAll('td.disabled-date');
    disabledCells.forEach((cell) => {
      // Reactivate the cell by removing custom styles and attributes
      cell.classList.remove('disabled-date');
      cell.removeAttribute('aria-disabled');
      cell.style.opacity = '';  // Remove the opacity
      cell.style.pointerEvents = '';  // Reactivate user interactions

      // Remove the strike-through text decoration
      const dateSpan = cell.querySelector('.spectrum-Calendar-date');
      if (dateSpan) {
        dateSpan.style.textDecoration = '';
      }
    });
  }
">

This script listens for DOM changes, locates cells with specific date titles, and applies a "disabled" effect to those cells. The key steps are:

  • Cloning the cell to remove existing event listeners.
  • Disabling user interactions and visually dimming the cell.
  • Adding a strikethrough effect to indicate the cell's state.

I hope this helps anyone else facing a similar issue. Feedback is welcome if there's a better way to achieve this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request medium-term
Projects
None yet
Development

No branches or pull requests

4 participants