Summary - Use
[isDateEnabled]="isBlockedDate"
to calculate blocked dates by passing a date string (eg '2022-08-23')
--
In Ionic 6, the new property isEnabledDate
can be used to block specific dates.
isBlockedDate = (dateString: string) => {
const result = this.current_month_blockout_dates.some((date) => {
let interstitial_date = `${date.years}-${('0' + (date.months)).slice(-2)}-${date.date}`;
// Example: comparing 2022-08-21 with 2022-08-12
return dateString == interstitial_date;
});
if(result === true) {
console.log(`Blocked confirmation for date: ${dateString}`, result);
}
return !result;
}
If you populate the array
this.current_month_blockout_dates
with relevant dates, refer to how the code works above:
this.current_month_blockout_dates = [{years:'2022', months:'10', date:'14'}];
To use, make sure to include
[isDateEnabled]="isBlockedDate"
, like so:
<ion-datetime
#orderDatePicker
id="orderDatePicker"
size="fixed"
(click)="createDateListeners()"
[isDateEnabled]="isBlockedDate"
presentation="date"
max="2025"
[min]="todaysDate">
</ion-datetime>
Extra Tip
You can set up an observer to recalibrate your blocked dates when the month changes:
createDateListeners() {
// Observe Date changes
const self = this;
var previous = '';
const targetNode = document.querySelector('ion-datetime#orderDatePicker');
const config = { attributes: true, childList: true, subtree: true };
const callback = function(mutationsList, observer) {
for(const mutation of mutationsList) {
if (mutation.type === 'attributes') {
var e = document.querySelector('ion-datetime#orderDatePicker').shadowRoot.querySelector('ion-label').textContent;
if(e !== previous)
{
previous = e;
console.log('[Date Listener]: e', e);
let date_interpret = new Date(e);
self.current_month = date_interpret.getMonth()+1;
console.log('[Date Listener]: Current month', self.current_month);
self.current_month_blockout_dates = self.checkMonth(self.current_month);
return;
}
}
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
}
The function I use is called checkMonth
, but feel free to customize it according to your needs. Best of luck.