All files / src/chap04 FormTextarea.js

0% Statements 0/6
100% Branches 0/0
0% Functions 0/3
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                                                 
import { useState } from 'react';
 
export default function FormTextarea() {
  // State 초기화
  const [form, setForm] = useState({
    comment: `다양한 폼 요소를 리액트로 구현하는 방법에 대해서 알아보겠습니다. \n참고로 <input> 요소에서는 type 속성을 변경하여 숫자 스피너, 날짜 입력 박스 등 다양한 입력 박스를 표현할 수 있습니다.`
  });
 
  // 텍스트 영역 변경 시 입력 값을 State에 반영
  const handleForm = e => {
    setForm({
      ...form,
      [e.target.name]: e.target.value
    });
  };
 
  // [보내기] 버튼 클릭 시 입력값 로그 출력
  const show = () => {
    console.log(`댓글: ${form.comment}`);
  };
 
  return (
    <form>
      <label htmlFor="comment">댓글: </label><br />
      <textarea id="comment" name="comment"
        cols="30" rows="7"
        value={form.comment}
        onChange={handleForm}></textarea><br />
      <button type="button" onClick={show}>
        보내기</button>
    </form>
  );
}