Skip to content

feat(datepicker): allow for the dropdown position to be customized #16698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/material/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MAT_DIALOG_DEFAULT_OPTIONS, MatDialogConfig} from '@angular/material/dialog';
import {Subject} from 'rxjs';
import {MatInputModule} from '../input/index';
import {MatDatepicker} from './datepicker';
import {
MatDatepicker,
DatepickerDropdownPositionX,
DatepickerDropdownPositionY,
} from './datepicker';
import {MatDatepickerInput} from './datepicker-input';
import {MatDatepickerToggle} from './datepicker-toggle';
import {MAT_DATEPICKER_SCROLL_STRATEGY, MatDatepickerIntl, MatDatepickerModule} from './index';
Expand Down Expand Up @@ -1689,6 +1693,36 @@ describe('MatDatepicker', () => {
.toBe(Math.floor(inputRect.right), 'Expected popup to align to input right.');
});

it('should be able to customize the calendar position along the X axis', () => {
input.style.top = input.style.left = '200px';
testComponent.xPosition = 'end';
fixture.detectChanges();

testComponent.datepicker.open();
fixture.detectChanges();

const overlayRect = document.querySelector('.cdk-overlay-pane')!.getBoundingClientRect();
const inputRect = input.getBoundingClientRect();

expect(Math.floor(overlayRect.right))
.toBe(Math.floor(inputRect.right), 'Expected popup to align to input right.');
});

it('should be able to customize the calendar position along the Y axis', () => {
input.style.bottom = input.style.left = '100px';
testComponent.yPosition = 'above';
fixture.detectChanges();

testComponent.datepicker.open();
fixture.detectChanges();

const overlayRect = document.querySelector('.cdk-overlay-pane')!.getBoundingClientRect();
const inputRect = input.getBoundingClientRect();

expect(Math.floor(overlayRect.bottom))
.toBe(Math.floor(inputRect.top), 'Expected popup to align to input top.');
});

});

