common-ui/src/lib/inputs/input-with-action/input-with-action.component.ts
2023-03-15 21:34:49 +02:00

56 lines
1.8 KiB
TypeScript

import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, Output } from '@angular/core';
import { randomString } from '../../utils';
import { FormsModule } from '@angular/forms';
import { NgIf } from '@angular/common';
import { CircleButtonComponent } from '../../buttons';
import { IqserIconsModule } from '../../icons';
@Component({
selector: 'iqser-input-with-action',
templateUrl: './input-with-action.component.html',
styleUrls: ['./input-with-action.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [FormsModule, NgIf, IqserIconsModule, CircleButtonComponent],
})
export class InputWithActionComponent {
@Input() inputId = `${randomString() + '-search-input'}`;
@Input() actionButtonId = `${randomString() + '-action-input'}`;
@Input() placeholder = '';
@Input() hint?: string;
@Input() width: number | 'full' = 250;
@Input() icon?: string;
@Input() autocomplete: 'on' | 'off' = 'on';
@Input() value = '';
@Output() readonly action = new EventEmitter<string>();
@Output() readonly valueChange = new EventEmitter<string>();
constructor(private readonly _changeDetectorRef: ChangeDetectorRef) {}
get hasContent(): boolean {
return !!this.value?.length;
}
get computedWidth(): string {
return this.width === 'full' ? '100%' : `${this.width}px`;
}
get isSearch(): boolean {
return !this.icon;
}
reset(): void {
this.value = '';
this.valueChange.emit(this.value);
this._changeDetectorRef.markForCheck();
}
executeAction($event: MouseEvent): void {
$event?.stopPropagation();
if (this.hasContent) {
this.action.emit(this.value);
}
}
}