Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close calender when date is selected using Angular material Datepicker when using mat-datepicker-actions

I'm using a normal Angular Material Datepicker (Angular 13), but I added some action buttons:

  • one to clear the date
  • one to set the date to 'infinity'
  • the ok button (which I want to get rid of)

See my example: https://stackblitz.com/edit/angular-material-13-starter-x1xj4z-wzqxfv?file=src%2Fapp%2Fapp.component.html

The clear button and 'infinity' button work fine. Once clicked, the date is cleared or set and the popup closes. Perfect!

However, selecting a date works only by selecting the date and pressing the ok button.

What I want: Selecting the date closes immediately the popup while setting the date. Just like the behaviour there exists without mat-datepicker-actions.

Any one knows how I can achieve this?

like image 615
Pieter Degraeuwe Avatar asked Oct 19 '25 20:10

Pieter Degraeuwe


1 Answers

This shouldn't be the answer, but it works:

@ViewChild(MatDatepicker) picker: MatDatepicker<any>;

ngAfterViewInit() {
    this.picker.openedStream.subscribe((event) => {
        setTimeout(() => {
            this.picker['_componentRef'].instance._calendar._userSelection.subscribe((event) => {
                console.log('Element selected')
            })
        },0)
    });
}
like image 183
Frederik D Avatar answered Oct 21 '25 10:10

Frederik D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!