Skip to content

fix(material/timepicker): allow timepicker to opt out of opening on click #31492

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
Jul 10, 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
3 changes: 2 additions & 1 deletion goldens/material/timepicker/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class MatTimepickerInput<D> implements ControlValueAccessor, Validator, O
readonly min: InputSignalWithTransform<D | null, unknown>;
// (undocumented)
ngOnDestroy(): void;
readonly openOnClick: InputSignalWithTransform<boolean, unknown>;
registerOnChange(fn: (value: any) => void): void;
registerOnTouched(fn: () => void): void;
registerOnValidatorChange(fn: () => void): void;
Expand All @@ -103,7 +104,7 @@ export class MatTimepickerInput<D> implements ControlValueAccessor, Validator, O
readonly value: ModelSignal<D | null>;
writeValue(value: any): void;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<MatTimepickerInput<any>, "input[matTimepicker]", ["matTimepickerInput"], { "value": { "alias": "value"; "required": false; "isSignal": true; }; "timepicker": { "alias": "matTimepicker"; "required": true; "isSignal": true; }; "min": { "alias": "matTimepickerMin"; "required": false; "isSignal": true; }; "max": { "alias": "matTimepickerMax"; "required": false; "isSignal": true; }; "disabledInput": { "alias": "disabled"; "required": false; "isSignal": true; }; }, { "value": "valueChange"; }, never, never, true, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<MatTimepickerInput<any>, "input[matTimepicker]", ["matTimepickerInput"], { "value": { "alias": "value"; "required": false; "isSignal": true; }; "timepicker": { "alias": "matTimepicker"; "required": true; "isSignal": true; }; "min": { "alias": "matTimepickerMin"; "required": false; "isSignal": true; }; "max": { "alias": "matTimepickerMax"; "required": false; "isSignal": true; }; "openOnClick": { "alias": "matTimepickerOpenOnClick"; "required": false; "isSignal": true; }; "disabledInput": { "alias": "disabled"; "required": false; "isSignal": true; }; }, { "value": "valueChange"; }, never, never, true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatTimepickerInput<any>, never>;
}
Expand Down
12 changes: 11 additions & 1 deletion src/material/timepicker/timepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ export class MatTimepickerInput<D> implements ControlValueAccessor, Validator, O
transform: (value: unknown) => this._transformDateInput<D>(value),
});

/**
* Whether to open the timepicker overlay when clicking on the input. Enabled by default.
* Note that when disabling this option, you'll have to provide your own logic for opening
* the overlay.
*/
readonly openOnClick: InputSignalWithTransform<boolean, unknown> = input(true, {
alias: 'matTimepickerOpenOnClick',
transform: booleanAttribute,
});

/** Whether the input is disabled. */
readonly disabled: Signal<boolean> = computed(
() => this.disabledInput() || this._accessorDisabled(),
Expand Down Expand Up @@ -254,7 +264,7 @@ export class MatTimepickerInput<D> implements ControlValueAccessor, Validator, O

/** Handles clicks on the input or the containing form field. */
private _handleClick = (): void => {
if (!this.disabled()) {
if (!this.disabled() && this.openOnClick()) {
this.timepicker().open();
}
};
Expand Down
11 changes: 11 additions & 0 deletions src/material/timepicker/timepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,15 @@ describe('MatTimepicker', () => {
fixture.detectChanges();
expect(getPanel()).toBeTruthy();
}));

it('should be able to opt out of opening on click', () => {
const fixture = TestBed.createComponent(StandaloneTimepicker);
fixture.componentInstance.openOnClick.set(false);
fixture.detectChanges();
getInput(fixture).click();
fixture.detectChanges();
expect(getPanel()).toBeFalsy();
});
});

// Note: these tests intentionally don't cover the full option generation logic
Expand Down Expand Up @@ -1313,6 +1322,7 @@ describe('MatTimepicker', () => {
[disabled]="disabled()"
[matTimepickerMin]="min()"
[matTimepickerMax]="max()"
[matTimepickerOpenOnClick]="openOnClick()"
[value]="value()"/>
<mat-timepicker
#picker
Expand Down Expand Up @@ -1345,6 +1355,7 @@ class StandaloneTimepicker {
readonly toggleDisabled = signal<boolean>(false);
readonly toggleTabIndex = signal<number>(0);
readonly customOptions = signal<MatTimepickerOption<Date>[] | null>(null);
readonly openOnClick = signal(true);
readonly openedSpy = jasmine.createSpy('opened');
readonly closedSpy = jasmine.createSpy('closed');
readonly selectedSpy = jasmine.createSpy('selected');
Expand Down
Loading