-
Notifications
You must be signed in to change notification settings - Fork 8
/
Main.jsx
86 lines (73 loc) · 2.32 KB
/
Main.jsx
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
import styled from '@emotion/styled';
import { useCallback, useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useRecoilValue } from 'recoil';
import { userStoredList } from '../atoms';
import Card from '../components/Card';
import { CardContainer } from '../components/CardContainer';
import Modal from '../components/Modal';
import SearchBar from '../components/SearchBar';
const Main = () => {
const userList = useRecoilValue(userStoredList);
const [searchFilter, setSearchFilter] = useState({ type: '', text: '' });
const [cardData, setCardData] = useState(null);
const navigate = useNavigate();
const filteredList = useMemo(() => {
const { type, text } = searchFilter;
if (type === '' || text === '') return userList;
const regex = new RegExp(text, 'gi');
if (type === '이름') {
return userList.filter((item) => item.fcNm.match(regex));
} else if (type === '주소') {
return userList.filter((item) => item.fcAddr.match(regex));
} else if (type === '메모') {
return userList.filter((item) => item.memo.match(regex));
}
}, [userList, searchFilter]);
const queryTextHandler = useCallback((type, text) => {
setSearchFilter({ type, text });
}, []);
const onCardClick = useCallback((card) => {
setCardData({ ...card });
}, []);
const onAddClick = useCallback(() => {
navigate('/list');
}, [navigate]);
return (
<Container>
<SearchBar onQueryText={queryTextHandler} />
<CardContainer
data={filteredList}
keyExtractor={(item) => item.fcNo.toString()}
renderItem={(data) => <Card cardData={data} onClickEvent={onCardClick} />}
/>
{cardData && <Modal type="edit" cardData={cardData} />}
<PlusButton onClick={onAddClick}>+</PlusButton>
</Container>
);
};
const PlusButton = styled.div`
margin: 0 auto 3rem auto;
width: 5rem;
height: 5rem;
border-radius: 50%;
background-color: #e74c3c;
color: #fff;
box-shadow: 0 2px 4px 0 grey, 0 0 0 0.5px #e9ebee inset;
cursor: pointer;
font-weight: bold;
font-size: 2.5rem;
display: flex;
justify-content: center;
align-items: center;
user-select: none;
&:hover {
background-color: #c73929;
}
`;
const Container = styled.div`
margin: 0;
padding: 0;
box-sizing: border-box;
`;
export default Main;