๐Ÿ˜€ <textarea /> ๋†’์ด ์ž๋™ ์ฒ˜๋ฆฌ

๋ฐ•์ƒ์€ยท2022๋…„ 4์›” 30์ผ
2

โœ๏ธ blelog โœ๏ธ

๋ชฉ๋ก ๋ณด๊ธฐ
5/13

textarea๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณธ์ธ์˜ ๋†’์ด๋ณด๋‹ค ๋” ๋งŽ์€ ์ปจํ…์ธ ๋ฅผ ๋„ฃ์œผ๋ฉด ์Šคํฌ๋กค๋ฐ”๊ฐ€ ์ƒ์„ฑ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์ด๋ฒˆ์— ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ž์— ๋”ฐ๋ผ์„œ ๋†’์ด๊ฐ€ ์ž๋™์œผ๋กœ ์ค„์–ด๋“ค๊ณ  ๋Š˜์–ด๋‚˜๋Š” ๊ฒƒ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ๊ธฐ๋กํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

react-hook-form๊ณผ ๋ณ‘ํ•ฉํ•ด์„œ ์ฒ˜๋ฆฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๋” ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์‹ค์ œ ์ ์šฉ ์ฝ”๋“œ
// 2022/04/30 - comment Ref - by 1-blue
const { ref, ...rest } = register("comment");
const commentRef = useRef<HTMLTextAreaElement | null>(null);

// 2022/04/30 - textarea ์ž๋™ ๋†’์ด ์กฐ์ ˆ - by 1-blue
const handleResizeHeight = useCallback(() => {
  if (!commentRef.current) return;

  commentRef.current.style.height = "auto";
  commentRef.current.style.height = commentRef.current?.scrollHeight + "px";
}, [commentRef]);
  
// jsx
<textarea
  placeholder="๋Œ“๊ธ€์„ ์ž‘์„ฑํ•˜์„ธ์š”"
  {...rest}
  className="w-full p-4 focus:outline-none resize-none rounded-sm bg-zinc-200 dark:bg-zinc-600"
  onInput={handleResizeHeight}
  ref={(e) => {
    ref(e);
    commentRef.current = e;
  }}
/>

์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

0๊ฐœ์˜ ๋Œ“๊ธ€