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

텍스트 영역 (Textarea)

원문 보기 위키 갱신

SlTextarea는 사용자로부터 여러 줄의 텍스트를 입력받는 요소예요. 댓글, 피드백 같은 긴 텍스트를 받을 때 유용합니다.

출처: 문서

본문

<sl-textarea></sl-textarea>

React에서는 이렇게 사용할 수 있어요.

import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea />;

이 컴포넌트는 표준 `` 요소와 함께 동작해요. 폼 제출(form submission)과 클라이언트 측 유효성 검사(client-side validation)에 대해 더 자세히 알고 싶다면 폼 컨트롤 섹션을 참고하세요.

예시 (Examples)

라벨 (Labels)

label 속성을 사용하면 textarea에 접근 가능한(accessible) 라벨을 붙일 수 있어요. HTML이 포함된 라벨이 필요하다면 label 슬롯을 대신 사용하세요.

<sl-textarea label="Comments"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea label="Comments" />;

도움말 텍스트 (Help Text)

help-text 속성을 사용하면 textarea에 설명용 도움말 텍스트를 추가할 수 있어요. HTML이 포함된 도움말이 필요하다면 help-text 슬롯을 대신 사용하세요.

<sl-textarea label="Feedback" help-text="Please tell us what you think."> </sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea label="Feedback" help-text="Please tell us what you think." />;

줄 수 (Rows)

rows 속성을 사용하면 표시되는 텍스트 줄 수를 바꿀 수 있어요.

<sl-textarea rows="2"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea rows={2} />;

플레이스홀더 (Placeholders)

placeholder 속성을 사용하면 플레이스홀더를 추가할 수 있어요.

<sl-textarea placeholder="Type something"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea placeholder="Type something" />;

채워진 텍스트 영역 (Filled Textareas)

filled 속성을 추가하면 채워진(filled) 스타일의 textarea를 만들 수 있어요.

<sl-textarea placeholder="Type something" filled></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea placeholder="Type something" filled />;

비활성화 (Disabled)

disabled 속성을 사용하면 textarea를 비활성화할 수 있어요.

<sl-textarea placeholder="Textarea" disabled></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea placeholder="Textarea" disabled />;

크기 (Sizes)

size 속성을 사용하면 textarea의 크기를 바꿀 수 있어요.

<sl-textarea placeholder="Small" size="small"></sl-textarea>
<br />
<sl-textarea placeholder="Medium" size="medium"></sl-textarea>
<br />
<sl-textarea placeholder="Large" size="large"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => (
  <>
    <SlTextarea placeholder="Small" size="small"></SlTextarea>
    <br />
    <SlTextarea placeholder="Medium" size="medium"></SlTextarea>
    <br />
    <SlTextarea placeholder="Large" size="large"></SlTextarea>
  </>
);

크기 조절 막기 (Prevent Resizing)

기본적으로 textarea는 사용자가 세로로 크기를 조절할 수 있어요. 크기 조절을 막으려면 resize 속성을 none으로 설정하면 됩니다.

<sl-textarea resize="none"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea resize="none" />;

내용에 맞춰 확장 (Expand with Content)

resize를 auto로 설정하면 textarea가 내용에 맞춰 자동으로 커집니다.

<sl-textarea resize="auto"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
const App = () => <SlTextarea resize="auto" />;

불러오기 (Importing)

autoloader나 전통적인 loader를 사용 중이라면 이 부분은 무시해도 돼요. 그게 아니라면 아래 스니펫 중 원하는 방식으로 이 컴포넌트만 골라서 불러올 수 있어요.

Script 태그로 CDN에서 이 컴포넌트를 불러오려면:

<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/textarea/textarea.js"></script>

JavaScript import 구문으로 CDN에서 불러오려면:

import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/textarea/textarea.js';

번들러(bundler)를 사용해 불러오려면:

import '@shoelace-style/shoelace/dist/components/textarea/textarea.js';

React 컴포넌트로 불러오려면:

import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';

슬롯 (Slots)

Name Description
label The textarea’s label. Alternatively, you can use the label attribute.
help-text Text that describes how to use the input. Alternatively, you can use the help-text attribute.

슬롯 사용법에 대해 더 알아보기.

속성 (Properties)

Name Description Reflects Type Default
name The name of the textarea, submitted as a name/value pair with form data. string ''
value The current value of the textarea, submitted as a name/value pair with form data. string ''
size The textarea’s size. 'small' | 'medium' | 'large' 'medium'
filled Draws a filled textarea. boolean false
label The textarea’s label. If you need to display HTML, use the label slot instead. string ''
helpText help-text The textarea’s help text. If you need to display HTML, use the help-text slot instead. string ''
placeholder Placeholder text to show as a hint when the input is empty. string ''
rows The number of rows to display by default. number 4
resize Controls how the textarea can be resized. 'none' | 'vertical' | 'auto' 'vertical'
disabled Disables the textarea. boolean false
readonly Makes the textarea readonly. boolean false
form By default, form controls are associated with the nearest containing `` element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work. string ''
required Makes the textarea a required field. boolean false
minlength The minimum length of input that will be considered valid. number -
maxlength The maximum length of input that will be considered valid. number -
autocapitalize Controls whether and how text input is automatically capitalized as it is entered by the user. 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters' -
autocorrect Indicates whether the browser’s autocorrect feature is on or off. string -
autocomplete Specifies what permission the browser has to provide assistance in filling out form field values. Refer to this page on MDN for available values. string -
autofocus Indicates that the input should receive focus on page load. boolean -
enterkeyhint Used to customize the label or icon of the Enter key on virtual keyboards. 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send' -
spellcheck Enables spell checking on the textarea. boolean true
inputmode Tells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual keyboard on supportive devices. 'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url' -
defaultValue The default value of the form control. Primarily used for resetting the form control. string ''
validity Gets the validity state object - -
validationMessage Gets the validation message - -
updateComplete A read-only promise that resolves when the component has finished updating.

속성(attribute)과 프로퍼티(property)에 대해 더 알아보기.

이벤트 (Events)

Name React Event Description Event Detail
sl-blur onSlBlur Emitted when the control loses focus. -
sl-change onSlChange Emitted when an alteration to the control’s value is committed by the user. -
sl-focus onSlFocus Emitted when the control gains focus. -
sl-input onSlInput Emitted when the control receives input. -
sl-invalid onSlInvalid Emitted when the form control has been checked for validity and its constraints aren’t satisfied. -

이벤트에 대해 더 알아보기.

메서드 (Methods)

Name Description Arguments
focus() Sets focus on the textarea. options: FocusOptions
blur() Removes focus from the textarea. -
select() Selects all the text in the textarea. -
scrollPosition() Gets or sets the textarea’s scroll position. position: { top?: number; left?: number }
setSelectionRange() Sets the start and end positions of the text selection (0-based). \n selectionStart: number, selectionEnd: number, selectionDirection: 'forward' | 'backward' | 'none'\n
setRangeText() Replaces a range of text with a new string. \n replacement: string, start: number, end: number, selectMode: 'select' | 'start' | 'end' | 'preserve'\n
checkValidity() Checks for validity but does not show a validation message. Returns true when valid and false when invalid. -
getForm() Gets the associated form, if one exists. -
reportValidity() Checks for validity and shows the browser’s validation message if the control is invalid. -
setCustomValidity() Sets a custom validation message. Pass an empty string to restore validity. message: string

메서드에 대해 더 알아보기.

파트 (Parts)

Name Description
form-control The form control that wraps the label, input, and help text.
form-control-label The label’s wrapper.
form-control-input The input’s wrapper.
form-control-help-text The help text’s wrapper.
base The component’s base wrapper.
textarea The internal `` control.

CSS 파트 커스터마이징에 대해 더 알아보기.

더 알아보기 (Learn more)