Skip to content
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

fix(material/timepicker): allow scroll strategy to be customized #30479

Merged
merged 1 commit into from
Feb 12, 2025
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
18 changes: 16 additions & 2 deletions src/material/timepicker/timepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
effect,
ElementRef,
inject,
InjectionToken,
Injector,
input,
InputSignal,
Expand All @@ -41,7 +42,7 @@ import {
MatOptionParentComponent,
} from '@angular/material/core';
import {Directionality} from '@angular/cdk/bidi';
import {Overlay, OverlayRef} from '@angular/cdk/overlay';
import {Overlay, OverlayRef, ScrollStrategy} from '@angular/cdk/overlay';
import {TemplatePortal} from '@angular/cdk/portal';
import {_getEventTarget} from '@angular/cdk/platform';
import {ENTER, ESCAPE, hasModifierKey, TAB} from '@angular/cdk/keycodes';
Expand All @@ -62,6 +63,18 @@ export interface MatTimepickerSelected<D> {
source: MatTimepicker<D>;
}

/** Injection token used to configure the behavior of the timepicker dropdown while scrolling. */
export const MAT_TIMEPICKER_SCROLL_STRATEGY = new InjectionToken<() => ScrollStrategy>(
'MAT_TIMEPICKER_SCROLL_STRATEGY',
{
providedIn: 'root',
factory: () => {
const overlay = inject(Overlay);
return () => overlay.scrollStrategies.reposition();
},
},
);

/**
* Renders out a listbox that can be used to select a time of day.
* Intended to be used together with `MatTimepickerInput`.
Expand Down Expand Up @@ -101,6 +114,7 @@ export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
private _defaultConfig = inject(MAT_TIMEPICKER_CONFIG, {optional: true});
private _dateAdapter = inject<DateAdapter<D>>(DateAdapter, {optional: true})!;
private _dateFormats = inject(MAT_DATE_FORMATS, {optional: true})!;
private _scrollStrategyFactory = inject(MAT_TIMEPICKER_SCROLL_STRATEGY);

private _isOpen = signal(false);
private _activeDescendant = signal<string | null>(null);
Expand Down Expand Up @@ -314,7 +328,7 @@ export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {

this._overlayRef = this._overlay.create({
positionStrategy,
scrollStrategy: this._overlay.scrollStrategies.reposition(),
scrollStrategy: this._scrollStrategyFactory(),
direction: this._dir || 'ltr',
hasBackdrop: false,
});
Expand Down
4 changes: 4 additions & 0 deletions tools/public_api_guard/material/timepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { MatOptionParentComponent } from '@angular/material/core';
import { ModelSignal } from '@angular/core';
import { OnDestroy } from '@angular/core';
import { OutputEmitterRef } from '@angular/core';
import { ScrollStrategy } from '@angular/cdk/overlay';
import { Signal } from '@angular/core';
import { TemplateRef } from '@angular/core';
import { ValidationErrors } from '@angular/forms';
Expand All @@ -25,6 +26,9 @@ import { Validator } from '@angular/forms';
// @public
export const MAT_TIMEPICKER_CONFIG: InjectionToken<MatTimepickerConfig>;

// @public
export const MAT_TIMEPICKER_SCROLL_STRATEGY: InjectionToken<() => ScrollStrategy>;
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason to not just put it as part of the MAT_TIMEPICKER_CONFIG?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly consistency with the rest of the components.


// @public
export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
constructor();
Expand Down
Loading