├── README.md ├── public ├── favicon.ico ├── img │ ├── img-1.svg │ ├── img-2.svg │ ├── img-3.svg │ └── img-4.svg ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── Form.css ├── Form.js ├── FormSignup.js ├── FormSuccess.js ├── index.js ├── useForm.js └── validateInfo.js /README.md: -------------------------------------------------------------------------------- 1 | # react-form-v1 2 | Created a simple form with react hooks and included validation 3 | 4 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 5 | 6 | ## Available Scripts 7 | 8 | In the project directory, you can run: 9 | 10 | ### `yarn start` 11 | 12 | Runs the app in the development mode.
13 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 14 | 15 | The page will reload if you make edits.
16 | You will also see any lint errors in the console. 17 | 18 | ### `yarn test` 19 | 20 | Launches the test runner in the interactive watch mode.
21 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 22 | 23 | ### `yarn build` 24 | 25 | Builds the app for production to the `build` folder.
26 | It correctly bundles React in production mode and optimizes the build for the best performance. 27 | 28 | The build is minified and the filenames include the hashes.
29 | Your app is ready to be deployed! 30 | 31 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 32 | 33 | ### `yarn eject` 34 | 35 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 36 | 37 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 38 | 39 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 40 | 41 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 42 | 43 | ## Learn More 44 | 45 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 46 | 47 | To learn React, check out the [React documentation](https://reactjs.org/). 48 | 49 | ### Code Splitting 50 | 51 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 52 | 53 | ### Analyzing the Bundle Size 54 | 55 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 56 | 57 | ### Making a Progressive Web App 58 | 59 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 60 | 61 | ### Advanced Configuration 62 | 63 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 64 | 65 | ### Deployment 66 | 67 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 68 | 69 | ### `yarn build` fails to minify 70 | 71 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 72 | 73 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/briancodex/react-form-v1/286f4a4603bda257ae001dc57c74d7f30bd4eedb/public/favicon.ico -------------------------------------------------------------------------------- /public/img/img-1.svg: -------------------------------------------------------------------------------- 1 | Browser stats -------------------------------------------------------------------------------- /public/img/img-2.svg: -------------------------------------------------------------------------------- 1 | To the stars -------------------------------------------------------------------------------- /public/img/img-3.svg: -------------------------------------------------------------------------------- 1 | Astronaut -------------------------------------------------------------------------------- /public/img/img-4.svg: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 18 | 19 | 28 | React App 29 | 30 | 31 | 32 |
33 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/briancodex/react-form-v1/286f4a4603bda257ae001dc57c74d7f30bd4eedb/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/briancodex/react-form-v1/286f4a4603bda257ae001dc57c74d7f30bd4eedb/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css2?family=PT+Sans&display=swap'); 2 | 3 | * { 4 | box-sizing: border-box; 5 | margin: 0; 6 | padding: 0; 7 | font-family: 'PT Sans', sans-serif; 8 | } 9 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './App.css'; 3 | import Form from './Form'; 4 | 5 | function App() { 6 | return
; 7 | } 8 | 9 | export default App; 10 | -------------------------------------------------------------------------------- /src/Form.css: -------------------------------------------------------------------------------- 1 | .form-container { 2 | margin: 100px auto; 3 | width: 1000px; 4 | box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2), 0 7px 20px 0 rgba(0, 0, 0, 0.2); 5 | position: relative; 6 | border-radius: 10px; 7 | height: 600px; 8 | display: grid; 9 | grid-template-columns: 1fr 1fr; 10 | } 11 | 12 | .close-btn { 13 | position: absolute; 14 | top: 2%; 15 | right: 3%; 16 | font-size: 1.5rem; 17 | z-index: 1; 18 | color: #fff; 19 | cursor: pointer; 20 | } 21 | 22 | .form-content-left { 23 | background: linear-gradient( 24 | 90deg, 25 | rgb(39, 176, 255) 0%, 26 | rgb(0, 232, 236) 100% 27 | ); 28 | border-radius: 10px 0 0 10px; 29 | position: relative; 30 | } 31 | 32 | .form-img { 33 | width: 80%; 34 | height: 80%; 35 | position: absolute; 36 | top: 50%; 37 | left: 50%; 38 | transform: translate(-50%, -50%); 39 | } 40 | 41 | .form-img-2 { 42 | width: 60%; 43 | height: 60%; 44 | position: absolute; 45 | top: 50%; 46 | left: 50%; 47 | transform: translate(-50%, -50%); 48 | } 49 | 50 | .form-success { 51 | text-align: center; 52 | font-size: 24px; 53 | margin-top: 80px; 54 | color: #fff; 55 | } 56 | 57 | .form-content-right { 58 | border-radius: 0 10px 10px 0; 59 | position: relative; 60 | background: linear-gradient(90deg, rgb(40, 40, 40) 0%, rgb(17, 17, 17) 100%); 61 | } 62 | 63 | .form { 64 | position: absolute; 65 | top: 50%; 66 | left: 50%; 67 | transform: translate(-50%, -50%); 68 | width: 90%; 69 | height: 100%; 70 | display: flex; 71 | flex-direction: column; 72 | justify-content: center; 73 | align-items: center; 74 | } 75 | 76 | .form h1 { 77 | font-size: 1rem; 78 | text-align: start; 79 | width: 80%; 80 | margin-bottom: 1rem; 81 | color: #fff; 82 | } 83 | 84 | .form-inputs { 85 | margin-bottom: 0.5rem; 86 | width: 80%; 87 | } 88 | 89 | .form-inputs p { 90 | font-size: 0.8rem; 91 | margin-top: 0.5rem; 92 | color: #f00e0e; 93 | } 94 | 95 | .form-label { 96 | display: inline-block; 97 | font-size: 0.8rem; 98 | margin-bottom: 6px; 99 | color: #fff; 100 | } 101 | 102 | .form-input { 103 | display: block; 104 | padding-left: 10px; 105 | outline: none; 106 | border-radius: 2px; 107 | height: 40px; 108 | width: 100%; 109 | border: none; 110 | } 111 | 112 | .form-input::placeholder { 113 | color: #595959; 114 | font-size: 12px; 115 | } 116 | 117 | .form-input-btn { 118 | width: 80%; 119 | height: 50px; 120 | margin-top: 10px; 121 | border-radius: 2px; 122 | background: linear-gradient( 123 | 90deg, 124 | rgb(39, 176, 255) 0%, 125 | rgb(0, 232, 236) 100% 126 | ); 127 | outline: none; 128 | border: none; 129 | color: #fff; 130 | font-size: 1rem; 131 | } 132 | 133 | .form-input-btn:hover { 134 | cursor: pointer; 135 | background: linear-gradient( 136 | 90deg, 137 | rgb(39, 143, 255) 0%, 138 | rgb(12, 99, 250) 100% 139 | ); 140 | transition: all 0.4s ease-out; 141 | } 142 | 143 | .form-input-login { 144 | font-size: 0.8rem; 145 | margin-top: 10px; 146 | color: #fff; 147 | width: 80%; 148 | text-align: center; 149 | } 150 | 151 | .form-input-login a { 152 | text-decoration: none; 153 | color: #27cdff; 154 | font-weight: 600; 155 | } 156 | -------------------------------------------------------------------------------- /src/Form.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import './Form.css'; 3 | import FormSignup from './FormSignup'; 4 | import FormSuccess from './FormSuccess'; 5 | 6 | const Form = () => { 7 | const [isSubmitted, setIsSubmitted] = useState(false); 8 | 9 | function submitForm() { 10 | setIsSubmitted(true); 11 | } 12 | return ( 13 | <> 14 |
15 | × 16 |
17 | spaceship 18 |
19 | {!isSubmitted ? ( 20 | 21 | ) : ( 22 | 23 | )} 24 |
25 | 26 | ); 27 | }; 28 | 29 | export default Form; 30 | -------------------------------------------------------------------------------- /src/FormSignup.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import validate from './validateInfo'; 3 | import useForm from './useForm'; 4 | import './Form.css'; 5 | 6 | const FormSignup = ({ submitForm }) => { 7 | const { handleChange, handleSubmit, values, errors } = useForm( 8 | submitForm, 9 | validate 10 | ); 11 | 12 | return ( 13 |
14 | 15 |

16 | Get started with us today! Create your account by filling out the 17 | information below. 18 |

19 |
20 | 21 | 29 | {errors.username &&

{errors.username}

} 30 |
31 |
32 | 33 | 41 | {errors.email &&

{errors.email}

} 42 |
43 |
44 | 45 | 53 | {errors.password &&

{errors.password}

} 54 |
55 |
56 | 57 | 65 | {errors.password2 &&

{errors.password2}

} 66 |
67 | 70 | 71 | Already have an account? Login here 72 | 73 | 74 |
75 | ); 76 | }; 77 | 78 | export default FormSignup; 79 | -------------------------------------------------------------------------------- /src/FormSuccess.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './Form.css'; 3 | 4 | const FormSuccess = () => { 5 | return ( 6 |
7 |

We have received your request!

8 | success-image 9 |
10 | ); 11 | }; 12 | 13 | export default FormSuccess; 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | ReactDOM.render( 6 | 7 | 8 | , 9 | document.getElementById('root') 10 | ); 11 | -------------------------------------------------------------------------------- /src/useForm.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from 'react'; 2 | 3 | const useForm = (callback, validate) => { 4 | const [values, setValues] = useState({ 5 | username: '', 6 | email: '', 7 | password: '', 8 | password2: '' 9 | }); 10 | const [errors, setErrors] = useState({}); 11 | const [isSubmitting, setIsSubmitting] = useState(false); 12 | 13 | const handleChange = e => { 14 | const { name, value } = e.target; 15 | setValues({ 16 | ...values, 17 | [name]: value 18 | }); 19 | }; 20 | 21 | const handleSubmit = e => { 22 | e.preventDefault(); 23 | 24 | setErrors(validate(values)); 25 | setIsSubmitting(true); 26 | }; 27 | 28 | useEffect( 29 | () => { 30 | if (Object.keys(errors).length === 0 && isSubmitting) { 31 | callback(); 32 | } 33 | }, 34 | [errors] 35 | ); 36 | 37 | return { handleChange, handleSubmit, values, errors }; 38 | }; 39 | 40 | export default useForm; 41 | -------------------------------------------------------------------------------- /src/validateInfo.js: -------------------------------------------------------------------------------- 1 | export default function validateInfo(values) { 2 | let errors = {}; 3 | 4 | if (!values.username.trim()) { 5 | errors.username = 'Username required'; 6 | } 7 | // else if (!/^[A-Za-z]+/.test(values.name.trim())) { 8 | // errors.name = 'Enter a valid name'; 9 | // } 10 | 11 | if (!values.email) { 12 | errors.email = 'Email required'; 13 | } else if (!/\S+@\S+\.\S+/.test(values.email)) { 14 | errors.email = 'Email address is invalid'; 15 | } 16 | if (!values.password) { 17 | errors.password = 'Password is required'; 18 | } else if (values.password.length < 6) { 19 | errors.password = 'Password needs to be 6 characters or more'; 20 | } 21 | 22 | if (!values.password2) { 23 | errors.password2 = 'Password is required'; 24 | } else if (values.password2 !== values.password) { 25 | errors.password2 = 'Passwords do not match'; 26 | } 27 | return errors; 28 | } 29 | --------------------------------------------------------------------------------