# API of `@koobiq/components/list`

Documented on [List](https://koobiq.io/en/components/list.md). NgModule: `KbqListModule`.

## Components

### KbqList (component)

A plain, non-selectable list container.

Intentionally carries no ARIA role: it is used both as a semantic list (consumers add
`role="list"` themselves, as `kbq-file-upload` does) and as a purely visual grouping.
For a selectable listbox use `kbq-list-selection` instead.

```ts
@Component({ selector: 'kbq-list' })
class KbqList {}
```

### KbqListItem (component)

A single row of a `kbq-list`. Non-selectable — see `kbq-list-option` for the selectable variant.

```ts
@Component({ selector: 'kbq-list-item, a[kbq-list-item]' })
class KbqListItem {
    getHostElement(): HTMLElement;
}
```

### KbqListOption (component)

Component for list-options of selection-list. Each list-option can automatically
generate a checkbox and can put current item into the selectionModel of selection-list
if the current item is selected.

```ts
@Component({ selector: 'kbq-list-option', exportAs: 'kbqListOption' })
class KbqListOption<T = any> implements IFocusableOption, KbqTitleTextRef {
    readonly checkboxPosition = input<'before' | 'after'>();
    value = input<T>();
    disabled = input<boolean>(false);
    draggable = input<boolean>(true);
    showCheckbox = input<boolean | undefined>();
    selected = input<boolean | undefined>();
    listSelection: KbqListSelection<T>;
    readonly group: KbqOptgroup | null;
    hasFocus: boolean;
    preventBlur: boolean;
    readonly onFocus: Subject<KbqOptionEvent<T>>;
    readonly onBlur: Subject<KbqOptionEvent<T>>;
    readonly actionButton: Signal<KbqOptionActionComponent | undefined>;
    get tooltipTrigger(): KbqTooltipTrigger | undefined;
    get dropdownTrigger(): KbqDropdownTrigger | undefined;
    readonly pseudoCheckbox: Signal<KbqPseudoCheckbox | undefined>;
    readonly text: Signal<ElementRef<any>>;
    get textElement(): ElementRef<HTMLElement> | undefined;
    toggle(): void;
    getLabel(): string;
    setSelected(selected: boolean): void;
    focus(): void;
    blur(): void;
    getHostElement(): HTMLElement;
}
```

- `[value]`: `T` — Value of the option, reported through the list's `ControlValueAccessor`.
- `[disabled]`: `boolean` — Whether the option is disabled, through its own input, its group or the list.
- `[draggable]`: `boolean` — Whether the option can be reordered by dragging. Set it to `false` to pin a single option while
  the rest of the list stays draggable. Unrelated to `disabled`: the option keeps taking focus and
  selection. Reads the resolved state, which the option can only narrow — the list gates its whole drop
  list, so an option cannot opt back in on its own.
- `[showCheckbox]`: `boolean | undefined` — Whether the option renders a pseudo-checkbox. Falls back to the list's own mode until bound.
- `[selected]`: `boolean | undefined` — Whether the option is selected. The state lives in the list's `SelectionModel`, not on the option,
  so it can also be set from code.
- `tooltipTrigger`: `KbqTooltipTrigger | undefined` — Tooltip trigger projected into the option.
- `dropdownTrigger`: `KbqDropdownTrigger | undefined` — Dropdown trigger projected into the option.
- `textElement`: `ElementRef<HTMLElement> | undefined` — Not a duplicate of `text`: this is the `KbqTitleTextRef` property that `title.directive.ts`
  reads through `KBQ_TITLE_TEXT_REF`, a plain `ElementRef` rather than a signal.
- `toggle()`: `void` — Toggles the selected state of this option.
- `setSelected()`: `void` — Sets the selected state on the list's `SelectionModel`; the `selected` property and input go through it.
- `focus()`: `void` — Moves DOM focus to this option, unless it is disabled or already focused.
- `blur()`: `void` — Marks this option as blurred after the next render, unless `preventBlur` is set.
- `getHostElement()`: `HTMLElement` — The option's host DOM element.

### KbqListSelection (component)

```ts
@Component({ selector: 'kbq-list-selection', exportAs: 'kbqListSelection' })
class KbqListSelection<T = any> implements ControlValueAccessor {
    autoSelect = input<boolean | undefined>();
    noUnselectLast = input<boolean | undefined>();
    draggable = input<boolean>(false);
    readonly connectedTo = input<KbqListSelection<T> | string | readonly (KbqListSelection<T> | string)[]>(
        []
    );
    readonly dragPreview = input<KbqListDragPreview>('text');
    readonly dragCursor = input<KbqListDragCursor>('auto');
    readonly selectAllToggle = input<boolean>(false);
    readonly selectAllEnabled = input<boolean>(false, {
        alias: 'selectAll'
    });
    multiple = input<KbqMultipleInput | undefined>();
    readonly horizontal = input<boolean>(false);
    disabled = input<boolean | undefined>();
    readonly compareWith = input<(o1: T, o2: T) => boolean>(
        (a1, a2) => a1 === a2
    );
    selectAllHandler = input<((event: KeyboardEvent, list: KbqListSelection<T>) => void) | undefined>();
    readonly onSelectAll = output<KbqListSelectAllEvent<KbqListOption<T>>>();
    readonly dropped = output<KbqListSelectionDroppedEvent>();
    readonly selectionChange = output<KbqListSelectionChange<T>>();
    keyManager: FocusKeyManager<KbqListOption<T>>;
    get options(): QueryList<KbqListOption<T>>;
    readonly onCopy: EventEmitter<KbqListCopyEvent<KbqListOption<T>>>;
    readonly selectAllOption: Signal<KbqListOption<T> | undefined>;
    get allOptionsSelected(): boolean;
    get selectAllState(): KbqPseudoCheckboxState;
    multipleMode: MultipleMode | null;
    userTabIndex: number | null;
    get showCheckbox(): boolean;
    selectionModel: SelectionModel<KbqListOption<T>>;
    get optionFocusChanges(): Observable<KbqOptionEvent<T>>;
    get optionBlurChanges(): Observable<KbqOptionEvent<T>>;
    toggleSelectAll(): void;
    focus(): void;
    blur(): void;
    selectAll(): void;
    deselectAll(): void;
    updateScrollSize(): void;
    setSelectedOptionsByClick(
        option: KbqListOption<T>,
        shiftKey: boolean,
        ctrlKey: boolean
    ): void;
    setSelectedOptionsByKey(
        option: KbqListOption<T>,
        shiftKey: boolean,
        ctrlKey: boolean
    ): void;
    selectActiveOptions(): void;
    getSelectedOptionValues(): T[];
    toggleFocusedOption(): void;
    canDeselectLast(listOption: KbqListOption<T>): boolean;
    removeOptionFromList(option: KbqListOption<T>): void;
    onKeyDown(event: KeyboardEvent): void;
    reportValueChange(): void;
    emitChangeEvent(option: KbqListOption<T>): void;
}
```

- `[autoSelect]`: `boolean | undefined` — Whether clicking an option clears the rest of the selection. Defaults to `true`, and to `false` for
  `multiple="checkbox"`, where a click is expected to toggle a single row. A bound value replaces the
  default derived from `multiple`.
- `[noUnselectLast]`: `boolean | undefined` — Whether the last selected option can be deselected. Derived from the mode like `autoSelect`.
- `[draggable]`: `boolean` — Whether options can be reordered by dragging them.
  Reordering never mutates the data — handle the `dropped` event and move the item yourself.
- `[connectedTo]`: `KbqListSelection<T> | string | readonly (KbqListSelection<T> | string)[]` — Lists that options of this list can be moved into. Accepts `KbqListSelection` instances or the
  `id` of another list. `cdkDropListGroup` on a common ancestor connects lists automatically.
- `[dragPreview]`: `KbqListDragPreview` — What the option's floating copy looks like while it is dragged. Defaults to `text`: a compact
  plate carrying the option's label and caption, capped at a readable width. Use `full` to drag a
  clone of the whole row, checkbox, icons and action button included.
- `[dragCursor]`: `KbqListDragCursor` — Whether a draggable option shows the grab cursor over the whole row. Defaults to `auto`, which
  leaves the row the cursor it has anyway. Use `grab` where the whole row is meant to read as a
  handle. An option carrying a projected `cdkDragHandle` ignores this — the handle keeps the grab.
- `[selectAllToggle]`: `boolean` — When `true`, a repeated Ctrl/Cmd+A deselects all options. Off by default (Ctrl+A only selects).
- `[selectAll]`: `boolean` — Whether to render the "select all" master checkbox above the options. `multiple="checkbox"` only —
  a vertical list, since the row is a full-width heading of the column it governs.

  The row acts on every option the user can toggle, disabled ones excluded. Enabling it also makes
  Ctrl/Cmd + A a two-way toggle, so the shortcut and the checkbox never disagree (`selectAllToggle`
  is implied).

  Aliased rather than named `selectAll`, which is already taken by the imperative method of that name.
- `[multiple]`: `KbqMultipleInput | undefined` — Selection mode of the list.

  `checkbox` renders a checkbox in every option, `keyboard` selects without one. A bare `multiple` and
  `true` both mean `checkbox`; `single`, `false` and an absent attribute mean single selection. The mode
  can be changed at any time — narrowing it to single selection keeps the first selected option and
  reports the shortened value through the `ControlValueAccessor`.

  The getter reports whether more than one option can be selected; read `multipleMode` for the
  mode itself.
- `[disabled]`: `boolean | undefined` — Whether the list is disabled, bound with `[disabled]` or set by a form control.
- `[compareWith]`: `(o1: T, o2: T) => boolean` — Function used for comparing an option against the selected value when determining which
  options should appear as selected. The first argument is the value of an options. The second
  one is a value from the selected value. A boolean must be returned.

  Bind a stable reference — a field or a bound method. An expression that builds a new function on
  every change detection pass makes the list re-resolve every value against every option each pass.
- `[selectAllHandler]`: `((event: KeyboardEvent, list: KbqListSelection<T>) => void) | undefined` — Function for handling the combination Ctrl + A (select all). By default, the internal handler is used,
  which toggles the selection of all non-disabled options.
- `(dropped)`: `KbqListSelectionDroppedEvent` — Emits when an option changes its position by dragging.
- `onCopy`: `EventEmitter<KbqListCopyEvent<KbqListOption<T>>>` — Emits when the active option is copied with Ctrl/Cmd + C. Subscribing replaces the built-in clipboard
  copy, so the event stays an `EventEmitter`: the decision is made by reading `observed`, which
  `OutputEmitterRef` does not have. A template listener subscribes to it through the output below.
- `selectAllOption`: `Signal<KbqListOption<T> | undefined>` — Reference to the built-in "select all" row, rendered only while `selectAll` is on.

  Located by template reference rather than by the `KbqListOption` type: that class is declared
  below this one in the same file, and a query locator is evaluated while this class is being
  defined, which would read it in its temporal dead zone.
- `allOptionsSelected`: `boolean` — Whether every option "select all" can act on is selected.
- `selectAllState`: `KbqPseudoCheckboxState` — State of the "select all" master checkbox.
- `multipleMode`: `MultipleMode | null` — Resolved selection mode, or `null` when only one option can be selected.
- `selectionModel`: `SelectionModel<KbqListOption<T>>` — Model backing the selection.

  The instance is replaced whenever `multiple` changes the multiplicity, because CDK freezes that
  flag at construction. Subscribe to `selectionChange` rather than to `selectionModel.changed`:
  a subscription taken on the model directly is left behind on the discarded instance.
- `toggleSelectAll()`: `void` — Selects every option "select all" can act on, or deselects them all when they are already selected.

  Backs both the master checkbox and Ctrl/Cmd + A, so the two can never disagree. Reports the value
  once for the whole batch instead of once per option.
- `blur()`: `void` — Clears the active key-manager item (unless an option is still focused) and marks the list as touched.
- `selectAll()`: `void` — Selects every option and reports the new value to the `ControlValueAccessor`.

  Unlike the `selectAll` attribute and Ctrl/Cmd + A, this acts on disabled options as well — it is
  an imperative command, not something the user performed. See `toggleSelectAll` for the
  behaviour the master checkbox has.
- `deselectAll()`: `void` — Deselects every option, disabled ones included, and reports the new value. See `selectAll`.
- `getSelectedOptionValues()`: `T[]` — Values of the currently selected options.
- `canDeselectLast()`: `boolean` — Whether `listOption` is allowed to be deselected, given `noUnselectLast`.
- `onKeyDown()`: `void` — Handles keydown events on the list.

## Directives

### KbqListOptionCaption (directive)

Marker directive for the secondary line of a `kbq-list-option`.

Purely a styling hook: it adds the `kbq-list-option-caption` class the list theme keys its
muted caption typography and per-state colors off.

```ts
@Directive({ selector: '[kbq-list-option-caption]' })
class KbqListOptionCaption {}
```

## Classes

### KbqListCopyEvent (class)

Event class that occurs when copying an item from the KbqListSelection.
Used to pass data about the copied item and copy context.

```ts
class KbqListCopyEvent<T> {
    source: KbqListSelection<any>;
    option: T;
    event?: KeyboardEvent;
    constructor(
        source: KbqListSelection<any>,
        option: T,
        event?: KeyboardEvent
    );
}
```

### KbqListSelectAllEvent (class)

```ts
class KbqListSelectAllEvent<T> {
    source: KbqListSelection<any>;
    options: T[];
    constructor(source: KbqListSelection<any>, options: T[]);
}
```

### KbqListSelectionChange (class)

```ts
class KbqListSelectionChange<T = any> {
    source: KbqListSelection<T>;
    option: KbqListOption<T>;
    constructor(source: KbqListSelection<T>, option: KbqListOption<T>);
}
```

## Types

### KbqListDragCursor (type)

Whether a draggable option advertises with the cursor that the whole row can be picked up.

`auto` leaves the row the cursor it would have anyway, `grab` turns the row into a visible handle.
A projected `cdkDragHandle` takes the grab over either way.

```ts
type KbqListDragCursor = 'auto' | 'grab';
```

### KbqListDragPreview (type)

How the floating copy of an option is rendered while it is dragged.

`text` shows a compact plate with the option's own text and nothing else, `full` a clone of the
whole row.

```ts
type KbqListDragPreview = 'text' | 'full';
```

### KbqListSelectionDroppedEvent (type)

Event emitted when an option changes its position by dragging.

```ts
type KbqListSelectionDroppedEvent = Pick<CdkDragDrop<KbqListSelection>, 'previousIndex' | 'currentIndex'> & {
    option: KbqListOption;
    container: KbqListSelection;
    previousContainer: KbqListSelection;
    event: MouseEvent | TouchEvent;
};
```

- `option`: `KbqListOption` — Option that has been moved.
- `container`: `KbqListSelection` — List the option has been moved into.
- `previousContainer`: `KbqListSelection` — List the option has been taken from. Equal to `container` when reordering within a single list.
- `event`: `MouseEvent | TouchEvent` — Pointer event that ended the drag.

### KbqOptionEvent (interface)

```ts
interface KbqOptionEvent<T = any> {
    option: KbqListOption<T>;
}
```

## Constants

### KBQ_SELECTION_LIST_VALUE_ACCESSOR (const)

```ts
const KBQ_SELECTION_LIST_VALUE_ACCESSOR: Provider;
```
