-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductForm.js
144 lines (142 loc) · 4.05 KB
/
ProductForm.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { useRef } from 'react';
import { onlyNumber } from '/src/utils/input';
import ColorButton from '/src/components/button/ColorButton';
import { MessageError } from './MessageError';
import { Form } from './style';
export const ProductForm = ({
productInfo,
setProductInfo,
productError,
imageSrc,
setImageSrc,
onClickCancel,
onClickSave,
}) => {
const uploadImageRef = useRef();
const onChangeImage = (e) => {
const reader = new FileReader();
reader.onload = () => {
if (reader.readyState === 2) {
setImageSrc(reader.result);
}
};
reader.readAsDataURL(e.target.files[0]);
setProductInfo({ ...productInfo, image: e.target.files[0] });
};
const onChangeProductInfo = (e) => {
setProductInfo((info) => ({ ...info, [e.target.name]: e.target.value }));
};
const onClickImage = (e) => {
e.preventDefault();
uploadImageRef.current.click();
};
const onClickShippingMethod = (e) => {
setProductInfo((info) => ({
...info,
shipping_method: e.target.dataset.method,
}));
};
return (
<Form>
<section>
<label>상품 이미지</label>
<img src={imageSrc} value={productInfo.image} onClick={onClickImage} />
<input
type="file"
accept="image/*"
ref={uploadImageRef}
onChange={onChangeImage}
/>
{productError.image && <MessageError content={productError.image} />}
</section>
<section>
<label>상품명</label>
<input
name="product_name"
value={productInfo.product_name}
onChange={onChangeProductInfo}
maxLength={50}
/>
{productError.product_name && (
<MessageError content={productError.product_name} />
)}
<label>판매가</label>
<input
name="price"
value={productInfo.price}
onInput={onlyNumber}
onChange={onChangeProductInfo}
maxLength={10}
/>
{productError.price && <MessageError content={productError.price} />}
<label>배송방법</label>
<div>
<ColorButton
size="MS"
width="220px"
data-method="DELIVERY"
onClick={onClickShippingMethod}
color={
productInfo.shipping_method === 'DELIVERY' ? 'green' : 'white'
}
>
택배, 소포, 등기
</ColorButton>
<ColorButton
size="MS"
width="220px"
data-method="PARCEL"
onClick={onClickShippingMethod}
color={productInfo.shipping_method === 'PARCEL' ? 'green' : 'white'}
>
직접배송(화물배달)
</ColorButton>
</div>
<label>기본 배송비</label>
<input
name="shipping_fee"
value={productInfo.shipping_fee}
onInput={onlyNumber}
onChange={onChangeProductInfo}
maxLength={7}
/>
{productError.shipping_fee && (
<MessageError content={productError.shipping_fee} />
)}
<label>재고</label>
<input
name="stock"
value={productInfo.stock}
onInput={onlyNumber}
onChange={onChangeProductInfo}
maxLength={7}
/>
{productError.stock && <MessageError content={productError.stock} />}
</section>
<section>
<label>상품 상세 정보</label>
<textarea
name="product_info"
value={productInfo.product_info}
onChange={onChangeProductInfo}
/>
{productError.product_info && (
<MessageError content={productError.product_info} />
)}
<div>
<ColorButton
width="200px"
size="M"
color="white"
onClick={onClickCancel}
>
취소
</ColorButton>
<ColorButton width="200px" size="M" onClick={onClickSave}>
저장하기
</ColorButton>
</div>
</section>
</Form>
);
};