Skip to content

Commit

Permalink
✨ Textarea 컴포넌트 글자 수 표시, 높이 조절
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJW1953 committed Nov 22, 2023
1 parent aaf8e48 commit 1d4da50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/frontend/src/components/commons/Input/Textarea.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export * from './index.css';
export const textarea = style([
input,
{
minHeight: '10rem',
resize: 'none',
overflow: 'hidden',
},
]);
21 changes: 20 additions & 1 deletion app/frontend/src/components/commons/Input/Textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useRef, useState } from 'react';

import * as styles from './Textarea.css';

type TextareaProps = {
Expand All @@ -10,6 +12,8 @@ type TextareaProps = {
fullWidth?: boolean;
};

const MIN_HEIGHT = '10rem';

export function Textarea({
label,
placeholder = '',
Expand All @@ -19,6 +23,17 @@ export function Textarea({
required = false,
fullWidth = false,
}: TextareaProps) {
const [textCount, setTextCount] = useState<number>(0);
const textRef = useRef<HTMLTextAreaElement | null>(null);

const handleInput = () => {
if (textRef && textRef.current) {
setTextCount(textRef.current.value.length);
textRef.current.style.height = MIN_HEIGHT;
textRef.current.style.height = `${textRef.current.scrollHeight}px`;
}
};

return (
<div
className={`${styles.container} ${errorMessage && styles.error} ${
Expand All @@ -30,14 +45,18 @@ export function Textarea({
{label}
{required && <span className={styles.required}>*</span>}
</span>
<span className={styles.count}>0/{maxLength}</span>
<span className={styles.count}>
{textCount}/{maxLength}
</span>
</div>
<textarea
ref={textRef}
className={styles.textarea}
placeholder={placeholder}
disabled={disabled}
maxLength={maxLength}
required={required}
onChange={handleInput}
/>
{!disabled && errorMessage && (
<p className={styles.errorMessage}>{errorMessage}</p>
Expand Down

0 comments on commit 1d4da50

Please sign in to comment.