File

src/components/sq-textarea/sq-textarea.component.ts

Description

Represents a textarea input component with various configuration options.

Look the link about the component in original framework and the appearance

See https://css.squidit.com.br/forms/textarea

Example :
<sq-textarea [name]="'description'" [id]="'description'" [label]="'Description'"[placeholder]="'Enter a description...'" [(value)]="text"></sq-textarea>

Metadata

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(validatorHelper: ValidatorHelper, element: ElementRef, translate: TranslateService)

Constructor for the SqInputComponent class.

Parameters :
Name Type Optional Description
validatorHelper ValidatorHelper No
  • The ValidatorHelper service for input validation.
element ElementRef No
  • The ElementRef representing the input element.
translate TranslateService No
  • The TranslateService for internationalization (optional).

Inputs

backgroundColor
Type : string
Default value : ''

The background color of the textarea.

borderColor
Type : string
Default value : ''

The border color of the textarea.

customClass
Type : string
Default value : ''

Additional CSS classes for styling the textarea.

disabled
Type : boolean
Default value : false

Flag to disable the textarea.

errorSpan
Type : boolean
Default value : true

Flag to show an error span.

externalError
Type : string
Default value : ''

External error message to display for the textarea.

externalIcon
Type : string
Default value : ''

External icon to display for the textarea.

id
Type : string

The id attribute of the textarea.

label
Type : string

The label to display for the textarea.

labelColor
Type : string
Default value : ''

The color of the textarea label.

maxLength
Type : number | null
Default value : null

The maximum length of the textarea.

name
Type : string
Default value : `random-name-${(1 + Date.now() + Math.random()).toString().replace('.', '')}`

The name attribute of the textarea.

placeholder
Type : string
Default value : ''

The placeholder text to display in the textarea.

readonly
Type : boolean
Default value : false

Flag to make the textarea readonly.

required
Type : boolean
Default value : false

Flag to mark the textarea as required.

timeToChange
Type : number
Default value : 0

The time interval for input timeout in ms.

tooltipColor
Type : string
Default value : 'inherit'

The color of the tooltip.

tooltipIcon
Type : string
Default value : ''

The icon to display in the tooltip.

tooltipMessage
Type : string
Default value : ''

The tooltip message to display.

tooltipPlacement
Type : "center top" | "center bottom" | "left center" | "right center"
Default value : 'right center'

The placement of the tooltip.

useFormErrors
Type : boolean
Default value : true

Flag to use form errors for validation.

value
Type : any
Default value : ''

The initial value of the textarea.

Outputs

inFocus
Type : EventEmitter<boolean>

Event emitted when the textarea gains or loses focus.

keyPressDown
Type : EventEmitter<KeyboardEvent>

Event emitted when a key is pressed down in the textarea.

keyPressUp
Type : EventEmitter<KeyboardEvent>

Event emitted when a key is released in the textarea.

valid
Type : EventEmitter<boolean>

Event emitter for validation status.

valueChange
Type : EventEmitter<any>

Event emitted when the textarea value changes.

Methods

Async change
change(event: any)

Handles changes to the textarea value.

Parameters :
Name Type Optional Description
event any No
  • The new value of the textarea.
Returns : any
keyDown
keyDown(event: KeyboardEvent)

Handle keydown events.

Parameters :
Name Type Optional Description
event KeyboardEvent No
  • The keydown event.
Returns : void
keyUp
keyUp(event: KeyboardEvent)

Handle keyup events.

Parameters :
Name Type Optional Description
event KeyboardEvent No
  • The keyup event.
Returns : void
Async setError
setError(key: string)

Asynchronously sets an error message.

Parameters :
Name Type Optional Description
key string No
  • The key for the error message.
Returns : any
Async validate
validate(isBlur)

Asynchronously validates the input value.

Parameters :
Name Optional Default value
isBlur No false
Returns : any

Properties

Public element
Type : ElementRef
- The ElementRef representing the input element.
error
Type : boolean | string
Default value : false

Represents the error state of the textarea.

labelTemplate
Type : TemplateRef<HTMLElement> | null
Default value : null
Decorators :
@ContentChild('labelTemplate')

Reference to a label template.

leftLabel
Type : TemplateRef<HTMLElement> | null
Default value : null
Decorators :
@ContentChild('leftLabel')

Reference to a left-aligned label template.

nativeElement
Type : ElementRef

Reference to the native element of the textarea.

rightLabel
Type : TemplateRef<HTMLElement> | null
Default value : null
Decorators :
@ContentChild('rightLabel')

Reference to a right-aligned label template.

timeoutInput
Type : ReturnType<>

Timeout for input changes.

Public translate
Type : TranslateService
Decorators :
@Optional()
- The TranslateService for internationalization (optional).
Public validatorHelper
Type : ValidatorHelper
- The ValidatorHelper service for input validation.
import { Component, ContentChild, ElementRef, EventEmitter, Input, Optional, Output, TemplateRef } from '@angular/core';
import { NgClass, NgStyle, NgTemplateOutlet } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ValidatorHelper } from '../../helpers/validator.helper';
import { TranslateService } from '@ngx-translate/core';
import { SqTooltipComponent } from '../sq-tooltip/sq-tooltip.component';
import { UniversalSafePipe } from '../../pipes/universal-safe/universal-safe.pipe';

