thingsboard/ui-ngx/src/app/shared/components/string-items-list.component.ts

152 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-02-02 15:25:04 +02:00
///
/// Copyright © 2016-2022 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { Component, forwardRef, Input } from '@angular/core';
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { MatChipInputEvent } from '@angular/material/chips';
import { COMMA, ENTER, SEMICOLON } from '@angular/cdk/keycodes';
2023-02-13 16:11:23 +02:00
import { FloatLabelType, MatFormFieldAppearance } from '@angular/material/form-field/form-field';
2023-02-02 15:25:04 +02:00
@Component({
2023-02-13 16:11:23 +02:00
selector: 'tb-string-items-list',
templateUrl: './string-items-list.component.html',
2023-02-02 15:25:04 +02:00
styleUrls: [],
providers: [
{
provide: NG_VALUE_ACCESSOR,
2023-02-13 16:11:23 +02:00
useExisting: forwardRef(() => StringItemsListComponent),
2023-02-02 15:25:04 +02:00
multi: true
}
]
})
2023-02-13 16:11:23 +02:00
export class StringItemsListComponent implements ControlValueAccessor{
2023-02-02 15:25:04 +02:00
2023-02-13 16:11:23 +02:00
stringItemsForm: FormGroup;
2023-02-02 15:25:04 +02:00
private modelValue: Array<string> | null;
readonly separatorKeysCodes: number[] = [ENTER, COMMA, SEMICOLON];
private requiredValue: boolean;
get required(): boolean {
return this.requiredValue;
}
@Input()
set required(value: boolean) {
const newVal = coerceBooleanProperty(value);
if (this.requiredValue !== newVal) {
this.requiredValue = newVal;
this.updateValidators();
}
}
@Input()
disabled: boolean;
2023-02-13 16:11:23 +02:00
@Input()
label: string;
@Input()
placeholder: string;
@Input()
hint: string;
@Input()
requiredText: string;
@Input()
floatLabel: FloatLabelType = 'auto';
@Input()
appearance: MatFormFieldAppearance = 'standard';
2023-02-02 15:25:04 +02:00
private propagateChange = (v: any) => { };
constructor(private fb: FormBuilder) {
2023-02-13 16:11:23 +02:00
this.stringItemsForm = this.fb.group({
items: [null, this.required ? [Validators.required] : []]
2023-02-02 15:25:04 +02:00
});
}
updateValidators() {
2023-02-13 16:11:23 +02:00
this.stringItemsForm.get('items').setValidators(this.required ? [Validators.required] : []);
this.stringItemsForm.get('items').updateValueAndValidity();
2023-02-02 15:25:04 +02:00
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
if (isDisabled) {
2023-02-13 16:11:23 +02:00
this.stringItemsForm.disable({emitEvent: false});
2023-02-02 15:25:04 +02:00
} else {
2023-02-13 16:11:23 +02:00
this.stringItemsForm.enable({emitEvent: false});
2023-02-02 15:25:04 +02:00
}
}
writeValue(value: Array<string> | null): void {
if (value != null && value.length > 0) {
this.modelValue = [...value];
2023-02-13 16:11:23 +02:00
this.stringItemsForm.get('items').setValue(value);
2023-02-02 15:25:04 +02:00
} else {
2023-02-13 16:11:23 +02:00
this.stringItemsForm.get('items').setValue(null);
2023-02-02 15:25:04 +02:00
this.modelValue = null;
}
}
2023-02-13 16:11:23 +02:00
addItem(event: MatChipInputEvent): void {
let item = event.value || '';
2023-02-02 15:25:04 +02:00
const input = event.chipInput.inputElement;
2023-02-13 16:11:23 +02:00
item = item.trim();
if (item) {
if (!this.modelValue || this.modelValue.indexOf(item) === -1) {
2023-02-02 15:25:04 +02:00
if (!this.modelValue) {
this.modelValue = [];
}
2023-02-13 16:11:23 +02:00
this.modelValue.push(item);
this.stringItemsForm.get('items').setValue(this.modelValue);
2023-02-02 15:25:04 +02:00
}
this.propagateChange(this.modelValue);
if (input) {
input.value = '';
}
}
}
2023-02-13 16:11:23 +02:00
removeItems(item: string) {
const index = this.modelValue.indexOf(item);
2023-02-02 15:25:04 +02:00
if (index >= 0) {
this.modelValue.splice(index, 1);
if (!this.modelValue.length) {
this.modelValue = null;
}
2023-02-13 16:11:23 +02:00
this.stringItemsForm.get('items').setValue(this.modelValue);
2023-02-02 15:25:04 +02:00
this.propagateChange(this.modelValue);
}
}
2023-02-13 16:11:23 +02:00
get stringItemsList(): string[] {
return this.stringItemsForm.get('items').value;
2023-02-02 15:25:04 +02:00
}
}