File

projects/davita/bridge-library/fields/textarea/textarea-fields.component.ts

Implements

OnInit OnChanges

Metadata

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor()

Inputs

errorText
Type : string | undefined
expandable
Type : boolean
Default value : false
formatErrorText
Type : string | undefined
helperText
Type : string | undefined
hideFloatTextAreaLabel
Type : boolean
Default value : false
hideOutlineLabel
Type : boolean
Default value : false
inputModeKeyboard
Type : string
Default value : ''
label
Type : string
Default value : 'Label'
maxLength
Type : number
Default value : 100
maxLengthErrorText
Type : string | undefined
maxRow
Type : number
Default value : 28
minRow
Type : number
Default value : 1
patternKeyboard
Type : string
Default value : ''
requiredFieldErrorText
Type : string | undefined
showCounterWhileTyping
Type : boolean
Default value : true
showErrorIcon
Type : boolean
Default value : true
showMaxLengthErrorText
Type : boolean
Default value : true
textareaFormControl
Type : FormControl
textAreaPlaceholder
Type : string
Default value : ''
typeKeyboard
Type : string
Default value : ''
units
Type : string | undefined
validators
Type : any | undefined

Outputs

enter
Type : EventEmitter<any>

Methods

Private addMoreValidatorsToControls
addMoreValidatorsToControls()
Returns : void
ngOnChanges
ngOnChanges(changes: any)
Parameters :
Name Type Optional
changes any No
Returns : void
ngOnInit
ngOnInit()
Returns : void
onEnter
onEnter(event?: any)
Parameters :
Name Type Optional
event any Yes
Returns : void
shouldDislayMatHintForTextArea
shouldDislayMatHintForTextArea()
Returns : any
validateField
validateField()
Returns : void

Properties

textareaField
Type : ElementRef<any>
Decorators :
@ViewChild('textareaField')
import { FocusMonitor } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import {
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  Inject,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  Optional,
  Output,
  Self,
  SimpleChanges,
  ViewChild,
  ViewEncapsulation,
} from '@angular/core';
import {
  AbstractControl,
  ControlValueAccessor,
  FormBuilder,
  FormControl,
  NgControl,
  Validators,
  ValidationErrors,
  ValidatorFn,
  FormGroup,
} from '@angular/forms';
import { MAT_FORM_FIELD, MatFormField, MatFormFieldControl } from '@angular/material/form-field';
import { Subject, tap } from 'rxjs';

