src/app/components/graph-editor/symbol-editor/symbol-editor.component.ts
Component for editing a set of symbols. Title and validation are provided by the configuration.
OnChanges
selector | apollo-symbol-editor[symbols][config] |
styleUrls | ./symbol-editor.component.scss |
templateUrl | ./symbol-editor.component.html |
Properties |
|
Methods |
|
Inputs |
Outputs |
Public
constructor(log: NGXLogger)
|
||||||
Parameters :
|
config | |
Type : SymbolEditorConfiguration
|
|
symbols | |
Type : Set<string>
|
|
symbolsUpdated | |
Type : EventEmitter
|
|
Public addSymbol | ||||||
addSymbol(symbolAddedEvent: MatChipInputEvent)
|
||||||
Parameters :
Returns :
void
|
Public ngOnChanges | ||||||
ngOnChanges(changes: SimpleChanges)
|
||||||
Parameters :
Returns :
void
|
Public removeSymbol | ||||||
removeSymbol(symbol: string)
|
||||||
Parameters :
Returns :
void
|
Public formControl |
Type : UntypedFormControl
|
Default value : new UntypedFormControl('')
|
import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core';
import { UntypedFormControl, Validators } from '@angular/forms';
import { MatLegacyChipInputEvent as MatChipInputEvent } from '@angular/material/legacy-chips';
import { NGXLogger } from 'ngx-logger';
import { SymbolEditorConfiguration } from 'src/app/configurations/symbol-editor.configuration';
/**
* Component for editing a set of symbols.
* Title and validation are provided by the configuration.
*/
@Component({
selector: 'apollo-symbol-editor[symbols][config]',
templateUrl: './symbol-editor.component.html',
styleUrls: ['./symbol-editor.component.scss'],
})
export class SymbolEditorComponent implements OnChanges {
@Input() public symbols!: Set<string>;
@Input() public config!: SymbolEditorConfiguration;
@Output() public readonly symbolsUpdated = new EventEmitter();
public formControl: UntypedFormControl = new UntypedFormControl('');
public constructor(private readonly log: NGXLogger) {}
public ngOnChanges(changes: SimpleChanges): void {
if (changes.config !== undefined) {
this.formControl = new UntypedFormControl('', Validators.pattern(this.config.symbolPattern));
}
}
public addSymbol(symbolAddedEvent: MatChipInputEvent): void {
if (this.formControl.invalid) {
return;
}
symbolAddedEvent.value
.split(',')
.map((symbol) => symbol.trim())
.forEach((symbol) => {
if (symbol.length >= 1) {
this.log.debug(`Adding symbol ${symbol}.`);
symbolAddedEvent.input.value = '';
this.symbols.add(symbol);
this.symbolsUpdated.emit();
}
});
}
public removeSymbol(symbol: string): void {
this.log.debug(`Removing symbol ${symbol}.`);
this.symbols.delete(symbol);
this.symbolsUpdated.emit();
}
}
<mat-form-field class="symbol-chip-input">
<mat-label>{{ config.title | translate }}</mat-label>
<mat-chip-list #chipList>
<mat-chip *ngFor="let symbol of symbols" [selectable]="false" [removable]="true" (removed)="removeSymbol(symbol)">
{{ symbol }}
<mat-icon matChipRemove>cancel</mat-icon>
</mat-chip>
<input
placeholder="{{ config.placeholder | translate }}"
[matChipInputFor]="chipList"
(matChipInputTokenEnd)="addSymbol($event)"
autocomplete="off"
[formControl]="formControl"
/>
<mat-error *ngIf="formControl.invalid">{{ config.patternError | translate }}</mat-error>
</mat-chip-list>
</mat-form-field>
./symbol-editor.component.scss
:host {
display: block;
}
.symbol-chip-input {
width: 100%;
}