-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (127 loc) · 5.01 KB
/
Copy pathindex.js
File metadata and controls
145 lines (127 loc) · 5.01 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
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
145
import React from 'react';
import { render } from 'react-dom';
import { Formik } from "formik";
import yup from "yup";
import Dropzone from "react-dropzone";
import Recaptcha from "react-recaptcha";
import Thumb from "./Thumb";
const dropzoneStyle = {
width: "100%",
height: "auto",
borderWidth: 2,
borderColor: "rgb(102, 102, 102)",
borderStyle: "dashed",
borderRadius: 5,
}
class App extends React.Component {
componentDidMount() {
const script = document.createElement("script");
script.src =
"https://www.google.com/recaptcha/api.js";
script.async = true;
script.defer = true;
document.body.appendChild(script);
}
render() {
return (
<div className="container">
<Formik
initialValues={{
name: "",
email: "",
photo: null,
attachments: [],
recaptcha: "",
}}
onSubmit={async (values) => {
let formData = new FormData();
formData.append("name", values.name);
formData.append("email", values.email);
formData.append("photo", values.photo);
for (let i = 0; i <= values.attachments.length; i++) {
formData.append(`attachments[${i}]`, values.attachments[i]);
}
formData.append("recaptcha", values.recaptcha);
// you would submit with fetch for example
// const res = await fetch("posturl", { method: "POST", body: formData });
// Do whatever on the sever
alert("Form submitted!");
console.log(formData.get("name"));
console.log(formData.get("email"));
console.log(formData.get("photo"));
console.log(formData.get("recaptcha"));
}}
validationSchema={yup.object().shape({
name: yup.string().required(),
email: yup.string().email().required(),
recaptcha: yup.string().required(),
})}
render={({ values, errors, touched, handleSubmit, handleChange, setFieldValue }) => (
<form onSubmit={handleSubmit}>
<div className="form-group">
<label for="name">Name</label>
<input id="name" name="name" type="text" className="form-control"
value={values.name} onChange={handleChange} />
{errors.name && touched.name && (
<p>{errors.name}</p>
)}
</div>
<div className="form-group">
<label for="email">E-mail</label>
<input id="email" name="email" type="email" className="form-control"
value={values.email} onChange={handleChange} />
{errors.email && touched.email && (
<p>{errors.email}</p>
)}
</div>
<div className="form-group">
<label for="photo">Photo</label>
<input id="photo" name="photo" type="file" className="form-control"
onChange={(event) => {
setFieldValue("photo", event.currentTarget.files[0]);
}} />
</div>
<div className="form-group">
<label>Multiple files</label>
<Dropzone style={dropzoneStyle} accept="image/*" onDrop={(acceptedFiles) => {
// do nothing if no files
if (acceptedFiles.length === 0) { return; }
// on drop we add to the existing files
setFieldValue("attachments", values.attachments.concat(acceptedFiles));
}}>
{({ isDragActive, isDragReject, acceptedFiles, rejectedFiles }) => {
if (isDragActive) {
return "This file is authorized";
}
if (isDragReject) {
return "This file is not authorized";
}
if (values.attachments.length === 0) {
return <p>Try dragging a file here!</p>
}
return values.attachments.map((file, i) => (<Thumb key={i} file={file} />));
}}
</Dropzone>
</div>
<div className="form-group">
<label>Recaptcha Validation</label>
<Recaptcha
sitekey="6Le2nREUAAAAALYuOv7X9Fe3ysDmOmghtj0dbCKW"
render="explicit"
theme="dark"
verifyCallback={(response) => { setFieldValue("recaptcha", response); }}
onloadCallback={() => { console.log("done loading!"); }}
/>
{errors.recaptcha
&& touched.recaptcha && (
<p>{errors.recaptcha}</p>
)}
</div>
<button type="submit" className="btn btn-primary">submit</button>
</form>
)} />
</div>
);
}
};
render(<App />, document.getElementById('root'));