|
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | + |
| 4 | +import { ColorTypes, FontTypes } from '../../styles/theme'; |
| 5 | +import { applyFontStyles } from '../../styles/mixins'; |
| 6 | + |
| 7 | +import PlusIcon from '../../assets/images/icons/ic_plus.svg'; |
| 8 | +import XIcon from '../../assets/images/icons/ic_X.svg'; |
| 9 | + |
| 10 | +function ImageUpload() { |
| 11 | + const [previewUrl, setPreviewUrl] = useState(null); |
| 12 | + const [error, setError] = useState(''); |
| 13 | + |
| 14 | + const handleImageUpload = (e) => { |
| 15 | + const file = e.target.files?.[0]; |
| 16 | + if (!file) return; |
| 17 | + |
| 18 | + const preview = URL.createObjectURL(file); |
| 19 | + setPreviewUrl(preview); |
| 20 | + |
| 21 | + if (previewUrl) { |
| 22 | + setError('*이미지 등록은 최대 1개까지 가능합니다.'); |
| 23 | + e.target.value = ''; |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + setError(''); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleImageRemove = () => { |
| 31 | + setPreviewUrl(null); |
| 32 | + }; |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + return () => { |
| 36 | + if (previewUrl) { |
| 37 | + URL.revokeObjectURL(previewUrl); |
| 38 | + } |
| 39 | + }; |
| 40 | + }, [previewUrl]); |
| 41 | + |
| 42 | + return ( |
| 43 | + <Container> |
| 44 | + <label>상품 이미지</label> |
| 45 | + |
| 46 | + <Wrapper> |
| 47 | + <StInput |
| 48 | + type="file" |
| 49 | + placeholder="이미지를 추가해주세요" |
| 50 | + accept="image/*" |
| 51 | + id="file-input" |
| 52 | + onChange={handleImageUpload} |
| 53 | + /> |
| 54 | + <ImageWrapper> |
| 55 | + <StLabel htmlFor="file-input"> |
| 56 | + <img |
| 57 | + src={PlusIcon} |
| 58 | + alt="plus" |
| 59 | + width={28} |
| 60 | + height={28} |
| 61 | + /> |
| 62 | + 이미지 등록 |
| 63 | + </StLabel> |
| 64 | + </ImageWrapper> |
| 65 | + |
| 66 | + {previewUrl && ( |
| 67 | + <PreviewImage> |
| 68 | + <StXIcon |
| 69 | + src={XIcon} |
| 70 | + alt="x" |
| 71 | + width={20} |
| 72 | + height={20} |
| 73 | + onClick={handleImageRemove} |
| 74 | + /> |
| 75 | + <StImage |
| 76 | + src={previewUrl} |
| 77 | + alt="샘플이미지" |
| 78 | + /> |
| 79 | + </PreviewImage> |
| 80 | + )} |
| 81 | + </Wrapper> |
| 82 | + |
| 83 | + {error && <ErrorMessage>{error}</ErrorMessage>} |
| 84 | + </Container> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +export default ImageUpload; |
| 89 | + |
| 90 | +const Container = styled.div` |
| 91 | + display: flex; |
| 92 | + flex-direction: column; |
| 93 | + gap: 16px; |
| 94 | +`; |
| 95 | + |
| 96 | +const Wrapper = styled.div` |
| 97 | + position: relative; |
| 98 | + display: flex; |
| 99 | + gap: 24px; |
| 100 | +`; |
| 101 | + |
| 102 | +const ImageWrapper = styled.div` |
| 103 | + display: flex; |
| 104 | +`; |
| 105 | + |
| 106 | +const StInput = styled.input` |
| 107 | + position: absolute; |
| 108 | + opacity: 0; |
| 109 | + width: 0; |
| 110 | + height: 0; |
| 111 | + overflow: hidden; |
| 112 | +`; |
| 113 | + |
| 114 | +const StLabel = styled.label` |
| 115 | + display: flex; |
| 116 | + align-items: center; |
| 117 | + justify-content: center; |
| 118 | + flex-direction: column; |
| 119 | + gap: 8px; |
| 120 | +
|
| 121 | + width: 168px; |
| 122 | + height: 168px; |
| 123 | + border-radius: 12px; |
| 124 | + background-color: ${({ theme }) => theme.colors[ColorTypes.SECONDARY_GRAY_100]}; |
| 125 | + ${applyFontStyles(FontTypes.REGULAR16, ColorTypes.SECONDARY_GRAY_400)} |
| 126 | + cursor: pointer; |
| 127 | + transition: all 0.3s ease; |
| 128 | +
|
| 129 | + &:hover { |
| 130 | + background-color: ${({ theme }) => theme.colors[ColorTypes.SECONDARY_GRAY_200]}; |
| 131 | + } |
| 132 | +`; |
| 133 | + |
| 134 | +const PreviewImage = styled.div` |
| 135 | + position: relative; |
| 136 | +`; |
| 137 | + |
| 138 | +const StImage = styled.img` |
| 139 | + width: 168px; |
| 140 | + height: 168px; |
| 141 | + border-radius: 12px; |
| 142 | +`; |
| 143 | + |
| 144 | +const StXIcon = styled.img` |
| 145 | + position: absolute; |
| 146 | + top: 14px; |
| 147 | + right: 13px; |
| 148 | +`; |
| 149 | + |
| 150 | +const ErrorMessage = styled.span` |
| 151 | + ${applyFontStyles(FontTypes.REGULAR16, ColorTypes.ERROR)} |
| 152 | +`; |
0 commit comments