import { NgTemplateOutlet } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
computed,
model,
} from '@angular/core';
import { type FormValueControl } from '@angular/forms/signals';
import { Combobox, ComboboxPopup, ComboboxWidget } from '@angular/aria/combobox';
import { Listbox, Option } from '@angular/aria/listbox';
import { OverlayModule } from '@angular/cdk/overlay';
import { TranslocoPipe, translateSignal } from '@jsverse/transloco';
import { LucideCheck, LucideChevronDown, LucideLoaderCircle, LucideSearch, LucideX } from '@lucide/angular';
import { cn } from '../cn';
import { AppControlTransitionDirective } from '../control.styles';
import { AppSelectBaseDirective } from './app-select-base.directive';
import { type SelectOption } from './app-select.options';
@Component({
selector: 'app-multi-select',
standalone: true,
imports: [
NgTemplateOutlet,
OverlayModule,
Combobox,
ComboboxPopup,
ComboboxWidget,
Listbox,
Option,
TranslocoPipe,
AppControlTransitionDirective,
LucideCheck,
LucideChevronDown,
LucideLoaderCircle,
LucideSearch,
LucideX,
],
host: { class: 'block w-full' },
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
{{ triggerLabel() }}
@if (showClear() && hasSelection() && !isUnavailable()) {
}
@if (pending()) {
} @else {
}
@if (filter()) {
} @else {
@for (grp of renderedGroups(); track grp.trackKey; let first = $first) {
@if (grouped()) {
@if (!first) {
}
- {{ grp.label }}
}
@for (option of grp.options; track option) {
-
@if (isSelected(option)) {
}
@if (optionTemplate(); as tpl) {
} @else {
{{ option.label }}
}
}
} @empty {
- {{ emptyMessage() || ('shared.ui.select.noResults' | transloco) }}
}
}
`,
})
export class AppMultiSelectComponent extends AppSelectBaseDirective implements FormValueControl {
readonly value = model([]);
private readonly selectedCount = computed(() => this.value().length);
private readonly selectedCountParam = computed(() => String(this.selectedCount()));
private readonly selectedCountLabel = translateSignal('shared.ui.select.selectedCount', {
count: this.selectedCountParam,
});
protected override readonly hasSelection = computed(() => this.selectedCount() > 0);
protected override readonly triggerLabel = computed(() =>
this.selectedCount() ? this.selectedCountLabel() : this.placeholder(),
);
protected override readonly listboxValues = computed(() => {
return this.visibleOptionValuesForSelected(this.value());
});
protected override onValuesChange(values: readonly T[]): void {
if (this.isUnavailable()) return;
const hiddenSelections = this.value().filter((selectedValue) => !this.isVisibleValue(selectedValue));
this.value.set([...hiddenSelections, ...values]);
}
protected override clearValue(): void {
this.value.set([]);
}
protected override isSelected(option: SelectOption): boolean {
return this.value().some((selectedValue) => this.valuesMatch(option.value, selectedValue));
}
protected checkboxClass(selected: boolean): string {
return cn(
'mr-2 flex size-4 shrink-0 items-center justify-center rounded-sm border transition-colors',
selected ? 'border-primary bg-primary' : 'border-border bg-card',
);
}
}