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