Skip to content

fix(material/datepicker): support for date range input in MDC-based form field #22565

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 27, 2021
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
2 changes: 1 addition & 1 deletion src/material/datepicker/date-range-input.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div
class="mat-date-range-input-container"
cdkMonitorSubtreeFocus
(cdkFocusChange)="focused = $event !== null">
(cdkFocusChange)="_updateFocus($event)">
<div class="mat-date-range-input-start-wrapper">
<ng-content select="input[matStartDate]"></ng-content>
<span
Expand Down
16 changes: 15 additions & 1 deletion src/material/datepicker/date-range-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {MatFormFieldControl, MatFormField, MAT_FORM_FIELD} from '@angular/materi
import {ThemePalette, DateAdapter} from '@angular/material/core';
import {NgControl, ControlContainer} from '@angular/forms';
import {Subject, merge, Subscription} from 'rxjs';
import {FocusOrigin} from '@angular/cdk/a11y';
import {coerceBooleanProperty, BooleanInput} from '@angular/cdk/coercion';
import {
MatStartDate,
Expand Down Expand Up @@ -243,6 +244,12 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
throw createMissingDateImplError('DateAdapter');
}

// The datepicker module can be used both with MDC and non-MDC form fields. We have
// to conditionally add the MDC input class so that the range picker looks correctly.
if (_formField?._elementRef.nativeElement.classList.contains('mat-mdc-form-field')) {
_elementRef.nativeElement.classList.add('mat-mdc-input-element');
}

// TODO(crisbeto): remove `as any` after #18206 lands.
this.ngControl = control as any;
}
Expand Down Expand Up @@ -342,7 +349,8 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,

/** Whether the separate text should be hidden. */
_shouldHideSeparator() {
return (!this._formField || this._formField._hideControlPlaceholder()) && this.empty;
return (!this._formField || (this._formField.getLabelId() &&
!this._formField._shouldLabelFloat())) && this.empty;
}

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

/** Updates the focused state of the range input. */
_updateFocus(origin: FocusOrigin) {
this.focused = origin !== null;
this.stateChanges.next();
}

/** Re-runs the validators on the start/end inputs. */
private _revalidate() {
if (this._startInput) {
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/datepicker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ export declare class MatDateRangeInput<D> implements MatFormFieldControl<DateRan
_handleChildValueChange(): void;
_openDatepicker(): void;
_shouldHidePlaceholders(): boolean;
_shouldHideSeparator(): boolean;
_shouldHideSeparator(): boolean | "" | null;
_updateFocus(origin: FocusOrigin): void;
getConnectedOverlayOrigin(): ElementRef;
getStartValue(): D | null;
getThemePalette(): ThemePalette;
Expand Down