### File

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

### Implements

`AfterViewInit` `OnDestroy`

### Index

###### Properties

- Protected deletedControl
- 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:34

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:45

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

### Methods

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

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

### Properties

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

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

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

### Accessors

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

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

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

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

    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 {
      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 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.formGroup.parent as UntypedFormArray).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 instanceof RxapFormControl) {
            this.deletedControl = this.formGroup.controls.deleted;
          } else {
            this.deletedControl = new RxapFormControl(false, { controlId: 'deleted' });
            this.formGroup.addControl('deleted', this.deletedControl);
          }
          if (!(this.formGroup.parent instanceof UntypedFormArray)) {
            throw new Error(`The parent of the FormGroup is not a FormArray 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 ""