Skip to content

Commit

Permalink
fix(addon-mobile): fix mobile-calendar size without header (#8296)
Browse files Browse the repository at this point in the history
  • Loading branch information
MillerSvt authored Aug 5, 2024
1 parent 68f28f6 commit 75bb445
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,22 @@ import {
TUI_DONE_WORD,
tuiImmutableUpdateInputDateMulti,
} from '@taiga-ui/kit';
import {identity, MonoTypeOperatorFunction, Observable, race, timer} from 'rxjs';
import {
BehaviorSubject,
identity,
MonoTypeOperatorFunction,
Observable,
race,
timer,
} from 'rxjs';
import {
debounceTime,
delay,
distinctUntilChanged,
filter,
map,
mergeMap,
skip,
switchMap,
take,
takeUntil,
Expand All @@ -73,7 +82,10 @@ import {
styleUrls: ['./mobile-calendar.style.less'],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: TUI_MOBILE_CALENDAR_PROVIDERS,
host: {'[class._ios]': 'isIOS', '[class._initialized]': 'initialized'},
host: {
'[class._ios]': 'isIOS',
'[class._initialized]': 'initialized',
},
})
export class TuiMobileCalendarComponent implements AfterViewInit {
@ViewChild('yearsScrollRef')
Expand All @@ -82,6 +94,10 @@ export class TuiMobileCalendarComponent implements AfterViewInit {
@ViewChild('monthsScrollRef')
private readonly monthsScrollRef?: CdkVirtualScrollViewport;

private readonly value$ = new BehaviorSubject<
TuiDay | TuiDayRange | readonly TuiDay[] | null
>(null);

private readonly today = TuiDay.currentLocal();
private activeYear = 0;
private activeMonth = 0;
Expand All @@ -107,7 +123,12 @@ export class TuiMobileCalendarComponent implements AfterViewInit {
@Output()
readonly confirm = new EventEmitter<TuiDay | TuiDayRange | readonly TuiDay[]>();

value: TuiDay | TuiDayRange | readonly TuiDay[] | null = null;
@Output()
readonly valueChange = this.value$.pipe(
skip(1),
distinctUntilChanged((a, b) => a?.toString() === b?.toString()),
takeUntil(this.destroy$),
);

readonly years = Array.from({length: RANGE}, (_, i) => i + STARTING_YEAR);

Expand Down Expand Up @@ -147,6 +168,15 @@ export class TuiMobileCalendarComponent implements AfterViewInit {
});
}

get value(): TuiDay | TuiDayRange | readonly TuiDay[] | null {
return this.value$.value;
}

@Input()
set value(value: TuiDay | TuiDayRange | readonly TuiDay[] | null) {
this.value$.next(value);
}

get yearWidth(): number {
return this.doc.documentElement.clientWidth / YEARS_IN_ROW;
}
Expand Down Expand Up @@ -176,12 +206,14 @@ export class TuiMobileCalendarComponent implements AfterViewInit {
this.value = day;
} else if (this.isMultiValue(this.value)) {
this.value = tuiImmutableUpdateInputDateMulti(this.value, day);
} else if (this.isSingleValue(this.value)) {
this.value = new TuiDayRange(day, day);
} else if (this.value instanceof TuiDay) {
this.value = TuiDayRange.sort(this.value, day);
} else if (this.value instanceof TuiDayRange && !this.value.isSingleDay) {
this.value = day;
} else if (this.value instanceof TuiDayRange) {
this.value = TuiDayRange.sort(this.value.from, day);
} else if (!this.value) {
this.value = new TuiDayRange(day, day);
this.value = day;
}
}

Expand Down Expand Up @@ -242,10 +274,6 @@ export class TuiMobileCalendarComponent implements AfterViewInit {
return !(day instanceof TuiDay) && !(day instanceof TuiDayRange) && this.multi;
}

private isSingleValue(day: any): day is TuiDay {
return day instanceof TuiDay || (day instanceof TuiDayRange && !day.isSingleDay);
}

private get initialYear(): number {
if (!this.value) {
return this.today.year;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@
:host._ios & {
height: ~'calc(100% - 8.75rem)';
}

&._without_header {
height: ~'calc(100% - 6rem)';
}
}

.t-month-wrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<cdk-virtual-scroll-viewport
#monthsScrollRef
class="t-months"
[class._without_header]="!chooseDayOrRangeTexts$"
(scrolledIndexChange)="onMonthChange($event)"
>
<section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export class ExampleTuiMobileCalendarComponent {

single = true;

multi = false;

readonly disabledItemHandlerVariants: ReadonlyArray<TuiBooleanHandler<TuiDay>> = [
ALWAYS_FALSE_HANDLER,
({day}) => day % 3 === 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,21 @@
[disabledItemHandler]="disabledItemHandler"
[max]="max"
[min]="min"
[multi]="multi"
[single]="single"
(cancel)="documentationPropertyCancel.emitEvent($event)"
(confirm)="documentationPropertyConfirm.emitEvent($event)"
></tui-mobile-calendar>
</tui-doc-demo>

<tui-doc-documentation>
<ng-template
documentationPropertyMode="input-output"
documentationPropertyName="value"
documentationPropertyType="TuiDay | TuiDayRange | readonly TuiDay[] | null"
>
Value
</ng-template>
<ng-template
documentationPropertyMode="input"
documentationPropertyName="disabledItemHandler"
Expand Down Expand Up @@ -144,6 +152,14 @@
>
Single date or a range
</ng-template>
<ng-template
documentationPropertyMode="input"
documentationPropertyName="multi"
documentationPropertyType="boolean"
[(documentationPropertyValue)]="multi"
>
Array of single dates
</ng-template>
<ng-template
#documentationPropertyCancel="documentationProperty"
documentationPropertyMode="output"
Expand Down

0 comments on commit 75bb445

Please sign in to comment.