/**
 * Represents a textarea input component with various configuration options.
 *
 * Look the link about the component in original framework and the appearance
 *
 * @see {@link https://css.squidit.com.br/forms/textarea}
 *
 * <div class='col-6 my-3'>
 *  <label class='display-block' for='textarea-text'>
 *    Label
 *  </label>
 *  <div class='wrapper-input mb-3'>
 *    <textarea
 *      class='display-block textarea'
 *      type='text'
 *      name='textarea-text'
 *      id='textarea-text'
 *      placeholder='Placeholder'
 *    ></textarea>
 *  </div>
 *</div>
 *
 * @example
 * <sq-textarea [name]="'description'" [id]="'description'" [label]="'Description'"[placeholder]="'Enter a description...'" [(value)]="text"></sq-textarea>
 *
 */
@Component({
  selector: 'sq-textarea',
  templateUrl: './sq-textarea.component.html',
  styleUrls: ['./sq-textarea.component.scss'],
  standalone: true,
  imports: [NgClass, NgStyle, NgTemplateOutlet, FormsModule, SqTooltipComponent, UniversalSafePipe],
})
export class SqTextAreaComponent {
  /**
   * The name attribute of the textarea.
   *
   * @default 'random-name-[hash-random-code]'
   */
  @Input() name = `random-name-${(1 + Date.now() + Math.random()).toString().replace('.', '')}`;

  /**
   * The id attribute of the textarea.
   */
  @Input() id?: string;

  /**
   * The label to display for the textarea.
   */
  @Input() label?: string;

  /**
   * Additional CSS classes for styling the textarea.
   */
  @Input() customClass = '';

  /**
   * The placeholder text to display in the textarea.
   */
  @Input() placeholder = '';

  /**
   * External error message to display for the textarea.
   */
  @Input() externalError = '';

  /**
   * External icon to display for the textarea.
   */
  @Input() externalIcon = '';

  /**
   * The initial value of the textarea.
   */
  @Input() value: any = '';

  /**
   * The time interval for input timeout in ms.
   */
  @Input() timeToChange = 0;

  /**
   * Flag to show an error span.
   */
  @Input() errorSpan = true;

  /**
   * Flag to disable the textarea.
   */
  @Input() disabled = false;

  /**
   * Flag to make the textarea readonly.
   */
  @Input() readonly = false;

  /**
   * Flag to mark the textarea as required.
   */
  @Input() required = false;

  /**
   * Flag to use form errors for validation.
   */
  @Input() useFormErrors = true;

  /**
   * The tooltip message to display.
   */
  @Input() tooltipMessage = '';

  /**
   * The placement of the tooltip.
   */
  @Input() tooltipPlacement: 'center top' | 'center bottom' | 'left center' | 'right center' = 'right center';

  /**
   * The color of the tooltip.
   */
  @Input() tooltipColor = 'inherit';

  /**
   * The icon to display in the tooltip.
   */
  @Input() tooltipIcon = '';

  /**
   * The background color of the textarea.
   */
  @Input() backgroundColor = '';

  /**
   * The border color of the textarea.
   */
  @Input() borderColor = '';

  /**
   * The color of the textarea label.
   */
  @Input() labelColor = '';

  /**
   * The maximum length of the textarea.
   */
  @Input() maxLength: number | null = null;

  /**
   * Event emitted when a key is pressed down in the textarea.
   */
  @Output() keyPressDown: EventEmitter<KeyboardEvent> = new EventEmitter();

  /**
   * Event emitted when a key is released in the textarea.
   */
  @Output() keyPressUp: EventEmitter<KeyboardEvent> = new EventEmitter();

  /**
   * Event emitted when the textarea gains or loses focus.
   */
  @Output() inFocus: EventEmitter<boolean> = new EventEmitter();

  /**
   * Event emitted when the textarea value changes.
   */
  @Output() valueChange: EventEmitter<any> = new EventEmitter();

  /**
   * Event emitter for validation status.
   */
  @Output() valid: EventEmitter<boolean> = new EventEmitter();

  /**
   * Reference to a left-aligned label template.
   */
  @ContentChild('leftLabel')
  leftLabel: TemplateRef<HTMLElement> | null = null;

  /**
   * Reference to a right-aligned label template.
   */
  @ContentChild('rightLabel')
  rightLabel: TemplateRef<HTMLElement> | null = null;

  /**
   * Reference to a label template.
   */
  @ContentChild('labelTemplate')
  labelTemplate: TemplateRef<HTMLElement> | null = null;

  /**
   * Represents the error state of the textarea.
   */
  error: boolean | string = false;

  /**
   * Reference to the native element of the textarea.
   */
  nativeElement: ElementRef;

  /**
   * Timeout for input changes.
   */
  timeoutInput!: ReturnType<typeof setTimeout>;

  /**
   * Constructor for the SqInputComponent class.
   * @param validatorHelper - The ValidatorHelper service for input validation.
   * @param element - The ElementRef representing the input element.
   * @param translate - The TranslateService for internationalization (optional).
   */
  constructor(
    public validatorHelper: ValidatorHelper,
    public element: ElementRef,
    @Optional() public translate: TranslateService
  ) {
    this.nativeElement = element.nativeElement;
  }

  /**
   * Asynchronously validates the input value.
   */
  async validate(isBlur = false) {
    if (isBlur) {
      this.inFocus.emit(false);
    }
    if (this.externalError) {
      this.error = false;
    } else if (this.required && !this.value) {
      this.valid.emit(false);
      this.setError('forms.required');
    } else {
      this.valid.emit(true);
      this.error = '';
    }
  }

  /**
   * Handles changes to the textarea value.
   *
   * @param {string} event - The new value of the textarea.
   */
  async change(event: any) {
    this.inFocus.emit(true);
    this.value = event;
    clearTimeout(this.timeoutInput);
    this.timeoutInput = setTimeout(() => {
      this.valueChange.emit(event);
    }, this.timeToChange);
    this.validate();
  }

  /**
   * Asynchronously sets an error message.
   *
   * @param {string} key - The key for the error message.
   */
  async setError(key: string) {
    if (this.useFormErrors && this.translate) {
      this.error = await this.translate.instant(key);
    }
  }

  /**
   * Handle keydown events.
   * @param event - The keydown event.
   */
  keyDown(event: KeyboardEvent) {
    this.keyPressDown.emit(event);
  }

  /**
   * Handle keyup events.
   * @param event - The keyup event.
   */
  keyUp(event: KeyboardEvent) {
    this.keyPressUp.emit(event);
  }
}
<div class="wrapper-all-inside-input {{ customClass }}">
  @if (label?.length || tooltipMessage || labelTemplate) {
    <label
      class="display-flex"
      [ngClass]="{
        readonly: readonly,
      }"
      [for]="id"
    >
      @if (label && !labelTemplate) {
        <div [ngStyle]="{ color: labelColor }" [innerHtml]="label | universalSafe"></div>
      }
      @if (labelTemplate) {
        <div>
          <ng-container *ngTemplateOutlet="labelTemplate"></ng-container>
        </div>
      }
      @if (tooltipMessage) {
        <sq-tooltip
          class="ml-1"
          [message]="tooltipMessage"
          [placement]="tooltipPlacement"
          [color]="tooltipColor"
          [icon]="tooltipIcon"
        ></sq-tooltip>
      }
    </label>
  }
  <div
    class="p-0 wrapper-input wrapper-input-squid text-ellipsisarea"
    [ngClass]="{
      error: (externalError && externalError !== '') || (error && error !== ''),
      readonly: readonly,
    }"
  >
    @if (leftLabel) {
      <span class="input-group-text m-0">
        <ng-container *ngTemplateOutlet="leftLabel"></ng-container>
      </span>
    }
    <textarea
      class="col textarea scrollbar"
      [ngStyle]="{
        'background-color': backgroundColor,
        'border-color': borderColor,
      }"
      [ngClass]="{
        error: (externalError && externalError !== '') || (error && error !== ''),
        disabled: disabled,
        readonly: readonly,
        'has-icon-external': externalIcon,
      }"
      [id]="id"
      [name]="name"
      [placeholder]="placeholder || ''"
      [required]="required"
      [disabled]="disabled"
      [readonly]="readonly"
      (blur)="validate(true)"
      [maxlength]="maxLength"
      [ngModel]="value"
      (ngModelChange)="change($event)"
      (keydown)="keyDown($event)"
      (keyup)="keyUp($event)"
      ngDefaultControl
      >{{ value }}</textarea
    >
    @if (rightLabel) {
      <span class="input-group-text m-0">
        <ng-container *ngTemplateOutlet="rightLabel"></ng-container>
      </span>
    }
  </div>
  @if (externalIcon) {
    <span
      class="icon icon-external textarea-icon"
      [ngClass]="{
        'no-label': !label?.length,
      }"
      [innerHtml]="externalIcon || '' | universalSafe"
    ></span>
  }
  @if (errorSpan) {
    <div
      class="box-validation box-invalid show"
      [ngClass]="{
        'has-max-length': maxLength,
      }"
    >
      @if (externalError || error) {
        <i class="fa-solid fa-triangle-exclamation"></i>
      }
      {{ externalError ? externalError : '' }}
      {{ error && !externalError ? error : '' }}
      @if (maxLength) {
        <span
          class="max-length-name"
          [ngClass]="{
            'visibility-hidden-force': disabled || readonly,
          }"
        >
          {{ maxLength - (value?.length || 0) }}
        </span>
      }
    </div>
  }
</div>

./sq-textarea.component.scss

.wrapper-all-inside-input {
  .icon {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    position: absolute;
    right: 24px;
    top: 40px;
    &.no-label {
      top: 75px;
    }
  }
  .icon-external {
    color: inherit !important;
  }
  .max-length-name {
    font-size: inherit;
    float: right;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""