Search code examples
angulartypescriptflatpickr

How to pass function to disable dates


I'm using fatpickr with the angular wrap https://www.npmjs.com/package/angularx-flatpickr

StackBlitz with the problem: https://stackblitz.com/edit/angular-npep74

Objective: I want to make Saturdays and Sundays appear disabled (The next step is that holidays cannot be selected either)

Problem: I can't find the explanation of how to do it with this angular wrap and doing it like the original flatpickr documentation doesn't work

Official documentation says it should be like this:

 "disable": [
        function(date) {
            // return true to disable
            return (date.getDay() === 0 || date.getDay() === 6);

        }
    ],

Wrapper documentation doesnt say anything

What have I tried ?:

HTML:

<input
               type="text"
               mwlFlatpickr
               [altInput]="true"
               dateFormat="Z"
               [(ngModel)]="desiredDate"
               [disable]="[disabledDay(date)]">

TS:

disabledDay(date) {
    console.log(date); // <--- on this point date is undefined
    return true;
  }

Thanks in advance


Solution

  • You made mistake in your code, your not suppose to pass the function`s execution your suppose to pass a function definition

    instead of

    <input
                   type="text"
                   mwlFlatpickr
                   [altInput]="true"
                   dateFormat="Z"
                   [(ngModel)]="desiredDate"
                   [disable]="[disabledDay(date)]">
    

    change to

    <input
                   type="text"
                   mwlFlatpickr
                   [altInput]="true"
                   dateFormat="Z"
                   [(ngModel)]="desiredDate"
                   [disable]="[disabledDay]">
    

    I forked your blitz checkout the example:

    https://stackblitz.com/edit/angular-u7r7rj