@taiga-ui/kit
Version:
Taiga UI Angular main components kit
222 lines (217 loc) • 9 kB
JavaScript
import { __decorate, __param } from 'tslib';
import { EventEmitter, Optional, Self, Inject, ChangeDetectorRef, Input, Output, ContentChild, TemplateRef, ViewChild, Component, ChangeDetectionStrategy, forwardRef, NgModule } from '@angular/core';
import { NgControl } from '@angular/forms';
import { AbstractTuiNullableControl, TUI_DEFAULT_STRINGIFY, TUI_STRICT_MATCHER, TUI_DEFAULT_IDENTITY_MATCHER, isNativeFocused, isPresent, setNativeFocused, tuiDefaultProp, tuiPure, TUI_FOCUSABLE_ITEM_ACCESSOR, TuiActiveZoneModule } from '@taiga-ui/cdk';
import { TuiDataListDirective, TUI_DATA_LIST_ACCESSOR, TuiHostedDropdownComponent, TuiPrimitiveTextfieldComponent, TUI_DATA_LIST_HOST, TUI_OPTION_CONTENT, TuiPrimitiveTextfieldModule, TuiHostedDropdownModule } from '@taiga-ui/core';
import { TUI_ARROW, TuiArrowModule } from '@taiga-ui/kit/components/arrow';
import { TUI_SELECT_OPTION, TuiSelectOptionModule } from '@taiga-ui/kit/components/select-option';
import { FIXED_DROPDOWN_CONTROLLER_PROVIDER } from '@taiga-ui/kit/providers';
import { CommonModule } from '@angular/common';
import { PolymorpheusModule } from '@tinkoff/ng-polymorpheus';
var TuiComboBoxComponent_1;
const ɵ0 = TUI_SELECT_OPTION;
let TuiComboBoxComponent = TuiComboBoxComponent_1 = class TuiComboBoxComponent extends AbstractTuiNullableControl {
constructor(control, changeDetectorRef) {
super(control, changeDetectorRef);
this.stringify = TUI_DEFAULT_STRINGIFY;
this.strictMatcher = TUI_STRICT_MATCHER;
this.identityMatcher = TUI_DEFAULT_IDENTITY_MATCHER;
this.valueContent = '';
this.strict = true;
this.search = '';
this.searchChange = new EventEmitter();
this.arrow = TUI_ARROW;
this.open = false;
this.datalist = '';
}
get nativeFocusableElement() {
return this.textfield ? this.textfield.nativeFocusableElement : null;
}
get focused() {
return (isNativeFocused(this.nativeFocusableElement) ||
(!!this.hostedDropdown && this.hostedDropdown.focused));
}
get nativeValue() {
return this.value === null ? this.search || '' : this.stringify(this.value);
}
get showValueTemplate() {
return isPresent(this.value) && !this.focused;
}
get canOpen() {
return !this.computedDisabled && !this.readOnly;
}
get computedContent() {
return this.valueContent || this.nativeValue;
}
onActiveZone(active) {
this.updateFocused(active);
}
checkOption(option) {
if (!this.isStrictMatch(option)) {
return;
}
this.updateValue(option);
this.updateSearch(null);
}
handleOption(item) {
this.focusInput();
this.close();
this.updateSearch(null);
this.updateValue(item);
}
onFieldKeyDownEnter(event) {
if (this.open) {
event.preventDefault();
}
const options = this.accessor ? this.accessor.getOptions() : [];
if (options.length !== 1) {
return;
}
this.updateValue(options[0]);
this.updateSearch(null);
this.close();
}
onInput(value) {
this.updateSearch(value);
const match = this.accessor &&
this.accessor.getOptions().find(item => this.isStrictMatch(item));
if (match !== undefined) {
this.updateValue(match);
this.updateSearch(null);
return;
}
this.updateValue(this.strict || this.search === '' ? null : this.search);
if (this.search && this.hostedDropdown) {
this.hostedDropdown.updateOpen(true);
}
}
onHovered(hovered) {
this.updateHovered(hovered);
}
computeContext($implicit, active) {
return {
$implicit,
active,
};
}
toggle() {
if (this.hostedDropdown) {
this.hostedDropdown.updateOpen(!this.open);
}
}
isStrictMatch(item) {
return this.strictMatcher(item, this.search || '', this.stringify);
}
close() {
if (this.hostedDropdown) {
this.hostedDropdown.updateOpen(false);
}
}
updateSearch(search) {
if (this.search === search) {
return;
}
this.search = search;
this.searchChange.emit(search);
}
focusInput(preventScroll = false) {
if (this.nativeFocusableElement) {
setNativeFocused(this.nativeFocusableElement, true, preventScroll);
}
}
};
TuiComboBoxComponent.ctorParameters = () => [
{ type: NgControl, decorators: [{ type: Optional }, { type: Self }, { type: Inject, args: [NgControl,] }] },
{ type: ChangeDetectorRef, decorators: [{ type: Inject, args: [ChangeDetectorRef,] }] }
];
__decorate([
Input(),
tuiDefaultProp()
], TuiComboBoxComponent.prototype, "stringify", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiComboBoxComponent.prototype, "strictMatcher", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiComboBoxComponent.prototype, "identityMatcher", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiComboBoxComponent.prototype, "valueContent", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiComboBoxComponent.prototype, "strict", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiComboBoxComponent.prototype, "search", void 0);
__decorate([
Output()
], TuiComboBoxComponent.prototype, "searchChange", void 0);
__decorate([
ContentChild(TuiDataListDirective, { read: TemplateRef })
], TuiComboBoxComponent.prototype, "datalist", void 0);
__decorate([
ContentChild(TUI_DATA_LIST_ACCESSOR)
], TuiComboBoxComponent.prototype, "accessor", void 0);
__decorate([
ViewChild(TuiHostedDropdownComponent)
], TuiComboBoxComponent.prototype, "hostedDropdown", void 0);
__decorate([
ViewChild(TuiPrimitiveTextfieldComponent)
], TuiComboBoxComponent.prototype, "textfield", void 0);
__decorate([
tuiPure
], TuiComboBoxComponent.prototype, "computeContext", null);
TuiComboBoxComponent = TuiComboBoxComponent_1 = __decorate([
Component({
selector: 'tui-combo-box',
template: "<tui-hosted-dropdown\n class=\"hosted\"\n [canOpen]=\"canOpen\"\n [content]=\"datalist || ''\"\n [(open)]=\"open\"\n (tuiActiveZoneChange)=\"onActiveZone($event)\"\n>\n <tui-primitive-textfield\n automation-id=\"tui-combo-box__textfield\"\n class=\"textfield\"\n [pseudoFocused]=\"computedFocused\"\n [pseudoHovered]=\"computedHovered\"\n [invalid]=\"computedInvalid\"\n [nativeId]=\"nativeId\"\n [readOnly]=\"readOnly\"\n [iconContent]=\"arrow\"\n [disabled]=\"computedDisabled\"\n [focusable]=\"computedFocusable\"\n [value]=\"nativeValue\"\n (valueChange)=\"onInput($event)\"\n (hoveredChange)=\"onHovered($event)\"\n (click)=\"toggle()\"\n (keydown.enter)=\"onFieldKeyDownEnter($event)\"\n >\n <ng-content></ng-content>\n <div\n polymorpheus-outlet\n *ngIf=\"showValueTemplate\"\n class=\"value\"\n automation-id=\"tui-combo-box__template\"\n [content]=\"computedContent\"\n [context]=\"computeContext(value, computedFocused)\"\n ></div>\n </tui-primitive-textfield>\n</tui-hosted-dropdown>\n",
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: TUI_FOCUSABLE_ITEM_ACCESSOR,
useExisting: forwardRef(() => TuiComboBoxComponent_1),
},
{
provide: TUI_DATA_LIST_HOST,
useExisting: forwardRef(() => TuiComboBoxComponent_1),
},
{
provide: TUI_OPTION_CONTENT,
useValue: ɵ0,
},
FIXED_DROPDOWN_CONTROLLER_PROVIDER,
],
styles: [":host{display:block;border-radius:var(--tui-radius-m)}.hosted{display:block;border-radius:inherit}.textfield{border-radius:inherit}.value{display:flex;width:100%;align-items:center}"]
}),
__param(0, Optional()),
__param(0, Self()),
__param(0, Inject(NgControl)),
__param(1, Inject(ChangeDetectorRef))
], TuiComboBoxComponent);
let TuiComboBoxModule = class TuiComboBoxModule {
};
TuiComboBoxModule = __decorate([
NgModule({
imports: [
CommonModule,
PolymorpheusModule,
TuiActiveZoneModule,
TuiPrimitiveTextfieldModule,
TuiHostedDropdownModule,
TuiSelectOptionModule,
TuiArrowModule,
],
declarations: [TuiComboBoxComponent],
exports: [TuiComboBoxComponent],
})
], TuiComboBoxModule);
/**
* Generated bundle index. Do not edit.
*/
export { TuiComboBoxComponent, TuiComboBoxModule, ɵ0 };
//# sourceMappingURL=taiga-ui-kit-components-combo-box.js.map