Files
grimmory/frontend/src/app/shared/ui/select/app-multi-select.component.ts
T

269 lines
10 KiB
TypeScript

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: `
<div #controlRoot class="relative block h-full w-full" (focusout)="markTouchedWhenFocusLeavesControl($event)">
<div
ngCombobox
#cb="ngCombobox"
appControlTransition
#origin
[class]="triggerClass()"
[attr.data-expanded]="cb.expanded() ? 'true' : null"
[attr.id]="resolvedInputId()"
[attr.name]="name() || null"
[attr.aria-label]="ariaLabel() || null"
[attr.aria-labelledby]="ariaLabelledBy() || null"
[attr.aria-invalid]="showInvalid() ? 'true' : null"
[attr.aria-busy]="pending() ? 'true' : null"
[attr.aria-required]="required() ? 'true' : null"
[attr.aria-describedby]="resolvedDescribedBy()"
[readonly]="readonly()"
[disabled]="disabled()"
(expandedChange)="onExpandedChange($event)">
<span [class]="labelClass()" [class.text-text-muted]="!hasSelection()">
{{ triggerLabel() }}
</span>
@if (showClear() && hasSelection() && !isUnavailable()) {
<button
type="button"
[attr.aria-label]="'shared.ui.select.clearSelection' | transloco"
[class]="clearButtonClass()"
(click)="clear($event)">
<svg lucideX class="size-4" aria-hidden="true"></svg>
</button>
}
<span [class]="indicatorClass()">
@if (pending()) {
<svg lucideLoaderCircle class="size-4 animate-spin" aria-hidden="true"></svg>
} @else {
<svg lucideChevronDown class="size-4 transition-transform" [class.rotate-180]="cb.expanded()" aria-hidden="true"></svg>
}
</span>
</div>
<ng-template ngComboboxPopup [combobox]="cb" [popupType]="filter() ? 'dialog' : 'listbox'">
<ng-template
[cdkConnectedOverlay]="{
origin,
usePopover: 'inline',
matchWidth: true,
positions: overlayPositions,
viewportMargin: 8,
push: true
}"
[cdkConnectedOverlayOpen]="cb.expanded()"
[cdkConnectedOverlayScrollStrategy]="overlayScrollStrategy"
(overlayOutsideClick)="closePopup()"
(attach)="onOverlayAttach()">
@if (filter()) {
<div
ngComboboxWidget
role="dialog"
[attr.aria-label]="filterPlaceholder() || ('shared.ui.select.search' | transloco)"
[class]="surfaceClass"
(keydown.escape)="closePopup()"
(focusout)="markTouchedWhenFocusLeavesControl($event)">
<div [class]="filterRowClass">
<input
ngCombobox
#filterCombobox="ngCombobox"
type="text"
autocomplete="off"
spellcheck="false"
alwaysExpanded
[value]="filterText()"
(valueChange)="filterText.set($event)"
[placeholder]="filterPlaceholder() || ('shared.ui.select.search' | transloco)"
[readonly]="readonly()"
[disabled]="disabled()"
[class]="filterInputClass" />
<svg lucideSearch class="size-4 shrink-0" aria-hidden="true"></svg>
</div>
<ng-template ngComboboxPopup [combobox]="filterCombobox">
<ul
ngComboboxWidget
ngListbox
#list="ngListbox"
tabindex="-1"
focusMode="activedescendant"
selectionMode="explicit"
[multi]="true"
[value]="listboxValues()"
[activeDescendant]="list.activeDescendant()"
[readonly]="readonly()"
[disabled]="disabled()"
(valueChange)="onValuesChange($event)"
[class]="listClass()">
@for (grp of renderedGroups(); track grp.trackKey; let first = $first) {
@if (grouped()) {
@if (!first) {
<li role="separator" [class]="separatorClass"></li>
}
<li role="presentation" [class]="sectionClass">{{ grp.label }}</li>
}
@for (option of grp.options; track option) {
<li
ngOption
tabindex="-1"
[value]="option.value"
[label]="option.label"
[disabled]="option.disabled === true"
[class]="optionClass">
<span [class]="checkboxClass(isSelected(option))">
@if (isSelected(option)) {
<svg lucideCheck class="size-3 text-white" aria-hidden="true"></svg>
}
</span>
@if (optionTemplate(); as tpl) {
<ng-container
[ngTemplateOutlet]="tpl.template"
[ngTemplateOutletContext]="{ $implicit: option }" />
} @else {
<span class="truncate leading-5">{{ option.label }}</span>
}
</li>
}
} @empty {
<li [class]="emptyClass">{{ emptyMessage() || ('shared.ui.select.noResults' | transloco) }}</li>
}
</ul>
</ng-template>
</div>
} @else {
<div [class]="surfaceClass" (focusout)="markTouchedWhenFocusLeavesControl($event)">
<ul
ngComboboxWidget
ngListbox
#list="ngListbox"
tabindex="-1"
focusMode="activedescendant"
selectionMode="explicit"
[multi]="true"
[value]="listboxValues()"
[activeDescendant]="list.activeDescendant()"
[readonly]="readonly()"
[disabled]="disabled()"
(valueChange)="onValuesChange($event)"
[class]="listClass()">
@for (grp of renderedGroups(); track grp.trackKey; let first = $first) {
@if (grouped()) {
@if (!first) {
<li role="separator" [class]="separatorClass"></li>
}
<li role="presentation" [class]="sectionClass">{{ grp.label }}</li>
}
@for (option of grp.options; track option) {
<li
ngOption
tabindex="-1"
[value]="option.value"
[label]="option.label"
[disabled]="option.disabled === true"
[class]="optionClass">
<span [class]="checkboxClass(isSelected(option))">
@if (isSelected(option)) {
<svg lucideCheck class="size-3 text-white" aria-hidden="true"></svg>
}
</span>
@if (optionTemplate(); as tpl) {
<ng-container
[ngTemplateOutlet]="tpl.template"
[ngTemplateOutletContext]="{ $implicit: option }" />
} @else {
<span class="truncate leading-5">{{ option.label }}</span>
}
</li>
}
} @empty {
<li [class]="emptyClass">{{ emptyMessage() || ('shared.ui.select.noResults' | transloco) }}</li>
}
</ul>
</div>
}
</ng-template>
</ng-template>
</div>
`,
})
export class AppMultiSelectComponent<T> extends AppSelectBaseDirective<T> implements FormValueControl<T[]> {
readonly value = model<T[]>([]);
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<T[]>(() => {
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<T>): 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',
);
}
}