이정민

사진 셀렉트

1 +import { useState } from "react";
1 import styled from "styled-components"; 2 import styled from "styled-components";
2 3
3 const Image = () => { 4 const Image = () => {
5 + const [file, setFile] = useState(undefined);
6 + const [previewURL, setPreviewURL] = useState("");
7 +
8 + // const uploadImage = (file) => {
9 + // if (!file) {
10 + // return;
11 + // }
12 + // };
13 +
14 + const selectImg = (e) => {
15 + const reader = new FileReader();
16 + const targetFile = e.target.files[0];
17 + setFile(targetFile);
18 + // uploadImage(targetFile);
19 +
20 + reader.onloadend = () => {
21 + setPreviewURL(reader.result);
22 + };
23 +
24 + reader.readAsDataURL(targetFile);
25 + };
26 +
4 return ( 27 return (
5 <Container> 28 <Container>
6 - <ImgBox /> 29 + <ImgBox>
30 + {file === undefined ? (
31 + <>
32 + <BlankBox />
33 + <div>Click to add a photo</div>
34 + <input
35 + type="file"
36 + style={{
37 + marginTop: "5px",
38 + position: "absolute",
39 + zIndex: 0,
40 + width: "90%",
41 + height: "100%",
42 + border: "none",
43 + cursor: "pointer",
44 + outline: "none",
45 + }}
46 + onChange={selectImg}
47 + />
48 + </>
49 + ) : (
50 + <img
51 + alt={""}
52 + style={{ objectFit: "cover", display: "flex", margin: "0 auto" }}
53 + src={previewURL}
54 + />
55 + )}
56 + </ImgBox>
7 <Menu /> 57 <Menu />
8 </Container> 58 </Container>
9 ); 59 );
...@@ -26,12 +76,17 @@ const Container = styled.div` ...@@ -26,12 +76,17 @@ const Container = styled.div`
26 justify-content: center; 76 justify-content: center;
27 `; 77 `;
28 const ImgBox = styled.div` 78 const ImgBox = styled.div`
79 + position: relative;
29 width: 40rem; 80 width: 40rem;
30 height: 30rem; 81 height: 30rem;
31 background-color: white; 82 background-color: white;
32 box-shadow: ${({ theme }) => theme.boxShadow.normal}; 83 box-shadow: ${({ theme }) => theme.boxShadow.normal};
33 border-radius: 2rem; 84 border-radius: 2rem;
34 margin-top: 2rem; 85 margin-top: 2rem;
86 + display: flex;
87 + align-items: center;
88 + justify-content: center;
89 + font-size: 1rem;
35 `; 90 `;
36 91
37 const Box = styled.div` 92 const Box = styled.div`
...@@ -42,5 +97,13 @@ const Box = styled.div` ...@@ -42,5 +97,13 @@ const Box = styled.div`
42 background-color: white; 97 background-color: white;
43 box-shadow: ${({ theme }) => theme.boxShadow.normal}; 98 box-shadow: ${({ theme }) => theme.boxShadow.normal};
44 `; 99 `;
100 +const BlankBox = styled.div`
101 + z-index: 1;
102 + position: absolute;
103 + top: 0;
104 + width: 90%;
105 + height: 50px;
106 + background-color: white;
107 +`;
45 108
46 export default Image; 109 export default Image;
......