@taiga-ui/kit
Version:
Taiga UI Angular main components kit
255 lines (250 loc) • 11.3 kB
JavaScript
import { __decorate, __param } from 'tslib';
import { Optional, Self, Inject, ChangeDetectorRef, Input, ViewChild, HostListener, Component, ChangeDetectionStrategy, forwardRef, NgModule } from '@angular/core';
import { NgControl } from '@angular/forms';
import { AbstractTuiControl, TUI_FIRST_DAY, TUI_LAST_DAY, ALWAYS_FALSE_HANDLER, TuiMonth, TuiDay, TuiTime, nullableSame, TUI_DATE_FILLER, tuiDefaultProp, tuiPure, TUI_FOCUSABLE_ITEM_ACCESSOR, TuiPreventDefaultModule } from '@taiga-ui/cdk';
import { sizeBigger, TuiTextfieldSizeDirective, TUI_TEXTFIELD_SIZE, TuiPrimitiveTextfieldComponent, TuiCalendarModule, TuiSvgModule, TuiLinkModule, TuiHostedDropdownModule, TuiPrimitiveTextfieldModule } from '@taiga-ui/core';
import { DATE_TIME_SEPARATOR, TUI_DATE_MASK } from '@taiga-ui/kit/constants';
import { LEFT_ALIGNED_DROPDOWN_CONTROLLER_PROVIDER } from '@taiga-ui/kit/providers';
import { TUI_TIME_TEXTS, TUI_CALENDAR_DATA_STREAM } from '@taiga-ui/kit/tokens';
import { tuiCreateTimeMask, tuiCreateAutoCorrectedDateTimePipe } from '@taiga-ui/kit/utils/mask';
import { TuiReplayControlValueChangesFactory } from '@taiga-ui/kit/utils/miscellaneous';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { CommonModule } from '@angular/common';
import { TuiValueAccessorModule } from '@taiga-ui/kit/directives';
import { PolymorpheusModule } from '@tinkoff/ng-polymorpheus';
import { TextMaskModule } from 'angular2-text-mask';
var TuiInputDateTimeComponent_1;
const ɵ0 = TuiReplayControlValueChangesFactory;
// @dynamic
let TuiInputDateTimeComponent = TuiInputDateTimeComponent_1 = class TuiInputDateTimeComponent extends AbstractTuiControl {
constructor(control, changeDetectorRef, textfieldSize, dateFiller, timeTexts$) {
super(control, changeDetectorRef);
this.textfieldSize = textfieldSize;
this.dateFiller = dateFiller;
this.timeTexts$ = timeTexts$;
this.min = TUI_FIRST_DAY;
this.max = TUI_LAST_DAY;
this.disabledItemHandler = ALWAYS_FALSE_HANDLER;
this.defaultActiveYearMonth = TuiMonth.currentLocal();
this.timeMode = 'HH:MM';
this.open = false;
this.month = null;
}
get fillerLength() {
return this.dateFiller.length + DATE_TIME_SEPARATOR.length + this.timeMode.length;
}
get textMaskOptions() {
return this.calculateMask(this.value[0], this.min, this.max, this.timeMode, this.dateFiller);
}
get nativeFocusableElement() {
return this.textfield ? this.textfield.nativeFocusableElement : null;
}
get focused() {
return !!this.textfield && this.textfield.focused;
}
get calendarIcon() {
return sizeBigger(this.textfieldSize.size)
? 'tuiIconCalendarLarge'
: 'tuiIconCalendar';
}
get computedValue() {
const { value, nativeValue, focused, touched } = this;
const [date, time] = value;
if ((date && !nativeValue) ||
(date && nativeValue.length === this.dateFiller.length) ||
(date && time)) {
return `${date.toString()}${DATE_TIME_SEPARATOR}${time ? time.toString(this.timeMode) : ''}`;
}
if (touched || focused) {
return nativeValue;
}
return date !== null ? date.toString() : '';
}
get calendarValue() {
return this.value[0];
}
get computedActiveYearMonth() {
return this.month || this.value[0] || this.defaultActiveYearMonth;
}
get nativeValue() {
return this.nativeFocusableElement ? this.nativeFocusableElement.value : '';
}
set nativeValue(value) {
if (!this.nativeFocusableElement) {
return;
}
this.nativeFocusableElement.value = value;
}
get canOpen() {
return !this.computedDisabled && !this.readOnly;
}
getFiller$(dateFiller, timeMode) {
return this.timeTexts$.pipe(map(texts => `${dateFiller}${DATE_TIME_SEPARATOR}${texts[timeMode]}`));
}
onClick() {
this.open = !this.open;
}
onValueChange(value) {
if (value.length < this.dateFiller.length) {
this.updateValue([null, null]);
return;
}
const [date, time] = value.split(DATE_TIME_SEPARATOR);
const parsedDate = TuiDay.normalizeParse(date);
const parsedTime = time && time.length === this.timeMode.length
? TuiTime.fromString(time)
: null;
if (parsedDate !== null) {
this.open = false;
}
this.updateValue([parsedDate, parsedTime]);
}
onDayClick(day) {
this.updateValue([day, this.value[1]]);
this.open = false;
}
onHovered(hovered) {
this.updateHovered(hovered);
}
onMonthChange(month) {
this.month = month;
}
onOpenChange(open) {
this.open = open;
}
onFocused(focused) {
this.updateFocused(focused);
if (focused ||
this.value[0] === null ||
this.value[1] !== null ||
this.nativeValue.length <= this.fillerLength + DATE_TIME_SEPARATOR.length ||
this.timeMode === 'HH:MM') {
return;
}
const [, time] = this.nativeValue.split(DATE_TIME_SEPARATOR);
if (!time) {
return;
}
const parsedTime = TuiTime.fromString(time);
this.updateValue([this.value[0], parsedTime]);
setTimeout(() => {
if (this.nativeValue.endsWith('.') || this.nativeValue.endsWith(':')) {
this.nativeValue = this.nativeValue.slice(0, -1);
}
});
}
setDisabledState() {
super.setDisabledState();
this.open = false;
}
writeValue(value) {
super.writeValue(value);
this.nativeValue = value && (value[0] || value[1]) ? this.computedValue : '';
}
getFallbackValue() {
return [null, null];
}
valueIdenticalComparator(oldValue, newValue) {
return (nullableSame(oldValue[0], newValue[0], (a, b) => a.daySame(b)) &&
nullableSame(oldValue[1], newValue[1], (a, b) => a.toString() === b.toString()));
}
calculateMask(day, min, max, timeMode, filler) {
return {
mask: [...TUI_DATE_MASK, ',', ' ', ...tuiCreateTimeMask(timeMode)],
pipe: tuiCreateAutoCorrectedDateTimePipe({ value: day, min, max, filler }, timeMode),
guide: false,
};
}
};
TuiInputDateTimeComponent.ctorParameters = () => [
{ type: NgControl, decorators: [{ type: Optional }, { type: Self }, { type: Inject, args: [NgControl,] }] },
{ type: ChangeDetectorRef, decorators: [{ type: Inject, args: [ChangeDetectorRef,] }] },
{ type: TuiTextfieldSizeDirective, decorators: [{ type: Inject, args: [TUI_TEXTFIELD_SIZE,] }] },
{ type: String, decorators: [{ type: Inject, args: [TUI_DATE_FILLER,] }] },
{ type: Observable, decorators: [{ type: Inject, args: [TUI_TIME_TEXTS,] }] }
];
__decorate([
Input(),
tuiDefaultProp()
], TuiInputDateTimeComponent.prototype, "min", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputDateTimeComponent.prototype, "max", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputDateTimeComponent.prototype, "disabledItemHandler", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputDateTimeComponent.prototype, "defaultActiveYearMonth", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputDateTimeComponent.prototype, "timeMode", void 0);
__decorate([
ViewChild(TuiPrimitiveTextfieldComponent)
], TuiInputDateTimeComponent.prototype, "textfield", void 0);
__decorate([
tuiPure
], TuiInputDateTimeComponent.prototype, "getFiller$", null);
__decorate([
HostListener('click')
], TuiInputDateTimeComponent.prototype, "onClick", null);
__decorate([
tuiPure
], TuiInputDateTimeComponent.prototype, "calculateMask", null);
TuiInputDateTimeComponent = TuiInputDateTimeComponent_1 = __decorate([
Component({
selector: 'tui-input-date-time',
template: "<tui-hosted-dropdown\n class=\"hosted\"\n [canOpen]=\"canOpen\"\n [content]=\"dropdown\"\n [open]=\"open && canOpen\"\n (openChange)=\"onOpenChange($event)\"\n>\n <tui-primitive-textfield\n class=\"textfield\"\n tuiValueAccessor\n [pseudoFocused]=\"pseudoFocused\"\n [pseudoHovered]=\"pseudoHovered\"\n [invalid]=\"computedInvalid\"\n [filler]=\"getFiller$(dateFiller, timeMode) | async\"\n [nativeId]=\"nativeId\"\n [readOnly]=\"readOnly\"\n [iconContent]=\"calendarIcon\"\n [disabled]=\"disabled\"\n [textMask]=\"textMaskOptions\"\n [value]=\"computedValue\"\n (valueChange)=\"onValueChange($event)\"\n (hoveredChange)=\"onHovered($event)\"\n (focusedChange)=\"onFocused($event)\"\n >\n <ng-content></ng-content>\n </tui-primitive-textfield>\n\n <ng-template #dropdown=\"polymorpheus\" polymorpheus>\n <tui-calendar\n tuiPreventDefault=\"mousedown\"\n [min]=\"min\"\n [max]=\"max\"\n [disabledItemHandler]=\"disabledItemHandler\"\n [month]=\"computedActiveYearMonth\"\n [value]=\"calendarValue\"\n (dayClick)=\"onDayClick($event)\"\n (monthChange)=\"onMonthChange($event)\"\n ></tui-calendar>\n </ng-template>\n</tui-hosted-dropdown>\n",
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: TUI_FOCUSABLE_ITEM_ACCESSOR,
useExisting: forwardRef(() => TuiInputDateTimeComponent_1),
},
{
provide: TUI_CALENDAR_DATA_STREAM,
deps: [[new Optional(), new Self(), NgControl]],
useFactory: ɵ0,
},
LEFT_ALIGNED_DROPDOWN_CONTROLLER_PROVIDER,
],
styles: [":host{display:block;border-radius:var(--tui-radius-m)}.hosted{display:block;border-radius:inherit}.textfield{border-radius:inherit}.icon{position:relative;cursor:pointer}.button{display:flex;height:44px;justify-content:center;box-shadow:inset 0 1px var(--tui-base-03)}"]
}),
__param(0, Optional()),
__param(0, Self()),
__param(0, Inject(NgControl)),
__param(1, Inject(ChangeDetectorRef)),
__param(2, Inject(TUI_TEXTFIELD_SIZE)),
__param(3, Inject(TUI_DATE_FILLER)),
__param(4, Inject(TUI_TIME_TEXTS))
], TuiInputDateTimeComponent);
let TuiInputDateTimeModule = class TuiInputDateTimeModule {
};
TuiInputDateTimeModule = __decorate([
NgModule({
imports: [
CommonModule,
TextMaskModule,
PolymorpheusModule,
TuiPreventDefaultModule,
TuiCalendarModule,
TuiSvgModule,
TuiLinkModule,
TuiHostedDropdownModule,
TuiPrimitiveTextfieldModule,
TuiValueAccessorModule,
],
declarations: [TuiInputDateTimeComponent],
exports: [TuiInputDateTimeComponent],
})
], TuiInputDateTimeModule);
/**
* Generated bundle index. Do not edit.
*/
export { TuiInputDateTimeComponent, TuiInputDateTimeModule, ɵ0 };
//# sourceMappingURL=taiga-ui-kit-components-input-date-time.js.map