Angular 10: Date range picker - calender opens at wrong place

Viewed 1745

I wanted to use the new Date picker in Angular 10.

I upgraded Angular successfully to Angular 10. Then I copied the html code from the first example here.

It should look like this with the calender pop up nicely placed:

from the example website

(when you click on the link to the example, maybe you have to scroll up to find the first example. If you click on the right <>you can see the code behind the example.)

I copied the html code from the example into one of my components, it now looks like this (tried it in Chrome & Firefox):

my website

The black bar is the sidebar of my page, so that has nothing to do with the dat-picker. But of course the pop up should be placed more to the right and further down. Also it looks very different to the example.

In the example they don't use any additional CSS, why does my pop-up look so differently? And why does it always get placed so badly, can I change that?

The basic function is working, you can pick a date. But it just looks very bad.

I deleted all my CSS and Bootstrap and other stylesheet links, but then it only looked worse:

my website without css & bootstrap

Now the calender pop-up is placed even worse with a huge gap in between.

But as I said, in the example it was enough to use the html code, no css was needed. What am I doing wrong?

This is the html code from the example that I copied:

<mat-form-field appearance="fill">
      <mat-label>Choose a date</mat-label>
      <input matInput [matDatepicker]="picker">
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
      <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>

and this is the typescript code from the example (I didn't change the ts code of my component, since I don't see that there is anything special done here:)

import {Component} from '@angular/core';

/** @title Basic datepicker */
@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html',
  styleUrls: ['datepicker-overview-example.css'],
})
export class DatepickerOverviewExample {}

Also, to make this even run on my page I had to install @angular/cdkand @angular/materialand then import a lot of stuff into my app.module.ts file. I don't know if that's normal? Without these imports it wouldn't even start, because there were a lot of errors thrown from the datepicker html code.

import {A11yModule} from '@angular/cdk/a11y';
import {ClipboardModule} from '@angular/cdk/clipboard';
import {DragDropModule} from '@angular/cdk/drag-drop';
import {PortalModule} from '@angular/cdk/portal';
import {ScrollingModule} from '@angular/cdk/scrolling';
import {CdkStepperModule} from '@angular/cdk/stepper';
import {CdkTableModule} from '@angular/cdk/table';
import {CdkTreeModule} from '@angular/cdk/tree';
import {MatBadgeModule} from '@angular/material/badge';
import {MatBottomSheetModule} from '@angular/material/bottom-sheet';
import {MatButtonModule} from '@angular/material/button';
import {MatButtonToggleModule} from '@angular/material/button-toggle';
import {MatCardModule} from '@angular/material/card';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {MatChipsModule} from '@angular/material/chips';
import {MatStepperModule} from '@angular/material/stepper';
import {MatDatepickerModule} from '@angular/material/datepicker';
import {MatDialogModule} from '@angular/material/dialog';
import {MatDividerModule} from '@angular/material/divider';
import {MatExpansionModule} from '@angular/material/expansion';
import {MatGridListModule} from '@angular/material/grid-list';
import {MatIconModule} from '@angular/material/icon';
import {MatInputModule} from '@angular/material/input';
import {MatListModule} from '@angular/material/list';
import {MatMenuModule} from '@angular/material/menu';
import {MatNativeDateModule, MatRippleModule} from '@angular/material/core';
import {MatPaginatorModule} from '@angular/material/paginator';
import {MatProgressBarModule} from '@angular/material/progress-bar';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {MatRadioModule} from '@angular/material/radio';
import {MatSelectModule} from '@angular/material/select';
import {MatSidenavModule} from '@angular/material/sidenav';
import {MatSliderModule} from '@angular/material/slider';
import {MatSlideToggleModule} from '@angular/material/slide-toggle';
import {MatSnackBarModule} from '@angular/material/snack-bar';
import {MatSortModule} from '@angular/material/sort';
import {MatTableModule} from '@angular/material/table';
import {MatTabsModule} from '@angular/material/tabs';
import {MatToolbarModule} from '@angular/material/toolbar';
import {MatTooltipModule} from '@angular/material/tooltip';
import {MatTreeModule} from '@angular/material/tree';

HttpClientModule
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
A11yModule,
ClipboardModule,
CdkStepperModule,
CdkTableModule,
CdkTreeModule,
DragDropModule,
MatBadgeModule,
MatBottomSheetModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
MatCheckboxModule,
MatChipsModule,
MatStepperModule,
MatDatepickerModule,
MatDialogModule,
MatDividerModule,
MatExpansionModule,
MatGridListModule,
MatIconModule,
MatInputModule,
MatListModule,
MatMenuModule,
MatNativeDateModule,
MatPaginatorModule,
MatProgressBarModule,
MatProgressSpinnerModule,
MatRadioModule,
MatRippleModule,
MatSelectModule,
MatSidenavModule,
MatSliderModule,
MatSlideToggleModule,
MatSnackBarModule,
MatSortModule,
MatTableModule,
MatTabsModule,
MatToolbarModule,
MatTooltipModule,
MatTreeModule,
PortalModule,
ScrollingModule
]

In the tutorial page where I found the first example, there was nothing about these necessary imports. I only found them here in this stackblitz example.

Why all these imports and am I importing them correctly? sorry for the long question btw!

2 Answers

Ok I should have read through the Angular Material getting started guide first :D I tried to follow the example on their page and therefore ran the command:

ng add @angular/material

This was the missing thing, now it looks like it's supposed to:

my page

EDIT: I solved my original problem, but I'm still wondering about all these impports? Any comment on this is appreciated.

Two things to solve your problem:

  1. As you already mentioned, @angular/material needs to be added to make use of all the components in Angular Material and add default styling.
  2. These are the only imports in material-module.ts that are needed for your date range selector:
import {NgModule} from '@angular/core';
import {MatInputModule} from '@angular/material/input';
import {MatDatepickerModule} from '@angular/material/datepicker';

@NgModule({
  exports: [
    MatDatepickerModule,
    MatInputModule
  ]
})
export class DemoMaterialModule {}

This is because the date range is wrapped inside a mat-form-field input, therefore you need to import this particular module. The rest of the imports are just for demo purposes and not needed for your case. See this updated StackBlitz with the updated Material module.

Related