Skip to content
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

fix:- select date-time from keyboard #119

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 6 additions & 4 deletions projects/core/src/datetimepicker/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export class MatDatetimepickerCalendar<D> implements AfterContentInit, OnDestroy
@Input() ariaPrevYearLabel = "Previous year";
@Input() ariaNextMultiYearLabel = "Next year range";
@Input() ariaPrevMultiYearLabel = "Previous year range";
/** Prevent user to select same date time */
@Input() preventSameDateTimeSelection = false;
/** Emits when the currently selected date changes. */
@Output() selectedChange: EventEmitter<D> = new EventEmitter<D>();
/** Emits when the view has been changed. **/
Expand Down Expand Up @@ -297,7 +299,7 @@ export class MatDatetimepickerCalendar<D> implements AfterContentInit, OnDestroy
/** Handles date selection in the month view. */
_dateSelected(date: D): void {
if (this.type === "date") {
if (!this._adapter.sameDate(date, this.selected)) {
if (!this._adapter.sameDate(date, this.selected) || !this.preventSameDateTimeSelection) {
this.selectedChange.emit(date);
}
} else {
Expand All @@ -309,7 +311,7 @@ export class MatDatetimepickerCalendar<D> implements AfterContentInit, OnDestroy
/** Handles month selection in the year view. */
_monthSelected(month: D): void {
if (this.type === "month") {
if (!this._adapter.sameMonthAndYear(month, this.selected)) {
if (!this._adapter.sameMonthAndYear(month, this.selected) || !this.preventSameDateTimeSelection) {
this.selectedChange.emit(this._adapter.getFirstDateOfMonth(month));
}
} else {
Expand All @@ -322,7 +324,7 @@ export class MatDatetimepickerCalendar<D> implements AfterContentInit, OnDestroy
/** Handles year selection in the multi year view. */
_yearSelected(year: D): void {
if (this.type === "year") {
if (!this._adapter.sameYear(year, this.selected)) {
if (!this._adapter.sameYear(year, this.selected) || !this.preventSameDateTimeSelection) {
const normalizedDate = this._adapter.createDatetime(this._adapter.getYear(year), 0, 1, 0, 0);
this.selectedChange.emit(normalizedDate);
}
Expand All @@ -337,7 +339,7 @@ export class MatDatetimepickerCalendar<D> implements AfterContentInit, OnDestroy
this._activeDate = this._updateDate(date);
this._clockView = "minute";
} else {
if (!this._adapter.sameDatetime(date, this.selected)) {
if (!this._adapter.sameDatetime(date, this.selected) || !this.preventSameDateTimeSelection) {
this.selectedChange.emit(date);
}
}
Expand Down
4 changes: 3 additions & 1 deletion projects/core/src/datetimepicker/datetimepicker-content.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<mat-datetimepicker-calendar (_userSelection)="datetimepicker.close()" (selectedChange)="datetimepicker._select($event)"
<mat-datetimepicker-calendar (_userSelection)="datetimepicker.close()"
(selectedChange)="onSelectionChange($event)"
(viewChanged)="datetimepicker._viewChanged($event)"
[ariaNextMonthLabel]="datetimepicker.ariaNextMonthLabel"
[ariaNextYearLabel]="datetimepicker.ariaNextYearLabel"
[ariaPrevMonthLabel]="datetimepicker.ariaPrevMonthLabel"
[ariaPrevYearLabel]="datetimepicker.ariaPrevYearLabel"
[preventSameDateTimeSelection]="datetimepicker.preventSameDateTimeSelection"
[attr.mode]="datetimepicker.mode"
[dateFilter]="datetimepicker._dateFilter"
[id]="datetimepicker.id"
Expand Down
7 changes: 7 additions & 0 deletions projects/core/src/datetimepicker/datetimepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export class MatDatetimepickerContent<D> implements AfterContentInit {
this._calendar._focusActiveCell();
}

onSelectionChange(date: D) {
this.datetimepicker._select(date);
this.datetimepicker.close();
}

/**
* Handles keydown event on datepicker content.
* @param event The event.
Expand Down Expand Up @@ -99,6 +104,8 @@ export class MatDatetimepicker<D> implements OnDestroy {
@Input() ariaPrevMonthLabel = "Previous month";
@Input() ariaNextYearLabel = "Next year";
@Input() ariaPrevYearLabel = "Previous year";
/** Prevent user to select same date time */
@Input() preventSameDateTimeSelection = false;
/**
* Emits new selected date when selected date changes.
* @deprecated Switch to the `dateChange` and `dateInput` binding on the input element.
Expand Down
7 changes: 7 additions & 0 deletions src/app/date.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ <h2>Date types with: {{ type }}</h2>
<mat-datetimepicker #touch mode="portrait" openOnFocus="true" timeInterval="5" touchUi="true"
type="datetime"></mat-datetimepicker>
</mat-form-field>
<mat-form-field>
<mat-placeholder>Prevent same date time</mat-placeholder>
<input [matDatetimepicker]="preventSame" formControlName="preventsame" matInput required>
<mat-datetimepicker-toggle [for]="preventSame" matSuffix></mat-datetimepicker-toggle>
<mat-datetimepicker #preventSame openOnFocus="true" timeInterval="5"
preventSameDateTimeSelection="true" type="datetime"></mat-datetimepicker>
</mat-form-field>
</form>
3 changes: 2 additions & 1 deletion src/app/moment/moment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export class MomentDatetimeComponent {
year: [null, Validators.required],
mintest: [this.today, Validators.required],
filtertest: [this.today, Validators.required],
touch: [null, Validators.required]
touch: [null, Validators.required],
preventsame: [new Date("2020-11-19T17:00:00.000Z"), Validators.required]
});
}
}
3 changes: 2 additions & 1 deletion src/app/native/native.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class NativeDatetimeComponent {
year: [null, Validators.required],
mintest: [this.today, Validators.required],
filtertest: [this.today, Validators.required],
touch: [null, Validators.required]
touch: [null, Validators.required],
preventsame: [new Date("2020-11-19T17:00:00.000Z"), Validators.required]
});
}
}