Skip to content

Commit

Permalink
feat(kit): InputDateRange navigate months in sync, cancel unfinishe…
Browse files Browse the repository at this point in the history
…d range on esc
  • Loading branch information
vladimirpotekhin committed Sep 8, 2022
1 parent 68b0acb commit a0bc906
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
16 changes: 14 additions & 2 deletions projects/kit/components/calendar-range/calendar-range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ChangeDetectorRef,
Component,
EventEmitter,
HostListener,
Inject,
Input,
Optional,
Expand Down Expand Up @@ -80,6 +81,8 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
@tuiDefaultProp()
value: TuiDayRange | null = null;

previousValue: TuiDayRange | null = null;

@Output()
readonly valueChange = new EventEmitter<TuiDayRange | null>();

Expand All @@ -104,8 +107,15 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
});
}

readonly monthShiftMapper: TuiMapper<TuiMonth, TuiMonth> = item =>
item.append({month: 1});
@HostListener('document:keydown.capture', ['$event'])
onEsc(event: KeyboardEvent): void {
if (event.key !== `Escape` || !this.value?.isSingleDay) {
return;
}

event.stopPropagation();
this.value = this.previousValue;
}

readonly mapper: TuiMapper<
readonly TuiDayRangePeriod[],
Expand Down Expand Up @@ -152,6 +162,8 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
onDayClick(day: TuiDay): void {
const {value} = this;

this.previousValue = value;

if (value === null || !value.isSingleDay) {
this.value = new TuiDayRange(day, day);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[min]="min | tuiMapper: maxLengthMapper:value:maxLength:true"
[max]="max | tuiMapper: maxLengthMapper:value:maxLength:false"
[defaultViewedMonthFirst]="defaultViewedMonth"
[defaultViewedMonthSecond]="defaultViewedMonth | tuiMapper: monthShiftMapper"
[disabledItemHandler]="calculatedDisabledItemHandler"
[value]="value"
(dayClick)="onDayClick($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,15 @@ export class TuiPrimitiveCalendarRangeComponent implements OnInit {
onSectionFirstViewedMonth(month: TuiMonth): void {
this.userViewedMonthFirst = month;

if (this.userViewedMonthSecond.year < this.userViewedMonthFirst.year) {
this.userViewedMonthSecond = this.userViewedMonthSecond.append({
year: month.year - this.userViewedMonthSecond.year,
});
}
this.userViewedMonthSecond = this.userViewedMonthFirst.append({month: 1});
}

onSectionSecondViewedMonth(month: TuiMonth): void {
this.userViewedMonthSecond = month;

if (this.userViewedMonthFirst.year > this.userViewedMonthSecond.year) {
this.userViewedMonthFirst = this.userViewedMonthFirst.append({
year: month.year - this.userViewedMonthFirst.year,
});
}
this.userViewedMonthFirst = this.userViewedMonthSecond.append({
month: -1,
});
}

onDayClick(day: TuiDay): void {
Expand Down Expand Up @@ -175,11 +169,7 @@ export class TuiPrimitiveCalendarRangeComponent implements OnInit {
private updateViewedMonths(): void {
this.userViewedMonthFirst =
this.value === null ? this.defaultViewedMonthFirst : this.value.from;
this.userViewedMonthSecond =
this.value === null ? this.defaultViewedMonthSecond : this.value.to;

if (this.userViewedMonthFirst.monthSame(this.userViewedMonthSecond)) {
this.userViewedMonthSecond = this.userViewedMonthSecond.append({month: 1});
}
this.userViewedMonthSecond = this.userViewedMonthFirst.append({month: 1});
}
}

0 comments on commit a0bc906

Please sign in to comment.