File

src/app/components/graph-editor/symbol-editor/symbol-editor.component.ts

Description

Component for editing a set of symbols. Title and validation are provided by the configuration.

Implements

OnChanges

Metadata

Index

Properties
Methods
Inputs
Outputs

Constructor

Public constructor(log: NGXLogger)
Parameters :
Name Type Optional
log NGXLogger No

Inputs

config
Type : SymbolEditorConfiguration
symbols
Type : Set<string>

Outputs

symbolsUpdated
Type : EventEmitter

Methods

Public addSymbol
addSymbol(symbolAddedEvent: MatChipInputEvent)
Parameters :
Name Type Optional
symbolAddedEvent MatChipInputEvent No
Returns : void
Public ngOnChanges
ngOnChanges(changes: SimpleChanges)
Parameters :
Name Type Optional
changes SimpleChanges No
Returns : void
Public removeSymbol
removeSymbol(symbol: string)
Parameters :
Name Type Optional
symbol string No
Returns : void

Properties

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%;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""