describe('internationalization', () => {
Expand Down Expand Up @@ -1786,7 +1820,13 @@ describe('MatDatepicker', () => {
@Component({
template: `
<input [matDatepicker]="d" [value]="date">
<mat-datepicker #d [touchUi]="touch" [disabled]="disabled" [opened]="opened"></mat-datepicker>
<mat-datepicker
#d
[touchUi]="touch"
[disabled]="disabled"
[opened]="opened"
[xPosition]="xPosition"
[yPosition]="yPosition"></mat-datepicker>
`,
})
class StandardDatepicker {
Expand All @@ -1796,6 +1836,8 @@ class StandardDatepicker {
date: Date | null = new Date(2020, JAN, 1);
@ViewChild('d') datepicker: MatDatepicker<Date>;
@ViewChild(MatDatepickerInput) datepickerInput: MatDatepickerInput<Date>;
xPosition: DatepickerDropdownPositionX;
yPosition: DatepickerDropdownPositionY;
}


Expand Down
109 changes: 72 additions & 37 deletions src/material/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
Overlay,
OverlayConfig,
OverlayRef,
PositionStrategy,
ScrollStrategy,
FlexibleConnectedPositionStrategy,
} from '@angular/cdk/overlay';
import {ComponentPortal, ComponentType} from '@angular/cdk/portal';
import {DOCUMENT} from '@angular/common';
Expand All @@ -36,6 +36,8 @@ import {
ViewContainerRef,
ViewEncapsulation,
ChangeDetectorRef,
OnChanges,
SimpleChanges,
} from '@angular/core';
import {
CanColor,
Expand Down Expand Up @@ -72,6 +74,12 @@ export const MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER = {
useFactory: MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY,
};

/** Possible positions for the datepicker dropdown along the X axis. */
export type DatepickerDropdownPositionX = 'start' | 'end';

/** Possible positions for the datepicker dropdown along the Y axis. */
export type DatepickerDropdownPositionY = 'above' | 'below';

// Boilerplate for applying mixins to MatDatepickerContent.
/** @docs-private */
class MatDatepickerContentBase {
Expand Down Expand Up @@ -164,7 +172,7 @@ export class MatDatepickerContent<D> extends _MatDatepickerContentMixinBase
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class MatDatepicker<D> implements OnDestroy, CanColor {
export class MatDatepicker<D> implements OnDestroy, CanColor, OnChanges {
private _scrollStrategy: () => ScrollStrategy;

/** An input indicating the type of the custom header component for the calendar, if set. */
Expand Down Expand Up @@ -223,6 +231,14 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
}
private _disabled: boolean;

/** Preferred position of the datepicker in the X axis. */
@Input()
xPosition: DatepickerDropdownPositionX = 'start';

/** Preferred position of the datepicker in the Y axis. */
@Input()
yPosition: DatepickerDropdownPositionY = 'below';

/**
* Emits selected year in multiyear view.
* This doesn't imply a change on the selected date.
Expand Down Expand Up @@ -315,6 +331,19 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
this._scrollStrategy = scrollStrategy;
}

ngOnChanges(changes: SimpleChanges) {
const positionChange = changes['xPosition'] || changes['yPosition'];

if (positionChange && !positionChange.firstChange && this._popupRef) {
this._setConnectedPositions(
this._popupRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy);

if (this.opened) {
this._popupRef.updatePosition();
}
}
}

ngOnDestroy() {
this._destroyPopup();
this.close();
Expand Down Expand Up @@ -464,8 +493,15 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {

/** Create the popup. */
private _createPopup(): void {
const positionStrategy = this._overlay.position()
.flexibleConnectedTo(this._datepickerInput.getConnectedOverlayOrigin())
.withTransformOriginOn('.mat-datepicker-content')
.withFlexibleDimensions(false)
.withViewportMargin(8)
.withLockedPosition();

const overlayConfig = new OverlayConfig({
positionStrategy: this._createPopupPositionStrategy(),
positionStrategy: this._setConnectedPositions(positionStrategy),
hasBackdrop: true,
backdropClass: 'mat-overlay-transparent-backdrop',
direction: this._dir,
Expand Down Expand Up @@ -501,40 +537,39 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
}
}

/** Create the popup PositionStrategy. */
private _createPopupPositionStrategy(): PositionStrategy {
return this._overlay.position()
.flexibleConnectedTo(this._datepickerInput.getConnectedOverlayOrigin())
.withTransformOriginOn('.mat-datepicker-content')
.withFlexibleDimensions(false)
.withViewportMargin(8)
.withLockedPosition()
.withPositions([
{
originX: 'start',
originY: 'bottom',
overlayX: 'start',
overlayY: 'top'
},
{
originX: 'start',
originY: 'top',
overlayX: 'start',
overlayY: 'bottom'
},
{
originX: 'end',
originY: 'bottom',
overlayX: 'end',
overlayY: 'top'
},
{
originX: 'end',
originY: 'top',
overlayX: 'end',
overlayY: 'bottom'
}
]);
/** Sets the positions of the datepicker in dropdown mode based on the current configuration. */
private _setConnectedPositions(strategy: FlexibleConnectedPositionStrategy) {
const primaryX = this.xPosition === 'end' ? 'end' : 'start';
const secondaryX = primaryX === 'start' ? 'end' : 'start';
const primaryY = this.yPosition === 'above' ? 'bottom' : 'top';
const secondaryY = primaryY === 'top' ? 'bottom' : 'top';

return strategy.withPositions([
Copy link

@SERGEES SERGEES Aug 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crisbeto maybe you could allow to pass ConnectedPosition so any consumer had full controll if needed.
As sometimes its realy needed to add offsets or class to overlay
And lines 509-512 would go away

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing that feels like we'd give people the ability to shoot themselves in the foot since they could pass in a configuration that makes the datepicker harder to use.

{
originX: primaryX,
originY: secondaryY,
overlayX: primaryX,
overlayY: primaryY
},
{
originX: primaryX,
originY: primaryY,
overlayX: primaryX,
overlayY: secondaryY
},
{
originX: secondaryX,
originY: secondaryY,
overlayX: secondaryX,
overlayY: primaryY
},
{
originX: secondaryX,
originY: primaryY,
overlayX: secondaryX,
overlayY: secondaryY
}
]);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions tools/public_api_guard/material/datepicker.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export declare type DatepickerDropdownPositionX = 'start' | 'end';

export declare type DatepickerDropdownPositionY = 'above' | 'below';

export declare const MAT_DATEPICKER_SCROLL_STRATEGY: InjectionToken<() => ScrollStrategy>;

export declare function MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () => ScrollStrategy;
Expand Down Expand Up @@ -107,7 +111,7 @@ export declare class MatCalendarHeader<D> {

export declare type MatCalendarView = 'month' | 'year' | 'multi-year';

export declare class MatDatepicker<D> implements OnDestroy, CanColor {
export declare class MatDatepicker<D> implements OnDestroy, CanColor, OnChanges {
_color: ThemePalette;
get _dateFilter(): (date: D | null) => boolean;
_datepickerInput: MatDatepickerInput<D>;
Expand Down Expand Up @@ -135,18 +139,21 @@ export declare class MatDatepicker<D> implements OnDestroy, CanColor {
startView: 'month' | 'year' | 'multi-year';
get touchUi(): boolean;
set touchUi(value: boolean);
xPosition: DatepickerDropdownPositionX;
yPosition: DatepickerDropdownPositionY;
readonly yearSelected: EventEmitter<D>;
constructor(_dialog: MatDialog, _overlay: Overlay, _ngZone: NgZone, _viewContainerRef: ViewContainerRef, scrollStrategy: any, _dateAdapter: DateAdapter<D>, _dir: Directionality, _document: any);
_registerInput(input: MatDatepickerInput<D>): void;
_selectMonth(normalizedMonth: D): void;
_selectYear(normalizedYear: D): void;
close(): void;
ngOnChanges(changes: SimpleChanges): void;
ngOnDestroy(): void;
open(): void;
select(date: D): void;
static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_touchUi: BooleanInput;
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatDatepicker<any>, "mat-datepicker", ["matDatepicker"], { "calendarHeaderComponent": "calendarHeaderComponent"; "startAt": "startAt"; "startView": "startView"; "color": "color"; "touchUi": "touchUi"; "disabled": "disabled"; "panelClass": "panelClass"; "dateClass": "dateClass"; "opened": "opened"; }, { "yearSelected": "yearSelected"; "monthSelected": "monthSelected"; "openedStream": "opened"; "closedStream": "closed"; }, never, never>;
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatDatepicker<any>, "mat-datepicker", ["matDatepicker"], { "calendarHeaderComponent": "calendarHeaderComponent"; "startAt": "startAt"; "startView": "startView"; "color": "color"; "touchUi": "touchUi"; "disabled": "disabled"; "xPosition": "xPosition"; "yPosition": "yPosition"; "panelClass": "panelClass"; "dateClass": "dateClass"; "opened": "opened"; }, { "yearSelected": "yearSelected"; "monthSelected": "monthSelected"; "openedStream": "opened"; "closedStream": "closed"; }, never, never>;
static ɵfac: i0.ɵɵFactoryDef<MatDatepicker<any>, [null, null, null, null, null, { optional: true; }, { optional: true; }, { optional: true; }]>;
}

Expand Down