1 import { Component, Input } from '@angular/core';
2 import { CommonModule } from '@angular/common';
3 import { ControlValueAccessor, NG_VALUE_ACCESSOR, ReactiveFormsModule } from '@angular/forms';
4 import { ButtonModule, GridModule, IconModule, InputModule } from 'carbon-components-angular';
5 import { ComponentsModule } from '../components.module';
8 selector: 'cd-text-label-list',
19 templateUrl: './text-label-list.component.html',
20 styleUrl: './text-label-list.component.scss',
23 provide: NG_VALUE_ACCESSOR,
24 useExisting: TextLabelListComponent,
29 export class TextLabelListComponent implements ControlValueAccessor {
30 // Label for the text list.
31 @Input() label: string = '';
32 // Optional helper text that appears under the label.
33 @Input() helperText: string = '';
34 // Value displayed if no item is present.
35 @Input() placeholder: string = '';
37 values: string[] = [''];
38 disabled: boolean = false;
39 touched: boolean = false;
41 private onChange: (value: string[]) => void = () => {};
42 private onTouched: () => void = () => {};
44 private _isRemovingChar(originalValue: string, value: string): boolean {
45 return originalValue.slice(0, -1) === value;
48 writeValue(value: string[]): void {
49 this.values = value?.length ? [...value, ''] : [''];
52 registerOnChange(onChange: (value: string[]) => void): void {
53 this.onChange = onChange;
56 registerOnTouched(onTouched: () => void): void {
57 this.onTouched = onTouched;
60 setDisabledState(disabled: boolean): void {
61 this.disabled = disabled;
64 onInputChange(index: number, value: string) {
65 const originalValue = this.values[index];
66 this.values[index] = value;
69 !this._isRemovingChar(originalValue, value) &&
70 index === this.values.length - 1 &&
76 const nonEmptyValues = this.values.filter((v) => v.trim() !== '');
77 this.onChange(nonEmptyValues.length ? nonEmptyValues : []);
89 deleteInput(index: number) {
90 this.values.splice(index, 1);
92 if (this.values.length === 0) {
96 const nonEmpty = this.values.filter((v) => v.trim() !== '');
97 this.onChange(nonEmpty.length ? nonEmpty : []);