Skip to content

fix(material/datepicker): date range inputs not aligning in some cases #25967

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
Nov 16, 2022
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
14 changes: 7 additions & 7 deletions src/material/datepicker/date-range-input-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ abstract class MatDateRangeInputPartBase<D>
this._elementRef.nativeElement.focus();
}

/** Gets the value that should be used when mirroring the input's size. */
getMirrorValue(): string {
const element = this._elementRef.nativeElement;
const value = element.value;
return value.length > 0 ? value : element.placeholder;
}

/** Handles `input` events on the input element. */
override _onInput(value: string) {
super._onInput(value);
Expand Down Expand Up @@ -286,13 +293,6 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpd
this._rangeInput._handleChildValueChange();
}

/** Gets the value that should be used when mirroring the input's size. */
getMirrorValue(): string {
const element = this._elementRef.nativeElement;
const value = element.value;
return value.length > 0 ? value : element.placeholder;
}

override _onKeydown(event: KeyboardEvent) {
const endInput = this._rangeInput._endInput;
const element = this._elementRef.nativeElement;
Expand Down
9 changes: 6 additions & 3 deletions src/material/datepicker/date-range-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
class="mat-date-range-input-container"
cdkMonitorSubtreeFocus
(cdkFocusChange)="_updateFocus($event)">
<div class="mat-date-range-input-start-wrapper">
<div class="mat-date-range-input-wrapper">
<ng-content select="input[matStartDate]"></ng-content>
<span
class="mat-date-range-input-mirror"
aria-hidden="true">{{_getInputMirrorValue()}}</span>
aria-hidden="true">{{_getInputMirrorValue('start')}}</span>
</div>

<span
class="mat-date-range-input-separator"
[class.mat-date-range-input-separator-hidden]="_shouldHideSeparator()">{{separator}}</span>

<div class="mat-date-range-input-end-wrapper">
<div class="mat-date-range-input-wrapper mat-date-range-input-end-wrapper">
<ng-content select="input[matEndDate]"></ng-content>
<span
class="mat-date-range-input-mirror"
aria-hidden="true">{{_getInputMirrorValue('end')}}</span>
</div>
</div>

41 changes: 21 additions & 20 deletions src/material/datepicker/date-range-input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,24 @@ $date-range-input-part-max-width: calc(50% - #{$date-range-input-separator-spaci
transition: none;
}

// Wrapper around the inner inputs. Used to facilitate the auto-resizing input.
.mat-date-range-input-wrapper {
position: relative;
overflow: hidden;
max-width: $date-range-input-part-max-width;
}

.mat-date-range-input-end-wrapper {
// Allow the end input to fill the rest of the available space.
flex-grow: 1;
}

// Underlying input inside the range input.
.mat-date-range-input-inner {
position: absolute;
top: 0;
left: 0;

// Reset the input so it's just a transparent rectangle.
font: inherit;
background: transparent;
Expand All @@ -60,6 +76,10 @@ $date-range-input-part-max-width: calc(50% - #{$date-range-input-separator-spaci
-webkit-appearance: none;
width: 100%;

// Does nothing on Chrome, but necessary for the text
// to align in some cases on Safari and Firefox.
height: 100%;

// Undo the red box-shadow glow added by Firefox on invalid inputs.
// See https://developer.mozilla.org/en-US/docs/Web/CSS/:-moz-ui-invalid
&:-moz-ui-invalid {
Expand Down Expand Up @@ -101,7 +121,7 @@ $date-range-input-part-max-width: calc(50% - #{$date-range-input-separator-spaci
// We want the start input to be flush against the separator, no matter how much text it has, but
// the problem is that inputs have a fixed width. We work around the issue by implementing an
// auto-resizing input that stretches based on its text, up to a point. It works by having
// a relatively-positioned wrapper (`.mat-date-range-input-start-wrapper` below) and an absolutely-
// a relatively-positioned wrapper (`.mat-date-range-input-wrapper` below) and an absolutely-
// positioned `input`, as well as a `span` inside the wrapper which mirrors the input's value and
// placeholder. As the user is typing, the value gets mirrored in the span which causes the wrapper
// to stretch and the input with it.
Expand All @@ -122,25 +142,6 @@ $date-range-input-part-max-width: calc(50% - #{$date-range-input-separator-spaci
min-width: 2px;
}

// Wrapper around the start input. Used to facilitate the auto-resizing input.
.mat-date-range-input-start-wrapper {
position: relative;
overflow: hidden;
max-width: $date-range-input-part-max-width;

.mat-date-range-input-inner {
position: absolute;
top: 0;
left: 0;
}
}

// Wrapper around the end input that makes sure that it has the proper size.
.mat-date-range-input-end-wrapper {
flex-grow: 1;
max-width: $date-range-input-part-max-width;
}

.mat-mdc-form-field-type-mat-date-range-input .mat-mdc-form-field-infix {
// Bump the default width slightly since it's somewhat cramped with two inputs and a separator.
width: 200px;
Expand Down
5 changes: 3 additions & 2 deletions src/material/datepicker/date-range-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ export class MatDateRangeInput<D>
}

/** Gets the value that is used to mirror the state input. */
_getInputMirrorValue() {
return this._startInput ? this._startInput.getMirrorValue() : '';
_getInputMirrorValue(part: 'start' | 'end') {
const input = part === 'start' ? this._startInput : this._endInput;
return input ? input.getMirrorValue() : '';
}

/** Whether the input placeholders should be hidden. */
Expand Down
3 changes: 1 addition & 2 deletions tools/public_api_guard/material/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
getConnectedOverlayOrigin(): ElementRef;
// (undocumented)
_getEndDateAccessibleName(): string;
_getInputMirrorValue(): string;
_getInputMirrorValue(part: 'start' | 'end'): string;
getOverlayLabelId(): string | null;
// (undocumented)
_getStartDateAccessibleName(): string;
Expand Down Expand Up @@ -811,7 +811,6 @@ export class MatStartDate<D> extends _MatDateRangeInputBase<D> implements CanUpd
protected _assignValueToModel(value: D | null): void;
// (undocumented)
protected _formatValue(value: D | null): void;
getMirrorValue(): string;
// (undocumented)
protected _getValueFromModel(modelValue: DateRange<D>): D | null;
// (undocumented)
Expand Down