All files / src/app/shared/formly/wrappers/maxlength-description-wrapper maxlength-description-wrapper.component.ts

100% Statements 11/11
80% Branches 8/10
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 441x 1x 1x 1x 1x                               1x     4x 4x       4x     5x         5x              
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FieldWrapper } from '@ngx-formly/core';
import { TranslateService } from '@ngx-translate/core';
import { Observable, of } from 'rxjs';
import { startWith, switchMap, throttleTime } from 'rxjs/operators';
 
/**
 * Wrapper to display a description that counts the remaining characters in a field.
 *
 * @props **maxLength** - will be used to determine the remaining available characters (required, otherwise the counter description will not be rendered).
 * @props **maxLengthDescription** - an alternative translation key that can be used to customize the counter description (default: 'textarea.max_limit').
 *
 * @usageNotes
 * This wrapper is made for the textarea type (and assigned to 'ish-textarea-field' by default) but could be used for different field types as well.
 */
@Component({
  selector: 'ish-maxlength-description-wrapper',
  templateUrl: './maxlength-description-wrapper.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class MaxlengthDescriptionWrapperComponent extends FieldWrapper implements OnInit {
  description$: Observable<string>;
 
  constructor(private translate: TranslateService) {
    super();
  }
 
  ngOnInit() {
    this.description$ = this.formControl.valueChanges.pipe(
      startWith(this.formControl.value),
      throttleTime(500, undefined, { leading: true, trailing: true }),
      switchMap(value => this.getDescription$(value))
    );
  }
 
  private getDescription$(value: string): Observable<string> {
    return this.props.maxLength
      ? this.translate.get(this.props.maxLengthDescription ?? 'textarea.max_limit', {
          0: Math.max(0, this.props.maxLength - (value?.length ?? 0)),
        })
      : of(undefined);
  }
}