@Component({
  selector: 'bri-textarea-fields',
  templateUrl: './textarea-fields.component.html',
  styleUrls: ['./textarea-fields.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class BriTextareaFieldsComponent implements OnInit, OnChanges {
  @ViewChild('textareaField')
  textareaField!: ElementRef<any>;

  @Input() label: string = 'Label';
  @Input() hideOutlineLabel: boolean = false;
  @Input() units: string | undefined;
  @Input() maxLength: number = 100;
  @Input() showMaxLengthErrorText = true;
  @Input() showCounterWhileTyping: boolean = true;
  @Input() textAreaPlaceholder: string = '';
  @Input() helperText: string | undefined;
  @Input() errorText: string | undefined;
  @Input() formatErrorText: string | undefined;
  @Input() requiredFieldErrorText: string | undefined;
  @Input() maxLengthErrorText: string | undefined;
  @Input() validators: any | undefined;
  @Input() hideFloatTextAreaLabel: boolean = false;
  @Input() showErrorIcon: boolean = true;
  @Input() inputModeKeyboard: string = '';
  @Input() typeKeyboard: string = '';
  @Input() patternKeyboard: string = '';
  @Input() expandable: boolean = false;
  @Input() minRow: number = 1;
  @Input() maxRow: number = 28;

  @Output() enter: EventEmitter<any> = new EventEmitter<any>();

  @Input() textareaFormControl!: FormControl;

  constructor() {}

  ngOnChanges(changes: any): void {}

  ngOnInit(): void {
    if (this.validators) {
      this.addMoreValidatorsToControls();
    }
  }

  private addMoreValidatorsToControls() {
    this.textareaFormControl.addValidators(this.validators);
    this.textareaFormControl.updateValueAndValidity();
  }

  validateField() {
    if (this.requiredFieldErrorText) {
      this.textareaFormControl.addValidators(Validators.required);
      this.textareaFormControl.updateValueAndValidity();
    }
  }

  onEnter(event?: any) {
    if (this.textareaFormControl.errors) {
      event.preventDefault();
    } else {
      this.textareaField.nativeElement.blur();
      this.enter.emit(this.textareaFormControl.value);
    }
  }

  shouldDislayMatHintForTextArea() {
    return (
      (this.helperText &&
        !(this.maxLengthErrorText && this.textareaFormControl.hasError('maxlength')) &&
        !(this.textareaFormControl.errors && !this.textareaFormControl.hasError('maxlength'))) ||
      (this.requiredFieldErrorText && this.textareaFormControl.hasError('required')) ||
      (this.maxLengthErrorText && this.textareaFormControl.hasError('maxlength')) ||
      (!(
        this.textareaFormControl.hasError('required') ||
        this.textareaFormControl.hasError('maxlength')
      ) &&
        this.textareaFormControl.errors) ||
      this.maxLength ||
      this.maxLengthErrorText
    );
  }
}
<div class="mat-typography bri-fields">
  <div [ngClass]="expandable ? 'expandable-textarea' : 'textarea'">
    <mat-form-field
      appearance="outline"
      [ngClass]="{ 'no-hint': !shouldDislayMatHintForTextArea() }"
    >
      <mat-label *ngIf="!hideFloatTextAreaLabel" class="mat-subtitle-1" errorState="true">{{
        label
      }}</mat-label>
      <textarea
        matInput
        #textareaField
        class="mat-subtitle-1"
        [placeholder]="textAreaPlaceholder"
        [formControl]="textareaFormControl"
        (keydown.enter)="onEnter($event)"
        (blur)="validateField()"
        cdkTextareaAutosize
        #autosize="cdkTextareaAutosize"
        [cdkAutosizeMinRows]="minRow"
        [cdkAutosizeMaxRows]="maxRow"
      ></textarea>
      <mat-hint class="mat-caption hint" *ngIf="shouldDislayMatHintForTextArea()">
        <div
          class="helper-txt"
          *ngIf="
            helperText &&
            !(maxLengthErrorText && textareaFormControl.hasError('maxlength')) &&
            !(textareaFormControl.errors && !textareaFormControl.hasError('maxlength'))
          "
        >
          {{ helperText }}
        </div>
        <div
          class="error-txt"
          *ngIf="requiredFieldErrorText && textareaFormControl.hasError('required')"
        >
          {{ requiredFieldErrorText }}
        </div>
        <div
          class="error-txt"
          *ngIf="maxLengthErrorText && textareaFormControl.hasError('maxlength')"
        >
          {{ maxLengthErrorText }}
        </div>
        <div
          class="error-txt"
          *ngIf="
            !(
              textareaFormControl.hasError('required') || textareaFormControl.hasError('maxlength')
            ) && textareaFormControl.errors
          "
        >
          {{ errorText }}
        </div>
        <div
          [class]="helperText && maxLengthErrorText ? '' : 'char-length'"
          *ngIf="showMaxLengthErrorText && (maxLength || maxLengthErrorText)"
        >
          {{ textareaFormControl.value?.length || 0 }}/{{ maxLength }}
        </div>
      </mat-hint>
    </mat-form-field>
  </div>
</div>

./textarea-fields.component.scss

@import '../../scss/style/colors';
.textarea {
  width: 100%;
  .mat-mdc-form-field-infix {
    min-height: unset;
    height: auto !important;
  }
  .mat-mdc-form-field {
    textarea {
      resize: none;
    }
  }
  .mat-mdc-text-field-wrapper {
    .mat-mdc-form-field-flex {
      .mat-mdc-input-element {
        margin-top: 6px !important;
        height: 136px !important;
        padding: 0;
      }
    }
  }
}
.expandable-textarea {
  width: 100%;
  .mat-mdc-form-field-infix {
    min-height: unset;
    height: auto !important;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""