Skip to content

Commit 355233d

Browse files
authored
fix(material/datepicker): support for date range input in MDC-based form field (#22565)
Resolves a few issues that were preventing the use of the range picker with an MDC-based `mat-form-field`. Including: 1. Conditionally setting the `mat-mdc-input-element` class so that the input is sized correctly. 2. Triggering a state change event when the focus state changes so that the floating label is up-to-date. 3. Fixing an error being thrown inside `_shouldHideSeparator`.
1 parent 75cbb16 commit 355233d

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/material/datepicker/date-range-input.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div
22
class="mat-date-range-input-container"
33
cdkMonitorSubtreeFocus
4-
(cdkFocusChange)="focused = $event !== null">
4+
(cdkFocusChange)="_updateFocus($event)">
55
<div class="mat-date-range-input-start-wrapper">
66
<ng-content select="input[matStartDate]"></ng-content>
77
<span

src/material/datepicker/date-range-input.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {MatFormFieldControl, MatFormField, MAT_FORM_FIELD} from '@angular/materi
2626
import {ThemePalette, DateAdapter} from '@angular/material/core';
2727
import {NgControl, ControlContainer} from '@angular/forms';
2828
import {Subject, merge, Subscription} from 'rxjs';
29+
import {FocusOrigin} from '@angular/cdk/a11y';
2930
import {coerceBooleanProperty, BooleanInput} from '@angular/cdk/coercion';
3031
import {
3132
MatStartDate,
@@ -243,6 +244,12 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
243244
throw createMissingDateImplError('DateAdapter');
244245
}
245246

247+
// The datepicker module can be used both with MDC and non-MDC form fields. We have
248+
// to conditionally add the MDC input class so that the range picker looks correctly.
249+
if (_formField?._elementRef.nativeElement.classList.contains('mat-mdc-form-field')) {
250+
_elementRef.nativeElement.classList.add('mat-mdc-input-element');
251+
}
252+
246253
// TODO(crisbeto): remove `as any` after #18206 lands.
247254
this.ngControl = control as any;
248255
}
@@ -342,7 +349,8 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
342349

343350
/** Whether the separate text should be hidden. */
344351
_shouldHideSeparator() {
345-
return (!this._formField || this._formField._hideControlPlaceholder()) && this.empty;
352+
return (!this._formField || (this._formField.getLabelId() &&
353+
!this._formField._shouldLabelFloat())) && this.empty;
346354
}
347355

348356
/** Gets the value for the `aria-labelledby` attribute of the inputs. */
@@ -351,6 +359,12 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
351359
return formField && formField._hasFloatingLabel() ? formField._labelId : null;
352360
}
353361

362+
/** Updates the focused state of the range input. */
363+
_updateFocus(origin: FocusOrigin) {
364+
this.focused = origin !== null;
365+
this.stateChanges.next();
366+
}
367+
354368
/** Re-runs the validators on the start/end inputs. */
355369
private _revalidate() {
356370
if (this._startInput) {

tools/public_api_guard/material/datepicker.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ export declare class MatDateRangeInput<D> implements MatFormFieldControl<DateRan
356356
_handleChildValueChange(): void;
357357
_openDatepicker(): void;
358358
_shouldHidePlaceholders(): boolean;
359-
_shouldHideSeparator(): boolean;
359+
_shouldHideSeparator(): boolean | "" | null;
360+
_updateFocus(origin: FocusOrigin): void;
360361
getConnectedOverlayOrigin(): ElementRef;
361362
getStartValue(): D | null;
362363
getThemePalette(): ThemePalette;

0 commit comments

Comments
 (0)