File

libs/acl/angular/src/lib/policy-editor/edit-selection/edit-selection.component.ts

Implements

OnChanges OnInit

Metadata

Index

Properties
Methods
Inputs
Outputs

Inputs

choices
Type : string[]
Default value : []
value
Type : string

Outputs

update
Type : EventEmitter

Methods

handleDataUpdated
handleDataUpdated()
Returns : void

Properties

editableComponent
Type : EditableComponent
Decorators :
@ViewChild('editableComponent')
inputControl
Type : UntypedFormControl
Default value : new UntypedFormControl(null)
import {
  Component,
  DestroyRef,
  EventEmitter,
  Input,
  OnChanges,
  OnInit,
  Output,
  SimpleChanges,
  ViewChild,
  inject
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { UntypedFormControl } from '@angular/forms';
import { tap } from 'rxjs/operators';
import { EditableComponent } from '../editable/editable.component';

@Component({
  // eslint-disable-next-line @angular-eslint/component-selector
  selector: 'edit-selection',
  templateUrl: './edit-selection.component.html',
  styleUrls: ['./edit-selection.component.html'],
  standalone: false
})
export class EditSelectionComponent implements OnChanges, OnInit {
  @Input() choices: string[] = [];
  @Input() value!: string;
  @Output() update = new EventEmitter();

  @ViewChild('editableComponent')
  editableComponent!: EditableComponent;
  inputControl: UntypedFormControl = new UntypedFormControl(null);

  private destroyRef = inject(DestroyRef);

  handleDataUpdated() {
    const newValue = this.inputControl.value;
    if (newValue !== this.value) {
      this.update.emit(newValue);
    }
  }

  ngOnChanges(changes: SimpleChanges): void {
    if (changes['value']) {
      this.inputControl.setValue(changes['value'].currentValue, { emitEvent: false });
    }
  }

  ngOnInit(): void {
    /**
     * auto close after a selection
     */
    this.inputControl.valueChanges
      .pipe(
        takeUntilDestroyed(this.destroyRef),
        tap(() => {
          this.editableComponent.toViewMode();
        })
      )
      .subscribe();
  }
}
<acl-editable (editComplete)="handleDataUpdated()" #editableComponent>
  <ng-template aclViewMode>
    <span>{{ value || '-' }}</span>
  </ng-template>
  <ng-template aclEditMode>
    <select class="inspector-select" focusable [formControl]="inputControl">
      <option *ngFor="let choice of choices" [value]="choice">{{ choice }}</option>
    </select>
  </ng-template>
</acl-editable>

./edit-selection.component.html

<acl-editable (editComplete)="handleDataUpdated()" #editableComponent>
  <ng-template aclViewMode>
    <span>{{ value || '-' }}</span>
  </ng-template>
  <ng-template aclEditMode>
    <select class="inspector-select" focusable [formControl]="inputControl">
      <option *ngFor="let choice of choices" [value]="choice">{{ choice }}</option>
    </select>
  </ng-template>
</acl-editable>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""