Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 1x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 44x 44x 42x 43x 42x 42x 42x 42x 42x 42x 5x 2x 3x 12x 12x 2x 4x 4x 4x 7x 7x 1x 1x 1x 1x 1x 1x 7x 4x 4x 4x 2x 23x 20x 20x 17x 17x 17x 17x 17x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 88x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { booleanAttribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, ElementRef, HostListener, inject, input, OnInit, signal, viewChild } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { SI_FORM_ITEM_CONTROL, SiFormItemControl } from '@siemens/element-ng/form'; import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate'; import { SiInputPillComponent } from './si-input-pill.component'; import { SiPillsInputValueHandlerDirective, SiPillsInputValueHandlerTrigger } from './si-pills-input-value-handler'; @Component({ selector: 'si-pills-input', imports: [SiInputPillComponent, SiTranslatePipe], templateUrl: './si-pills-input.component.html', styleUrl: './si-pills-input.component.scss', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: SiPillsInputComponent, multi: true }, { provide: SI_FORM_ITEM_CONTROL, useExisting: SiPillsInputComponent } ], changeDetection: ChangeDetectionStrategy.OnPush, host: { class: 'form-control', role: 'listbox', 'aria-orientation': 'horizontal', '[class.disabled]': 'disabled()', '[attr.aria-disabled]': 'disabled()', '[class.readonly]': 'readonly()', '[attr.aria-readonly]': 'readonly()', // using attr so that tabindex is removed if not defined '[attr.tabindex]': 'tabindex()', '[attr.aria-activedescendant]': 'activeDescendant()', '[attr.aria-labelledby]': 'labelledby()', '[attr.aria-describedby]': 'errormessageId()' } }) export class SiPillsInputComponent implements OnInit, ControlValueAccessor, SiFormItemControl { private static idCounter = 0; /** * The identifier of the pills-input. Will be generated if not provided. * * @defaultValue * ``` * `__si-pills-input-${SiPillsInputComponent.idCounter++}` * ``` */ readonly id = input(`__si-pills-input-${SiPillsInputComponent.idCounter++}`); /** * The aria-label for the inner input where users enter new items. * * @defaultValue * ``` * t(() => $localize`:@@SI_PILLS_INPUT.INPUT_ELEMENT_ARIA_LABEL:Create item`) * ``` */ readonly inputElementAriaLabel = input( t(() => $localize`:@@SI_PILLS_INPUT.INPUT_ELEMENT_ARIA_LABEL:Create item`) ); /** * Whether the pills-input is disabled. * * @defaultValue false */ // eslint-disable-next-line @angular-eslint/no-input-rename readonly disabledInput = input(false, { alias: 'disabled', transform: booleanAttribute }); /** * Whether the pills-input is readonly * * @defaultValue false */ readonly readonly = input(false, { transform: booleanAttribute }); /** The placeholder to be shown if no value is currently present. */ readonly placeholder = input<string>(); /** * @defaultValue * ``` * `${this.id()}-label` * ``` */ readonly labelledby = input(`${this.id()}-label`); /** * This ID will be bound to the `aria-describedby` attribute of the pills-input. * Use this to reference the element containing the error message(s) for the pills-input. * It will be picked by the {@link SiFormItemComponent} if the pills-input is used inside a form item. * * @defaultValue * ``` * `${this.id()}-errormessage` * ``` */ readonly errormessageId = input(`${this.id()}-errormessage`); protected inputValue = ''; protected onTouched: () => void = () => {}; protected onChange: (val: any) => void = () => {}; protected readonly pills = signal<string[]>([]); protected readonly activePillIndex = signal<number | undefined>(undefined); protected readonly inputId = computed(() => `${this.id()}-input`); protected readonly disabled = computed(() => this.disabledInput() || this.disabledNgControl()); protected readonly activeDescendant = computed(() => { const activePillIndex = this.activePillIndex(); return activePillIndex !== undefined ? `${this.id()}-pill-${activePillIndex}` : null; }); protected readonly tabindex = computed(() => this.disabled() ? undefined : this.readonly() ? 0 : -1 ); private readonly inputElement = viewChild<ElementRef<HTMLInputElement>>('inputElement'); private readonly disabledNgControl = signal(false); private siPillsInputValueHandlerDirective = inject(SiPillsInputValueHandlerDirective, { optional: true }); private readonly cdRef = inject(ChangeDetectorRef); private readonly elementRef = inject<ElementRef<HTMLElement>>(ElementRef); ngOnInit(): void { this.siPillsInputValueHandlerDirective ??= { handle: (value, trigger) => { if (trigger !== 'input') { return { newPills: [value], newValue: '' }; } return undefined; } }; } protected input(): void { this.inputValue = this.inputElement()!.nativeElement.value; this.rebuildValue(this.inputValue, 'input'); } @HostListener('click') protected click(): void { this.inputElement()?.nativeElement.focus(); } protected blur(): void { this.rebuildValue(this.inputValue, 'blur'); this.activePillIndex.set(undefined); this.onTouched(); } protected keydownEnter(event: Event): void { this.rebuildValue(this.inputValue, 'keydown.enter'); event.preventDefault(); } protected keydownBackspace(event: Event): void { const count = this.pills().length; if (!this.inputValue && count) { const lastChipValue = this.pills().at(-1)!; this.remove(count - 1, false); this.inputValue = lastChipValue; event.preventDefault(); } } protected remove(index: number, focus = true): void { this.pills.update(pills => pills.filter((_, pillIndex) => index !== pillIndex)); this.onTouched(); this.onChange(this.pills()); if (focus) { this.inputElement()!.nativeElement.focus(); } } private rebuildValue(value: string, trigger: SiPillsInputValueHandlerTrigger): void { if (value) { const valueParseResult = this.siPillsInputValueHandlerDirective?.handle(value, trigger); if (valueParseResult) { this.pills.update(pills => [...pills, ...valueParseResult.newPills]); // Doesn't update when setting to empty string. // Not using setTimeout to avoid flickering. this.cdRef.detectChanges(); this.inputValue = valueParseResult.newValue; this.cdRef.detectChanges(); if (valueParseResult.newPills.length) { this.onTouched(); this.onChange(this.pills()); } } } } @HostListener('keydown.arrowLeft') protected arrowLeft(): void { const activePillIndex = this.activePillIndex(); const count = this.pills().length; Iif (activePillIndex !== undefined) { this.activePillIndex.set(Math.max(0, activePillIndex - 1)); } else if (!this.inputValue.length && count) { this.elementRef.nativeElement.focus(); this.activePillIndex.set(count - 1); } } @HostListener('keydown.arrowRight') protected arrowRight(): void { const activePillIndex = this.activePillIndex(); Iif (activePillIndex !== undefined) { this.activePillIndex.set(activePillIndex + 1); Iif (this.activePillIndex()! >= this.pills().length) { this.inputElement()?.nativeElement.focus(); this.activePillIndex.set(undefined); } } } @HostListener('keydown.backspace') @HostListener('keydown.delete') protected delete(): void { const activePillIndex = this.activePillIndex(); const count = this.pills().length; if (activePillIndex !== undefined && !this.readonly()) { const targetIndex = count > 1 ? Math.min(activePillIndex, count - 2) : undefined; this.remove(activePillIndex, targetIndex === undefined); this.activePillIndex.set(targetIndex); } } /** @internal */ registerOnChange(fn: any): void { this.onChange = fn; } /** @internal */ registerOnTouched(fn: () => void): void { this.onTouched = fn; } /** @internal */ setDisabledState(isDisabled: boolean): void { this.disabledNgControl.set(isDisabled); } /** @internal */ writeValue(value?: string[] | null): void { this.pills.set(value?.slice() ?? []); } } |