Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.73 KB | None | 0 0
  1. import "date-fns";
  2. import React, { useState } from "react";
  3. import DateFnsUtils from "@date-io/date-fns";
  4. import {
  5. MuiPickersUtilsProvider,
  6. KeyboardDatePicker
  7. } from "@material-ui/pickers";
  8.  
  9. //Components
  10.  
  11. //Mui
  12. import Avatar from "@material-ui/core/Avatar";
  13. import Button from "@material-ui/core/Button";
  14. import CssBaseline from "@material-ui/core/CssBaseline";
  15. import TextField from "@material-ui/core/TextField";
  16. import FormControl from "@material-ui/core/FormControl";
  17. import Select from "@material-ui/core/Select";
  18. import Link from "@material-ui/core/Link";
  19. import Grid from "@material-ui/core/Grid";
  20. import Box from "@material-ui/core/Box";
  21. import LockOutlinedIcon from "@material-ui/icons/LockOutlined";
  22. import Typography from "@material-ui/core/Typography";
  23. import { makeStyles } from "@material-ui/core/styles";
  24. import Container from "@material-ui/core/Container";
  25. import MenuItem from "@material-ui/core/MenuItem";
  26. import Radio from "@material-ui/core/Radio";
  27. import RadioGroup from "@material-ui/core/RadioGroup";
  28. import FormControlLabel from "@material-ui/core/FormControlLabel";
  29. import FormLabel from "@material-ui/core/FormLabel";
  30. import FormHelperText from '@material-ui/core/FormHelperText';
  31. import InputLabel from '@material-ui/core/InputLabel';
  32.  
  33. function Copyright() {
  34. return (
  35. <Typography variant="body2" color="textSecondary" align="center">
  36. {"Copyright © "}
  37. <Link color="inherit" href="https://material-ui.com/">
  38. DonutFactory Corp.
  39. </Link>{" "}
  40. {new Date().getFullYear()}
  41. {"."}
  42. </Typography>
  43. );
  44. }
  45.  
  46. const useStyles = makeStyles(theme => ({
  47. "@global": {
  48. body: {
  49. backgroundColor: theme.palette.common.white
  50. }
  51. },
  52. top: {
  53. marginTop: theme.spacing(8),
  54. display: "flex",
  55. flexDirection: "column",
  56. alignItems: "center"
  57. },
  58. paper: {},
  59. flexitem: {
  60. display: "flex",
  61. flexDirection: "column",
  62. alignItems: "center",
  63. width: "50%",
  64. padding: "5px"
  65. },
  66. avatar: {
  67. margin: theme.spacing(1),
  68. backgroundColor: theme.palette.secondary.main
  69. },
  70. form: {
  71. width: "100%", // Fix IE 11 issue.
  72. marginTop: theme.spacing(3)
  73. },
  74. submit: {
  75. margin: theme.spacing(3, 0, 2)
  76. },
  77. formControl: {
  78. margin: theme.spacing(3)
  79. }
  80. }));
  81.  
  82. const initialValues = {
  83. firstName: "",
  84. lastName: "",
  85. userName: "",
  86. phoneNumber: "",
  87. email: "",
  88. password: "",
  89. repeatPassword: "",
  90. address1: "",
  91. address2: "",
  92. state: "",
  93. city: "",
  94. zipCode: "",
  95. gender: "female",
  96. marital: "single"
  97. };
  98.  
  99. const initialErrors = {
  100. firstName: "",
  101. lastName: "",
  102. userName: "",
  103. phoneNumber: "",
  104. email: "",
  105. password: "",
  106. repeatPassword: "",
  107. address1: "",
  108. state: "",
  109. city: "",
  110. zipCode: ""
  111. };
  112.  
  113. export default function Registration() {
  114. //states
  115. const [labelWidth, setLabelWidth] = React.useState(0);
  116. const [selectedDate, setSelectedDate] = React.useState(
  117. new Date("2014-08-18T21:11:54")
  118. );
  119.  
  120. const [values, setValues] = useState({
  121. firstName: "",
  122. lastName: "",
  123. userName: "",
  124. phoneNumber: "",
  125. email: "",
  126. password: "",
  127. repeatPassword: "",
  128. address1: "",
  129. address2: "",
  130. state: "",
  131. city: "",
  132. zipCode: "",
  133. gender: "female",
  134. marital: "single",
  135. numFix: ""
  136. });
  137.  
  138. const [errors, setErrors] = useState({
  139. firstName: "",
  140. lastName: "",
  141. userName: "",
  142. phoneNumber: "",
  143. email: "",
  144. password: "",
  145. repeatPassword: "",
  146. address1: "",
  147. address2: "",
  148. state: "",
  149. city: "",
  150. zipCode: ""
  151. });
  152.  
  153. const classes = useStyles();
  154.  
  155. const isEmpty = string =>{
  156. if(string.trim() === '') return true;
  157. else return false;
  158. }
  159.  
  160. const isEmail = email =>{
  161. const emailRegEx = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
  162. if(email.match(emailRegEx)) return true;
  163. else return false;
  164. }
  165.  
  166. function number(e){
  167. e.persist();
  168.  
  169. if(e.target.value){
  170. if(e.target.value.length <= 12){
  171. setValues(values => ({ ...values, [e.target.name]: e.target.value, numFix: 1}))
  172. }
  173. }
  174.  
  175. handlePhoneNumber(values.phoneNumber)
  176. }
  177.  
  178. function handlePhoneNumber(phoneNum) {
  179. if(phoneNum.length < 12){
  180. var cleaned = ('' + phoneNum).replace(/\D/g, '')
  181. var match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/)
  182. var final = match ? match[1] + '-' + match[2] + '-' + match[3] : null
  183. if (final && values.numFix === "") {
  184. return setValues(values => ({ ...values, phoneNumber: final, numFix: 1}))
  185. }
  186. return null
  187. }
  188. }
  189.  
  190. function submitHandler(e) {
  191.  
  192. e.preventDefault();
  193.  
  194. if(isEmpty(values.firstName)) setErrors(errors => ({ ...errors, firstName: 'Must not be empty'}));
  195. if(isEmpty(values.lastName)) setErrors(errors => ({ ...errors, lastName: 'Must not be empty'}));
  196. if(isEmpty(values.userName)) setErrors(errors => ({ ...errors, userName: 'Must not be empty'}));
  197.  
  198. if(isEmpty(values.phoneNumber)) setErrors(errors => ({ ...errors, phoneNumber: 'Must not be empty'}));
  199.  
  200. if(isEmpty(values.email)){setErrors(errors => ({ ...errors, email: 'Must not be empty'}));}
  201. else if(!isEmail(values.email)){setErrors(errors => ({ ...errors, email: 'Must be an email (x@x.x)'}));}
  202.  
  203. if(isEmpty(values.password)) setErrors(errors => ({ ...errors, password: 'Must not be empty'}));
  204. if(isEmpty(values.repeatPassword)) setErrors(errors => ({ ...errors, repeatPassword: 'Must not be empty'}));
  205. if(isEmpty(values.address1)) setErrors(errors => ({ ...errors, address1: 'Must not be empty'}));
  206. if(isEmpty(values.state)) setErrors(errors => ({ ...errors, state: 'Must not be empty'}));
  207. if(isEmpty(values.city)) setErrors(errors => ({ ...errors, city: 'Must not be empty'}));
  208. if(isEmpty(values.zipCode)) setErrors(errors => ({ ...errors, zipCode: 'Must not be empty'}));
  209.  
  210. return Object.keys(errors).length > 0 ? null : console.log(values);
  211. }
  212.  
  213. function handleDateChange(date) {
  214. setSelectedDate(date);
  215. }
  216.  
  217.  
  218. function handleChange(e) {
  219. e.persist();
  220.  
  221. setValues(values => ({
  222. ...values,
  223. [e.target.name]: e.target.value,
  224. numFix: ""
  225. }));
  226.  
  227. setErrors(errors =>({
  228. ...errors,
  229. [e.target.name]: ""
  230. }));
  231.  
  232. // console.log(e.target.name, e.target.value);
  233. }
  234.  
  235. function resetHandler() {
  236. setErrors(initialErrors)
  237. setValues(initialValues);
  238. }
  239.  
  240. return (
  241. <Container component="main" maxWidth="md">
  242. <CssBaseline />
  243. <div className={classes.top}>
  244. <Avatar className={classes.avatar}>
  245. <LockOutlinedIcon />
  246. </Avatar>
  247. <Typography component="h1" variant="h5">
  248. Sign up
  249. </Typography>
  250. </div>
  251. <div className={classes.paper}>
  252. <form className={classes.form} noValidate onSubmit={submitHandler}>
  253. <div className={classes.flexitem} style={{ float: "left" }}>
  254. <Grid container spacing={2}>
  255. <Grid item xs={12}>
  256. <TextField
  257. variant="outlined"
  258. required
  259. fullWidth
  260. id="userName"
  261. label="User Name"
  262. name="userName"
  263. autoComplete="userName"
  264. onChange={handleChange}
  265. value={values.userName}
  266. helperText={errors.userName}
  267. error={errors.userName ? true : false}
  268. />
  269. </Grid>
  270. <Grid item xs={12} sm={6}>
  271. <TextField
  272. name="firstName"
  273. variant="outlined"
  274. required
  275. fullWidth
  276. id="firstName"
  277. label="First Name"
  278. autoFocus
  279. onChange={handleChange}
  280. value={values.firstName}
  281. helperText={errors.firstName}
  282. error={errors.firstName ? true : false}
  283. />
  284. </Grid>
  285. <Grid item xs={12} sm={6}>
  286. <TextField
  287. variant="outlined"
  288. required
  289. fullWidth
  290. id="lastName"
  291. label="Last Name"
  292. name="lastName"
  293. autoComplete="lname"
  294. onChange={handleChange}
  295. value={values.lastName}
  296. helperText={errors.lastName}
  297. error={errors.lastName ? true : false}
  298. />
  299. </Grid>
  300. <Grid item xs={12}>
  301. <TextField
  302. variant="outlined"
  303. required
  304. fullWidth
  305. id="phoneNumber"
  306. label="Phone Number"
  307. name="phoneNumber"
  308. autoComplete="phoneNumber"
  309. onChange={number}
  310. onblur={handlePhoneNumber(values.phoneNumber)}
  311. value={values.phoneNumber}
  312. helperText={errors.phoneNumber}
  313. error={errors.phoneNumber ? true : false}
  314. />
  315. </Grid>
  316. <Grid item xs={12}>
  317. <TextField
  318. variant="outlined"
  319. required
  320. fullWidth
  321. id="email"
  322. label="Email Address"
  323. name="email"
  324. autoComplete="email"
  325. onChange={handleChange}
  326. value={values.email}
  327. helperText={errors.email}
  328. error={errors.email ? true : false}
  329. />
  330. </Grid>
  331. <Grid item xs={12} sm={6}>
  332. <TextField
  333. variant="outlined"
  334. required
  335. fullWidth
  336. name="password"
  337. label="Password"
  338. type="password"
  339. id="password"
  340. autoComplete="current-password"
  341. onChange={handleChange}
  342. value={values.password}
  343. helperText={errors.password}
  344. error={errors.password ? true : false}
  345. />
  346. </Grid>
  347. <Grid item xs={12} sm={6}>
  348. <TextField
  349. variant="outlined"
  350. required
  351. fullWidth
  352. name="repeatPassword"
  353. label="Repeat Password"
  354. type="password"
  355. id="repeatPassword"
  356. autoComplete="current-password"
  357. onChange={handleChange}
  358. value={values.repeatPassword}
  359. helperText={errors.repeatPassword}
  360. error={errors.repeatPassword ? true : false}
  361. />
  362. </Grid>
  363. <Grid item xs={12}>
  364. <TextField
  365. variant="outlined"
  366. required
  367. fullWidth
  368. id="address1"
  369. label="Address 1"
  370. name="address1"
  371. autoComplete="address1"
  372. onChange={handleChange}
  373. value={values.address1}
  374. helperText={errors.address1}
  375. error={errors.address1 ? true : false}
  376. />
  377. </Grid>
  378. </Grid>
  379. </div>
  380. <div className={classes.flexitem}>
  381. <Grid container spacing={2}>
  382. <Grid item xs={12}>
  383. <TextField
  384. variant="outlined"
  385. required
  386. fullWidth
  387. id="address2"
  388. label="Address 2"
  389. name="address2"
  390. autoComplete="address2"
  391. onChange={handleChange}
  392. value={values.address2}
  393. />
  394. </Grid>
  395. <Grid item xs={12} sm={6}>
  396. <TextField
  397. variant="outlined"
  398. required
  399. fullWidth
  400. id="city"
  401. label="City"
  402. name="city"
  403. autoComplete="city"
  404. onChange={handleChange}
  405. value={values.city}
  406. helperText={errors.city}
  407. error={errors.city ? true : false}
  408. />
  409. </Grid>
  410. <Grid item xs={12} sm={6}>
  411. <MuiPickersUtilsProvider utils={DateFnsUtils}>
  412. <KeyboardDatePicker
  413. disableToolbar
  414. variant="inline"
  415. format="MM/dd/yyyy"
  416. id="date-picker-inline"
  417. label="Date picker inline"
  418. value={selectedDate}
  419. onChange={handleDateChange}
  420. KeyboardButtonProps={{
  421. "aria-label": "change date"
  422. }}
  423. />
  424. </MuiPickersUtilsProvider>
  425. </Grid>
  426. <Grid item xs={12} sm={6}>
  427. <FormControl variant="outlined" error={errors.state ? true : false} >
  428. <InputLabel htmlFor="state-error">State</InputLabel>
  429. <Select
  430. value={values.state}
  431. onChange={handleChange}
  432. labelWidth={labelWidth}
  433. displayEmpty
  434. inputProps={{
  435. name: "state",
  436. id: "outlined-state"
  437. }}
  438. >
  439. <MenuItem value="" disabled>
  440. <em style={{ marginRight: "40px" }}>Select your state</em>
  441. </MenuItem>
  442. <MenuItem value="AL">Alabama</MenuItem>
  443. <MenuItem value="AK">Alaska</MenuItem>
  444. <MenuItem value="AZ">Arizona</MenuItem>
  445. <MenuItem value="AR">Arkansas</MenuItem>
  446. <FormHelperText>{errors.state}</FormHelperText>
  447. </Select>
  448. </FormControl>
  449. </Grid>
  450. <Grid item xs={12} sm={6}>
  451. <TextField
  452. variant="outlined"
  453. required
  454. fullWidth
  455. id="zipCode"
  456. label="Zip Code"
  457. name="zipCode"
  458. autoComplete="zipCode"
  459. onChange={handleChange}
  460. value={values.zipCode}
  461. helperText={errors.zipCode}
  462. error={errors.zipCode ? true : false}
  463. />
  464. </Grid>
  465. <Grid item xs={12} sm={6}>
  466. <FormControl
  467. component="fieldset"
  468. className={classes.formControl}
  469. >
  470. <FormLabel component="legend">Gender</FormLabel>
  471. <RadioGroup
  472. aria-label="gender"
  473. name="gender"
  474. value={values.gender}
  475. onChange={handleChange}
  476. >
  477. <FormControlLabel
  478. value="female"
  479. control={<Radio />}
  480. label="Female"
  481. />
  482. <FormControlLabel
  483. value="male"
  484. control={<Radio />}
  485. label="Male"
  486. />
  487. <FormControlLabel
  488. value="other"
  489. control={<Radio />}
  490. label="Other"
  491. />
  492. <FormControlLabel
  493. value="decline to state"
  494. control={<Radio />}
  495. label="Decline to State"
  496. />
  497. </RadioGroup>
  498. </FormControl>
  499. </Grid>
  500. <Grid item xs={12} sm={6}>
  501. <FormControl
  502. component="fieldset"
  503. className={classes.formControl}
  504. >
  505. <FormLabel component="legend">Marital Status</FormLabel>
  506. <RadioGroup
  507. aria-label="marital"
  508. name="marital"
  509. value={values.marital}
  510. onChange={handleChange}
  511. >
  512. <FormControlLabel
  513. value="single"
  514. control={<Radio />}
  515. label="Single"
  516. />
  517. <FormControlLabel
  518. value="married"
  519. control={<Radio />}
  520. label="Married"
  521. />
  522. <FormControlLabel
  523. value="divorced"
  524. control={<Radio />}
  525. label="Divorced"
  526. />
  527. <FormControlLabel
  528. value="widowed"
  529. control={<Radio />}
  530. label="Widowed"
  531. />
  532. </RadioGroup>
  533. </FormControl>
  534. </Grid>
  535. </Grid>
  536. </div>
  537. <Button
  538. fullWidth
  539. variant="contained"
  540. color="primary"
  541. className={classes.submit}
  542. onClick={resetHandler}
  543. >
  544. Reset
  545. </Button>
  546. <Button type="submit" fullWidth variant="contained" color="primary">
  547. Sign Up
  548. </Button>
  549. </form>
  550. </div>
  551. <Box mt={5}>
  552. <Copyright />
  553. </Box>
  554. </Container>
  555. );
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement