useTextarea
textarea 요소에 자동 크기 조절(autosize) 로직과 data 속성을 제공해 주는 훅이에요.
출처: 문서
본문
사용법 (Usage)
import { useTextarea } from '@primereact/headless/textarea';
const { rootProps } = useTextarea({ autoResize: true });
return <textarea {...rootProps} rows={5} placeholder="Enter text" />;
useTextarea는 옵션으로 자동 크기 조절 로직과 스타일링용 data 속성이 붙은 펼칠 수 있는 rootProps를 반환해요. 컴포넌트 기반 API가 필요하다면 Primitive 문서를 참고하세요.
기능 (Features)
- 펼칠 수 있는 root props, textarea 요소를 위한 단일
rootProps객체예요 - 자동 크기 조절,
autoResize를 켜면 높이가 콘텐츠를 따라가요 - 네이티브 우선 크기 측정, 브라우저가 지원하면 CSS
field-sizing: content를 사용하고, 그렇지 않으면scrollHeight측정으로 대체해요 - 무상태 값, 값 관리는 React나 네이티브 요소에 맡겨요
콜백 다루기 (Working with callbacks)
제어형 값 (Controlled value)
요소에 직접 value/onChange를 두어 바깥 상태에서 textarea를 조종해요.
const [text, setText] = React.useState('');
const { rootProps } = useTextarea({ autoResize: true });
return <textarea {...rootProps} value={text} onChange={(e) => setText(e.target.value)} />;
최소/최대 행과 함께 자동 크기 조절 (Auto-resize with min/max rows)
autoResize에 최소 rows와 CSS max-height를 조합하면 긴 입력에서도 크기가 무한정 커지지 않게 막을 수 있어요.
const { rootProps } = useTextarea({ autoResize: true });
return <textarea {...rootProps} rows={3} style={{ maxHeight: '12rem' }} />;
자동 크기 조절 끄기 (Disable auto-resize)
주변 레이아웃이 높이를 관리할 때는 autoResize를 설정하지 않거나(false) 두면 훅이 일반 textarea로 동작해요.
const { rootProps } = useTextarea();
return <textarea {...rootProps} rows={8} />;
data 속성으로 스타일링 (Styling with data attributes)
prop 객체에는 스타일링을 위한 data-scope="textarea"와 data-part="root"가 포함돼요.
[data-scope='textarea'][data-part='root'] {
padding: 0.5rem;
border: 1px solid #ccc;
border-radius: 0.25rem;
}
[data-scope='textarea'][data-part='root']:focus {
border-color: var(--p-primary-color);
outline: none;
}
[data-scope='textarea'][data-part='root']:disabled {
opacity: 0.5;
cursor: not-allowed;
}
API
useTextarea
접근성 (Accessibility)
Enter로 줄바꿈이 들어가는 표준 다중 줄 텍스트 입력 동작이에요. 전체 WAI-ARIA 준수 세부 사항은 Primitive 문서를 참고하세요.