Components: [Feature Request] (Datepicker) should update selected date on input change

Created on 8 Oct 2018  路  8Comments  路  Source: angular/components

Bug, feature request, or proposal:

feature request

What is the expected behavior?

update the highlight date every input change while the calendar open
(or we can create some method like: selectedDate method to update it, or updateDate method to trigger change detection)

What is the current behavior?

image
If I press next day button (update the input value) while the calendar open but still not update the highlight date. only I close and reopen datepicker, it will update value.

What are the steps to reproduce?

I think it simple and clear to understand. tell me if you need example repository.

What is the use-case or motivation for changing an existing behavior?

I have next day, and previous day button, with today button. I want to change date manually but the selected date not update

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

angular 6
material 6
typescript 3
chrome 60

Is there anything else we should know?

no

P4 materiadatepicker

Most helpful comment

image
@jelbourn are you kidding me? why you close my issues and concluded hurriedly?
I'm creating an customize style on that Material calendar, sir

All 8 comments

please, can someone help me?

Your screenshot isn't showing an Angular Material datepicker, so I don't think you have the right library.

image
@jelbourn are you kidding me? why you close my issues and concluded hurriedly?
I'm creating an customize style on that Material calendar, sir

I already try everything, event use changeDetectionRef.detectchanges() to update after have value but nothing happen, If only I close and reopen that calendar, the highlight position will be update

``` .html





``` .ts
import { Component, OnInit, ViewChild, AfterViewInit, Output, EventEmitter, Input, OnChanges, SimpleChanges, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { MAT_DATE_FORMATS, MatDatepickerInputEvent, MatDatepicker } from '@angular/material';
import { FormControl } from '@angular/forms';
export const APP_DATE_FORMATS = {
  parse: {
    dateInput: { month: 'short', year: 'numeric', day: 'numeric', date: 'short' },
  },
  display: {
    dateInput: { month: 'short', year: 'numeric', day: 'numeric', date: 'short' },
    monthYearLabel: { month: 'long', year: 'numeric' },
    dateA11yLabel: 'numeric',
    monthYearA11yLabel: 'numeric',
  }
  // ,
  // weekdays: [
  //   'Mon','Tue','Wed','Thu','Fri','Sat','Sun'
  // ]
};
@Component({
  selector: 'app-dashboar-toolbar-date-picker',
  templateUrl: './dashboar-toolbar-date-picker.component.html',
  styleUrls: ['./dashboar-toolbar-date-picker.component.scss'],
  providers: [
    {
      provide: MAT_DATE_FORMATS, useValue: APP_DATE_FORMATS
    }
  ],
  // changeDetection: ChangeDetectionStrategy.OnPush
})
export class DashboarToolbarDatePickerComponent implements OnInit, AfterViewInit, OnChanges {
  @ViewChild('picker') public picker: MatDatepicker<any>;
  @Output() actionChangeDate: EventEmitter<any> = new EventEmitter;
  @Input() viewDate:any;
  date = new FormControl();
  events: string[] = [];
  constructor(
    public cd: ChangeDetectorRef
  ) { }

  ngOnInit() {
  }

  ngOnChanges(changes: SimpleChanges) {
    this.date = new FormControl(new Date(changes.viewDate.currentValue));

    console.log("date picker changes")
    let dateElement = document.getElementsByTagName('mat-datepicker-content')[0];
    if (dateElement) {
      console.log("ya, we see calendar open")
      this.picker.close();
      setTimeout(() => {
        this.picker.open();
      }, 50);
    }
  }

  ngAfterViewInit() {
    // console.log("after view init")

  }

  addEvent(type: string, event: MatDatepickerInputEvent<Date>) {
    console.log('date event')
    this.events.push(`${type}: ${event.value}`);
  }

}

BTW, I use this function to navigate time:
``` .ts
navigateDate(date = this.viewDate) {
this.itemRouting.dateChange$.next(new Date(date).getTime());
}

then I put it in to the component I commented above:
``` .html 
<app-dashboar-toolbar-date-picker [viewDate]="viewDate" (actionChangeDate)="changeDate($event)" #dateComponent></app-dashboar-toolbar-date-picker>

anything I can provide that could help?

thank you sir, thank you very much @jelbourn

The MatDatepicker has a method select(date), but it looks like it does not trigger change detection in the calendar

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jelbourn picture jelbourn  路  3Comments

shlomiassaf picture shlomiassaf  路  3Comments

Miiekeee picture Miiekeee  路  3Comments

LoganDupont picture LoganDupont  路  3Comments

kara picture kara  路  3Comments