simple diary - validate input

This commit is contained in:
haerong22
2022-04-13 21:12:05 +09:00
parent 94d7660bdf
commit 9e5fcfb2ab

View File

@@ -1,10 +1,13 @@
import { useState } from "react"; import React, { useRef, useState } from "react";
const DiaryEditor = () => { const DiaryEditor = () => {
const authorInput = useRef();
const contentInput = useRef();
const [state, setState] = useState({ const [state, setState] = useState({
author: "", author: "",
content: "", content: "",
emotion: 1, emotion: 5,
}); });
const handleChangeState = (e) => { const handleChangeState = (e) => {
@@ -15,7 +18,16 @@ const DiaryEditor = () => {
}; };
const handleSubmit = () => { const handleSubmit = () => {
console.log(state); if (state.author.length < 1) {
authorInput.current.focus();
return;
}
if (state.content.length < 5) {
contentInput.current.focus();
return;
}
alert("저장 성공"); alert("저장 성공");
}; };
@@ -24,6 +36,7 @@ const DiaryEditor = () => {
<h2>오늘의 일기</h2> <h2>오늘의 일기</h2>
<div> <div>
<input <input
ref={authorInput}
name="author" name="author"
value={state.author} value={state.author}
onChange={handleChangeState} onChange={handleChangeState}
@@ -31,12 +44,14 @@ const DiaryEditor = () => {
</div> </div>
<div> <div>
<textarea <textarea
ref={contentInput}
name="content" name="content"
value={state.content} value={state.content}
onChange={handleChangeState} onChange={handleChangeState}
/> />
</div> </div>
<div> <div>
<span>오늘의 감정점수 : </span>
<select <select
name="emotion" name="emotion"
value={state.emotion} value={state.emotion}