### File

`src/lib/directives/array/form-array-item-button.ts`

### Implements

`AfterViewInit` `OnDestroy`

### Index

###### Properties
Protected deletedControl Protected formArray Protected formGroup type
###### Methods
Protected Abstract onClick Protected Abstract updateDisplayStyle
###### HostBindings
disabled type
###### Accessors
disabled isDeleted index isDisabled

### Constructor

`constructor(parent: ControlContainer, matButton: MatButton, renderer: Renderer2, elementRef: ElementRef)`
Defined in src/lib/directives/array/form-array-item-button.ts:36
Parameters :
Name | Type | Optional
parent | `ControlContainer` | No
matButton | `MatButton` | No
renderer | `Renderer2` | No
elementRef | `ElementRef` | No

### HostBindings

disabled
Type : `boolean`
Defined in src/lib/directives/array/form-array-item-button.ts:47

type
Type : `string`
Default value : `'button'`
Defined in src/lib/directives/array/form-array-item-button.ts:31

### Methods

Protected Abstract onClick
`onClick()`
Defined in src/lib/directives/array/form-array-item-button.ts:99
Returns : `void`

Protected Abstract updateDisplayStyle
`updateDisplayStyle()`
Defined in src/lib/directives/array/form-array-item-button.ts:101
Returns : `void`

### Properties

Protected deletedControl
Type : `RxapFormControl<boolean>`
Defined in src/lib/directives/array/form-array-item-button.ts:34

Protected formArray
Type : `RxapFormArray`
Defined in src/lib/directives/array/form-array-item-button.ts:33

Protected formGroup
Type : `RxapFormGroup<literal type>`
Defined in src/lib/directives/array/form-array-item-button.ts:32

type
Type : `string`
Default value : `'button'`
Decorators : `@HostBinding('type')`
Defined in src/lib/directives/array/form-array-item-button.ts:31

### Accessors

disabled
get `disabled()`
Defined in src/lib/directives/array/form-array-item-button.ts:47

isDeleted
get `isDeleted()`
Defined in src/lib/directives/array/form-array-item-button.ts:51

index
get `index()`
Defined in src/lib/directives/array/form-array-item-button.ts:55

isDisabled
get `isDisabled()`
Defined in src/lib/directives/array/form-array-item-button.ts:59

    import {
      AfterViewInit,
      Directive,
      ElementRef,
      HostBinding,
      OnDestroy,
      Optional,
      Renderer2,
      SkipSelf,
    } from '@angular/core';
    import {
      ControlContainer,
      UntypedFormArray,
    } from '@angular/forms';
    import { MatButton } from '@angular/material/button';
    import {
      RxapFormArray,
      RxapFormControl,
      RxapFormGroup,
    } from '@rxap/forms';
    import {
      Subscription,
      tap,
    } from 'rxjs';

    // TODO : move to rxap packages
    @Directive()
    export abstract class FormArrayItemButton implements AfterViewInit, OnDestroy {

      @HostBinding('type')
      type = 'button';
      protected formGroup!: RxapFormGroup<{ deleted: boolean }>;
      protected formArray!: RxapFormArray;
      protected deletedControl!: RxapFormControl<boolean>;
      private _controlDisabledSubscription?: Subscription;
      private _deletedControlValueSubscription?: Subscription;

      constructor(
        @SkipSelf() protected readonly parent: ControlContainer,
        @Optional() protected readonly matButton: MatButton,
        protected readonly renderer: Renderer2,
        protected readonly elementRef: ElementRef,
      ) {
      }

      @HostBinding('disabled')
      get disabled() {
        return this.isDisabled;
      }

      get isDeleted(): boolean {
        return this.deletedControl.value;
      }

      get index(): number {
        return this.formArray.controls.findIndex(control => control === this.formGroup);
      }

      protected get isDisabled(): boolean {
        return !!this.formGroup?.parent?.disabled;
      }

      ngAfterViewInit() {
        if (this.parent.control instanceof RxapFormGroup) {
          this.formGroup = this.parent.control;
          if (this.formGroup.controls.deleted) {
            if (this.formGroup.controls.deleted instanceof RxapFormControl) {
              this.deletedControl = this.formGroup.controls.deleted;
            } else {
              throw new Error(`The parent FormGroup instance does have a 'deleted' control, but it is not a RxapFormControl instance`);
            }
          } else {
            this.deletedControl = new RxapFormControl(false, { controlId: 'deleted' });
            this.formGroup.addControl('deleted', this.deletedControl);
          }
          if (this.formGroup.parent instanceof RxapFormArray) {
            this.formArray = this.formGroup.parent;
          } else {
            throw new Error(`The parent of the FormGroup is not a RxapFormArray instance`);
          }
        } else {
          throw new Error(`The parent is not a form group instance`);
        }
        if (this.matButton) {
          this._controlDisabledSubscription = this.formGroup.disabled$.pipe(
            tap(isDisabled => this.matButton.disabled = isDisabled),
          ).subscribe();
        }
        this.deletedControl.value$.pipe(
          tap(() => this.updateDisplayStyle()),
        ).subscribe();
      }

      ngOnDestroy() {
        this._controlDisabledSubscription?.unsubscribe();
        this._deletedControlValueSubscription?.unsubscribe();
      }

      protected abstract onClick(): void;

      protected abstract updateDisplayStyle(): void;

    }

# results matching ""

# No results matching ""