Skip to content

Commit deebaae

Browse files
authored
fix(material/datepicker): avoid interfering with external click listener (#21856)
Currently the datepicker toggle's `click` listener is on the inner `button` element and we call `stopPropagation` on it in order to prevent the form field from focusing the input. The problem is that doing so will also prevent any custom `click` handler that the user might have added to the `mat-datepicker-toggle`. These changes resolve the issue by moving the listener directly onto the toggle host which will invoke any external listeners while still allowing us to stop its propagation. Fixes #21836.
1 parent 6f99852 commit deebaae

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/material/datepicker/datepicker-toggle.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
[attr.aria-label]="ariaLabel || _intl.openCalendarLabel"
77
[attr.tabindex]="disabled ? -1 : tabIndex"
88
[disabled]="disabled"
9-
[disableRipple]="disableRipple"
10-
(click)="_open($event)">
9+
[disableRipple]="disableRipple">
1110

1211
<svg
1312
*ngIf="!_customIcon"

src/material/datepicker/datepicker-toggle.ts

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export class MatDatepickerToggleIcon {}
4747
'[class.mat-warn]': 'datepicker && datepicker.color === "warn"',
4848
// Used by the test harness to tie this toggle to its datepicker.
4949
'[attr.data-mat-calendar]': 'datepicker ? datepicker.id : null',
50+
// Bind the `click` on the host, rather than the inner `button`, so that we can call
51+
// `stopPropagation` on it without affecting the user's `click` handlers. We need to stop
52+
// it so that the input doesn't get focused automatically by the form field (See #21836).
53+
'(click)': '_open($event)',
5054
},
5155
exportAs: 'matDatepickerToggle',
5256
encapsulation: ViewEncapsulation.None,

0 commit comments

Comments
 (0)