-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInputBox.jsx
More file actions
111 lines (100 loc) · 2.67 KB
/
InputBox.jsx
File metadata and controls
111 lines (100 loc) · 2.67 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import 'normalize.css';
import { useRef, useState } from 'react';
import PropTypes from 'prop-types';
export default function InputBox({ todoList, setTodoList }) {
const formStyle = css`
display: flex;
align-items: center;
`;
const inputStyle = css`
width: 300px;
height: 35px;
outline: none;
border-radius: 20px;
border: 1.2px solid;
border-color: rgb(247, 196, 218);
padding-left: 12px;
@font-face {
font-family: 'LINESeedKR-Rg';
src: url('https://fastly.jsdelivr.net/gh/projectnoonnu/noonfonts_11-01@1.0/LINESeedKR-Rg.woff2')
format('woff2');
font-weight: 400;
font-style: normal;
}
font-family: 'LINESeedKR-Rg';
font-size: 12px;
color: #3d3d3d;
`;
const addButtonStyle = css`
border: 0;
background-color: white;
font-size: 20px;
color: rgb(59, 56, 56);
margin-left: 8px;
cursor: pointer;
&:hover {
color: rgb(247, 196, 218);
}
`;
const [text, setText] = useState(''); // input에 입력한 값
const inputRef = useRef(null);
// form 제출 시 새로고침 방지
const formClickEvent = (e) => {
e.preventDefault();
};
// input 값 가져오기
function onChangeInput(e) {
setText(e.target.value);
// e.target에 있는 <input.../>으로부터 value 값을 가져옴
}
// + 버튼 클릭(form 제출)
function onClickButton() {
// 공백 입력 방지
if (text.trim() === '') return;
// todoItemList에 값 추가
const AddTodoList = todoList.concat({
id: todoList.length,
text,
checked: false,
});
setTodoList(AddTodoList);
setText(''); // input 값 초기화
inputRef.current.focus(); // 버튼 누른 후에도 input box에 자동 포커싱
}
return (
<div>
<form onSubmit={formClickEvent} className="form" css={formStyle}>
<input
type="text"
name="todoItem"
value={text}
ref={inputRef}
className="input-box"
placeholder="할일을 입력하세요"
onChange={onChangeInput} // input 값이 변하면(이벤트 발생) 메소드 실행
css={inputStyle}
autoFocus
/>
<button
className="add-button"
onClick={onClickButton}
css={addButtonStyle}
>
+
</button>
</form>
</div>
);
}
// props 값 검증
InputBox.propTypes = {
todoList: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
text: PropTypes.string.isRequired,
}).isRequired
),
setTodoList: PropTypes.func.isRequired,
};