Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material DatePicker showing calendar below date field

I am trying to implement a basic date picker inside a dialog exactly as described in the documentation but it does not show the calendar icon correctly. Here is how it looks like: datepicker-problem

Here is the html template code for the dialog:

<div class="dialog-header">
  <button mat-icon-button tabindex="-1" (click)="cancel()">
    <mat-icon>close</mat-icon>
  </button>
</div>
<div mat-dialog-content>
  <mat-form-field appearance="fill">
    <mat-label>Choose a date</mat-label>
    <input matInput [matDatepicker]="picker" />
    <mat-hint>MM/DD/YYYY</mat-hint>
    <mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
  </mat-form-field>
</div>

And here is the css code:

:host {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.dialog-header {
  display: flex;
  flex-direction: row;
  margin: -10px -10px 10px;

  & > h1 {
      margin: auto 0;
  }

  & > .mat-icon-button {
      margin-left: auto;
      margin-bottom: auto;
  }
}

.mat-dialog-content {
  width: 100%;
  display: flex;
  max-height: unset;
}

I have already tried to remove almost all CSS of all container components but it does not matter, the datepicker is always displayed with this broken layout. I also have no global styles or anything else that could cause this, so I am completely clueless about what is going on here.

like image 668
Chris Avatar asked Dec 01 '25 13:12

Chris


2 Answers

I just noticed that I was looking at the wrong documentation (15.1.0-next.0). In that version, matIconSuffix was used instead of matSuffix like in previous versions. After changing matIconSuffix to matSuffix in my code, the problem disappeared.

like image 138
Chris Avatar answered Dec 03 '25 03:12

Chris


You can define the placement of the icon by changing the matSuffix directive to the matPrefix directive on the mat-datepicker-toggle, with no additional styling required in the .css file.

See the example I have forked from the docs here.

In your code you seem to be using matIconSuffix, which I didn't find in the current version of the angular-material - if that's not a type, and it is what was being used in an older version, perhaps you might have to change it to matIconPrefix.

like image 25
TotallyNewb Avatar answered Dec 03 '25 03:12

TotallyNewb



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!