import {
Component,
ContentChild,
ElementRef,
AfterViewInit,
Output,
EventEmitter,
Input,
} from "@angular/core";
import { AbstractCombobox } from "../aria/abstract-combobox/abstract-combobox";
import { AbstractCell } from "../aria/abstract-grid/abstract-cell";
import { Popover } from "./popover";
@Component({
selector: "sam-picker",
template: ``,
standalone: false,
})
export class SamPickerComponent implements AfterViewInit {
@ContentChild("input", { static: true }) public input: ElementRef;
@ContentChild(Popover, { static: true }) public popover: Popover;
@Output() public onSearch: EventEmitter = new EventEmitter();
@Output() public onChange: EventEmitter =
new EventEmitter();
@Input() public selected: AbstractCell;
public combobox: AbstractCombobox;
public ngAfterViewInit() {
if (this.input && this.popover) {
this.combobox = new AbstractCombobox(
this.input.nativeElement,
this.popover.grid
);
this.combobox.onSearch(
(e) => this.onSearch.emit(this._extractValue(e)),
this
);
this.combobox.onChange((e) => this._onChange(e), this);
}
}
public clearInput() {
this.combobox.clearInput();
}
private _onChange(e?: unknown) {
void e;
this.selected = this.combobox.selected;
this.onChange.emit(this.selected);
}
private _extractValue(e: unknown): string {
if (e instanceof Event) {
return (e.target as HTMLInputElement).value;
}
return e as string;
}
}