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

Prevent select when disabled dates are in range #526

Closed
Eldzej opened this issue Feb 22, 2023 · 2 comments
Closed

Prevent select when disabled dates are in range #526

Eldzej opened this issue Feb 22, 2023 · 2 comments

Comments

@Eldzej
Copy link

Eldzej commented Feb 22, 2023

This is a possible duplicate of two existing issues ( #348 and #423 ), but those have not seen any activity in 2+ years now (and dating to the jQuery-dependent version)

When using range selection, there should be an option to allow disabled days (with default probably set to false, which is the more common usecase, I imagine). If the option is set to false and disabled dates are in range, the selection should simply not happen, and everything should unselect (or remain on the previous selected range)

For the moment, it is doable with a bit of hacking around (see below, using Day.js library), but it would be nice to have by default in ADP

const disabledDays = ['2023-03-01', '2023-03-02', '2023-03-03', '2023-03-10', '2023-03-16']

onSelect: ({ date, formattedDate, datepicker }) => {
    if (date.length > 1) {
        const dateFormattedFrom = dayjs(date[0]).format('YYYY-MM-DD')
        const dateFormattedTo = dayjs(date[1]).format('YYYY-MM-DD')
        let disabledInRange = false

        disabledDays.forEach(day => {
            if (dayjs(day).isBetween(dateFormattedFrom, dateFormattedTo)) {
                disabledInRange = true
            }
        })

        if (disabledInRange) {
            datepicker.clear()
            ;[...datepicker.$datepicker.querySelectorAll('.-in-range-')].forEach(day => day.classList.remove('-in-range-'))
        }
    }
}
@t1m0n
Copy link
Owner

t1m0n commented Mar 2, 2023

Hmm, honestly this doesn't seem like a must have native feature. It is vary from user's needs, for instance, how many disabled dates should be in range to consider it as a disabled, should calendar disable range or just show tooltip with warning, should it disable range when user focuses on cell or when he selects one, etc.

I think I will add onFocus event or smth similar to gain more control, then user can create any behaviour that he wants.

t1m0n added a commit that referenced this issue Jul 24, 2023
@t1m0n
Copy link
Owner

t1m0n commented Jul 24, 2023

@Eldzej added some new options which grant you more control over range behaviour. Added example of disabled range under Prohibiting range selection section https://air-datepicker.com/examples

@t1m0n t1m0n closed this as completed Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants