본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

텍스트에어리어

원문 보기 위키 갱신

텍스트에어리어 (Textarea)

텍스트에어리어는 표준 textarea 요소에 스타일링과 autoResize 기능을 추가하는 컴포넌트예요. pTextarea 디렉티브를 입력 필드에 적용해서 사용하고, 활성화하면 스크롤바 대신 텍스트 양에 맞춰 높이가 자동으로 늘어나요. filled 변형, float label, 크기 조절, 유효성 상태 표시 같은 다양한 옵션도 제공해요. 네이티브 textarea를 렌더링하기 때문에 label이나 aria 속성을 활용해 스크린 리더 접근성도 챙길 수 있어요

출처: 문서

본문

접근성

스크린 리더 Textarea 컴포넌트는 전달된 모든 prop을 암묵적으로 포함하는 네이티브 textarea 엘리먼트를 렌더링해요. 컴포넌트를 설명하는 값은 label 태그와 id prop을 조합하거나 aria-labelledby, aria-label props를 사용해서 제공할 수 있어요.

AutoResize

autoResize가 활성화되면 스크롤바를 표시하는 대신 textarea가 늘어나요.

import { Component } from '@angular/core';

@Component({
    template: `
        <div class="card flex justify-center">
            <textarea rows="5" cols="30" pTextarea [autoResize]="true"></textarea>
        </div>
    `,
    standalone: true,
    imports: []
})
export class TextareaAutoresizeDemo {}

기본

pTextarea 디렉티브를 사용해서 input 필드에 Textarea를 적용해요.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
    template: `
        <div class="card flex justify-center">
            <textarea rows="5" cols="30" pTextarea [(ngModel)]="value"></textarea>
        </div>
    `,
    standalone: true,
    imports: [FormsModule]
})
export class TextareaBasicDemo {
    value!: string;
}

비활성화

disabled가 설정되어 있으면 엘리먼트를 편집하거나 포커스할 수 없어요.

import { Component } from '@angular/core';

@Component({
    template: `
        <div class="card flex justify-center">
            <textarea rows="5" cols="30" pTextarea [disabled]="true"></textarea>
        </div>
    `,
    standalone: true,
    imports: []
})
export class TextareaDisabledDemo {}

Filled

variant 속성을 filled로 지정하면 기본 outlined 스타일보다 시각적으로 더 강조된 모습으로 컴포넌트가 표시돼요.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
    template: `
        <div class="card flex justify-center">
            <textarea [(ngModel)]="value" [variant]="'filled'" rows="5" cols="30" pTextarea></textarea>
        </div>
    `,
    standalone: true,
    imports: [FormsModule]
})
export class TextareaFilledDemo {
    value!: string;
}

플로트 라벨

포커스가 가면 입력 필드 위에 플로팅 라벨이 나타나요. 자세한 내용은 FloatLabel 문서를 참고하세요.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FloatLabelModule } from 'primeng/floatlabel';

@Component({
    template: `
        <div class="card flex flex-wrap justify-center items-stretch gap-4">
            <p-floatlabel>
                <textarea pTextarea id="over_label" [(ngModel)]="value1" rows="5" cols="30" style="resize: none" class="h-full"></textarea>
                <label for="over_label">Over Label</label>
            </p-floatlabel>
            <p-floatlabel variant="in">
                <textarea pTextarea id="in_label" [(ngModel)]="value2" rows="5" cols="30" style="resize: none" class="h-full"></textarea>
                <label for="in_label">In Label</label>
            </p-floatlabel>
            <p-floatlabel variant="on">
                <textarea pTextarea id="on_label" [(ngModel)]="value3" rows="5" cols="30" style="resize: none" class="h-full"></textarea>
                <label for="on_label">On Label</label>
            </p-floatlabel>
        </div>
    `,
    standalone: true,
    imports: [FloatLabelModule, FormsModule]
})
export class TextareaFloatlabelDemo {
    value1: string = '';
    value2: string = '';
    value3: string = '';
}

Fluid

fluid prop을 true로 설정하면 컴포넌트가 컨테이너의 전체 너비를 차지해요.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
    template: `
        <div class="card">
            <textarea rows="5" cols="30" pTextarea [(ngModel)]="value" fluid></textarea>
        </div>
    `,
    standalone: true,
    imports: [FormsModule]
})
export class TextareaFluidDemo {
    value!: string;
}

Ifta 라벨

IftaLabel은 필드 안에서 위쪽에 정렬된 라벨을 만들 때 사용해요. 자세한 내용은 IftaLabel 문서를 참고하세요.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { IftaLabelModule } from 'primeng/iftalabel';

@Component({
    template: `
        <div class="card flex justify-center">
            <p-iftalabel>
                <textarea pTextarea id="description" [(ngModel)]="value" rows="5" cols="30" style="resize: none"></textarea>
                <label for="description">Description</label>
            </p-iftalabel>
        </div>
    `,
    standalone: true,
    imports: [IftaLabelModule, FormsModule]
})
export class TextareaIftalabelDemo {
    value: string = '';
}

Invalid

invalid 속성을 사용해서 유효성 검사 실패를 나타내는 invalid 상태를 적용하며, Angular Forms와 통합할 수 있어요.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
    template: `
        <div class="card flex justify-center">
            <textarea rows="5" cols="30" pTextarea [(ngModel)]="value" [invalid]="!value" placeholder="Address"></textarea>
        </div>
    `,
    standalone: true,
    imports: [FormsModule]
})
export class TextareaInvalidDemo {
    value!: string;
}

keyfilter-doc

InputText는 특정 키를 차단하는 키 필터링 기능을 기본으로 제공해요. 자세한 내용은 keyfilter 페이지를 참고하세요.

import { Component } from '@angular/core';

@Component({
    template: `
        <div class="card flex justify-center">
            <textarea pKeyFilter="int" rows="5" cols="30" pTextarea></textarea>
        </div>
    `,
    standalone: true,
    imports: []
})
export class TextareaKeyfilterDemo {}

reactiveforms-doc

Textarea는 reactive forms와 함께 사용할 수도 있어요. 이 경우 formControlName 속성을 사용해서 컴포넌트를 폼 컨트롤에 바인딩해요.

import { Component, inject } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { MessageModule } from 'primeng/message';
import { ToastModule } from 'primeng/toast';
import { ButtonModule } from 'primeng/button';
import { MessageService } from 'primeng/api';

@Component({
    template: `
        <p-toast />
        <div class="card flex justify-center">
            <form [formGroup]="exampleForm" (ngSubmit)="onSubmit()" class="flex flex-col gap-4">
                <div class="flex flex-col gap-1">
                    <textarea rows="5" cols="30" pTextarea formControlName="address" [invalid]="isInvalid('address')"></textarea>
                    @if (isInvalid('address')) {
                        <p-message severity="error" size="small" variant="simple">Address is required..</p-message>
                    }
                </div>
                <button pButton severity="secondary" type="submit"><span pButtonLabel>Submit</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [MessageModule, ToastModule, ButtonModule, ReactiveFormsModule]
})
export class TextareaReactiveformsDemo {
    messageService = inject(MessageService);
    items: any[] | undefined;
    exampleForm: FormGroup | undefined;
    formSubmitted: boolean = false;

    constructor() {
        this.exampleForm = this.fb.group({
                    address: ['', Validators.required]
                });
    }

    onSubmit() {
        this.formSubmitted = true;
        if (this.exampleForm.valid) {
            this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form is submitted', life: 3000 });
            this.exampleForm.reset();
            this.formSubmitted = false;
        }
    }

    isInvalid(controlName: string) {
        const control = this.exampleForm.get(controlName);
        return control?.invalid && (control.touched || this.formSubmitted);
    }
}

크기

Textarea는 기본 크기 외에 small과 large 크기도 제공해요.

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <textarea pTextarea [(ngModel)]="value1" pSize="small" placeholder="Small" rows="3"></textarea>
            <textarea pTextarea [(ngModel)]="value2" placeholder="Normal" rows="3"></textarea>
            <textarea pTextarea [(ngModel)]="value3" pSize="large" placeholder="Large" rows="3"></textarea>
        </div>
    `,
    standalone: true,
    imports: [FormsModule]
})
export class TextareaSizesDemo {
    value1!: string;
    value2!: string;
    value3!: string;
}

templatedrivenforms-doc

import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MessageModule } from 'primeng/message';
import { ToastModule } from 'primeng/toast';
import { ButtonModule } from 'primeng/button';
import { MessageService } from 'primeng/api';

@Component({
    template: `
        <p-toast />
        <div class="card flex justify-center">
            <form #exampleForm="ngForm" (ngSubmit)="onSubmit(exampleForm)" (ngSubmit)="onSubmit(exampleForm)" class="flex flex-col gap-4">
                <div class="flex flex-col gap-1">
                    <textarea name="address" #address="ngModel" rows="5" cols="30" [(ngModel)]="value" pTextarea [invalid]="address.invalid && (address.touched || exampleForm.submitted)" required></textarea>
                    @if (address.invalid && (address.touched || exampleForm.submitted)) {
                        <p-message severity="error" size="small" variant="simple">Address is required.</p-message>
                    }
                </div>
                <button pButton severity="secondary" type="submit"><span pButtonLabel>Submit</span></button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [MessageModule, ToastModule, ButtonModule, FormsModule]
})
export class TextareaTemplatedrivenformsDemo {
    messageService = inject(MessageService);
    items: any[] = [];
    value: any;

    onSubmit(form: any) {
        if (form.valid) {
            this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form is submitted', life: 3000 });
            form.resetForm();
        }
    }
}

Textarea

Textarea는 표준 textarea 엘리먼트에 스타일링과 autoResize 기능을 추가해요.

프로퍼티

Name Type Default Description
dt InputSignal undefined 컴포넌트의 스코프 디자인 토큰을 정의해요.
unstyled InputSignal undefined 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요.
pt InputSignal undefined 컴포넌트 내부의 DOM 엘리먼트에 속성을 전달하는 데 사용해요.
ptOptions InputSignal undefined 컴포넌트의 passthrough(pt) 옵션을 구성하는 데 사용해요.
pTextareaPT InputSignal undefined Textarea 컴포넌트 내부의 DOM 엘리먼트에 속성을 전달하는 데 사용해요.
pTextareaUnstyled InputSignal undefined 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요.
autoResize boolean false 설정되어 있으면 타이핑할 때 textarea 크기가 변해요.
pSize "small" | "large" - 컴포넌트의 크기를 정의해요.
variant InputSignal<"outlined" | "filled"> undefined 컴포넌트의 입력 variant를 지정해요.
fluid InputSignalWithTransform<boolean, unknown> undefined 활성화하면 컨테이너 너비의 100%를 차지해요.
invalid InputSignalWithTransform<boolean, unknown> false 설정되어 있으면 컴포넌트에 invalid 상태 스타일이 적용돼요.

이벤트 방출

Name Parameters Description
onResize event: {} | Event textarea 크기가 조정될 때 호출되는 콜백이에요.

패스스루 옵션

Name Type Description
host PassThroughOption<HTMLTextAreaElement, I> 호스트의 DOM 엘리먼트에 속성을 전달하는 데 사용해요.
root PassThroughOption<HTMLTextAreaElement, I> 루트의 DOM 엘리먼트에 속성을 전달하는 데 사용해요.

테밍

CSS 클래스

Class Description
p-textarea 루트 엘리먼트의 클래스 이름이에요.

디자인 토큰

Token CSS Variable Description
textarea.background --p-textarea-background 루트의 배경
textarea.disabled.background --p-textarea-disabled-background 루트의 비활성 상태 배경
textarea.filled.background --p-textarea-filled-background 루트의 filled 배경
textarea.filled.hover.background --p-textarea-filled-hover-background 루트의 filled 호버 배경
textarea.filled.focus.background --p-textarea-filled-focus-background 루트의 filled 포커스 배경
textarea.border.color --p-textarea-border-color 루트의 테두리 색상
textarea.hover.border.color --p-textarea-hover-border-color 루트의 호버 테두리 색상
textarea.focus.border.color --p-textarea-focus-border-color 루트의 포커스 테두리 색상
textarea.invalid.border.color --p-textarea-invalid-border-color 루트의 invalid 상태 테두리 색상
textarea.color --p-textarea-color 루트의 색상
textarea.disabled.color --p-textarea-disabled-color 루트의 비활성 상태 색상
textarea.placeholder.color --p-textarea-placeholder-color 루트의 플레이스홀더 색상
textarea.invalid.placeholder.color --p-textarea-invalid-placeholder-color 루트의 invalid 상태 플레이스홀더 색상
textarea.shadow --p-textarea-shadow 루트의 그림자
textarea.padding.x --p-textarea-padding-x 루트의 가로 패딩
textarea.padding.y --p-textarea-padding-y 루트의 세로 패딩
textarea.border.radius --p-textarea-border-radius 루트의 테두리 반경
textarea.focus.ring.width --p-textarea-focus-ring-width 루트의 포커스 링 너비
textarea.focus.ring.style --p-textarea-focus-ring-style 루트의 포커스 링 스타일
textarea.focus.ring.color --p-textarea-focus-ring-color 루트의 포커스 링 색상
textarea.focus.ring.offset --p-textarea-focus-ring-offset 루트의 포커스 링 오프셋
textarea.focus.ring.shadow --p-textarea-focus-ring-shadow 루트의 포커스 링 그림자
textarea.transition.duration --p-textarea-transition-duration 루트의 트랜지션 지속 시간
textarea.sm.font.size --p-textarea-sm-font-size 루트의 sm 폰트 크기
textarea.sm.padding.x --p-textarea-sm-padding-x 루트의 sm 가로 패딩
textarea.sm.padding.y --p-textarea-sm-padding-y 루트의 sm 세로 패딩
textarea.lg.font.size --p-textarea-lg-font-size 루트의 lg 폰트 크기
textarea.lg.padding.x --p-textarea-lg-padding-x 루트의 lg 가로 패딩
textarea.lg.padding.y --p-textarea-lg-padding-y 루트의 lg 세로 패딩

더 알아보기 (Learn more)

이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.