├── .all-contributorsrc ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── PULL_REQUEST_TEMPLATE └── workflows │ └── codeql-analysis.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── server.js ├── src ├── App.js ├── DataContext.js ├── Result.js ├── Step1.js ├── Step2.js ├── Step3.js ├── components │ ├── FileInput.js │ ├── Form.js │ ├── Header.js │ ├── Input.js │ ├── MainContainer.js │ └── PrimaryButton.js ├── index.css ├── index.js ├── serviceWorker.js └── setupTests.js └── yarn.lock /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "files": [ 3 | "README.md" 4 | ], 5 | "imageSize": 100, 6 | "commit": false, 7 | "contributors": [ 8 | { 9 | "login": "Njokosi", 10 | "name": "Njokosi", 11 | "avatar_url": "https://avatars.githubusercontent.com/u/75541922?v=4", 12 | "profile": "https://github.com/Njokosi", 13 | "contributions": [ 14 | "bug" 15 | ] 16 | }, 17 | { 18 | "login": "satansdeer", 19 | "name": "Maksim Ivanov", 20 | "avatar_url": "https://avatars.githubusercontent.com/u/450319?v=4", 21 | "profile": "http://maksimivanov.com", 22 | "contributions": [ 23 | "maintenance", 24 | "code" 25 | ] 26 | } 27 | ], 28 | "contributorsPerLine": 7, 29 | "projectName": "ultimate-react-hook-form-form", 30 | "projectOwner": "satansdeer", 31 | "repoType": "github", 32 | "repoHost": "https://github.com", 33 | "skipCi": true 34 | } 35 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE: -------------------------------------------------------------------------------- 1 | Fixes # 2 | 3 | ## Proposed Changes 4 | 5 | - 6 | - 7 | - 8 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ master ] 20 | schedule: 21 | - cron: '41 10 * * 3' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'javascript' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 37 | # Learn more: 38 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 39 | 40 | steps: 41 | - name: Checkout repository 42 | uses: actions/checkout@v2 43 | 44 | # Initializes the CodeQL tools for scanning. 45 | - name: Initialize CodeQL 46 | uses: github/codeql-action/init@v1 47 | with: 48 | languages: ${{ matrix.language }} 49 | # If you wish to specify custom queries, you can do so here or in a config file. 50 | # By default, queries listed here will override any specified in a config file. 51 | # Prefix the list here with "+" to use these queries and those in the config file. 52 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 53 | 54 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 55 | # If this step fails, then you should remove it and run the build manually (see below) 56 | - name: Autobuild 57 | uses: github/codeql-action/autobuild@v1 58 | 59 | # ℹ️ Command-line programs to run using the OS shell. 60 | # 📚 https://git.io/JvXDl 61 | 62 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 63 | # and modify them (or add more) to build your code if your project 64 | # uses a compiled language 65 | 66 | #- run: | 67 | # make bootstrap 68 | # make release 69 | 70 | - name: Perform CodeQL Analysis 71 | uses: github/codeql-action/analyze@v1 72 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | /uploads 4 | 5 | # dependencies 6 | /node_modules 7 | /.pnp 8 | .pnp.js 9 | 10 | # testing 11 | /coverage 12 | 13 | # production 14 | /build 15 | 16 | # misc 17 | .DS_Store 18 | .env.local 19 | .env.development.local 20 | .env.test.local 21 | .env.production.local 22 | 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | satansdeer@gmail.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thank you for willing to contribute! 4 | 5 | ## Project setup 6 | 7 | 1. Fork and clone the repo 8 | 2. Run `yarn && yarn start` to install dependencies and run the project. 9 | 3. Create a branch for your PR with `git checkout -b pr/your-branch-name` 10 | 11 | ## Help needed 12 | 13 | Please checkout the [the open issues][issues] 14 | 15 | Also, please watch the repo and respond to questions/bug reports/feature 16 | requests! Thanks! 17 | 18 | [issues]: https://github.com/satansdeer/ultimate-react-hoom-form-form/issues 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Maksim Ivanov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Screen_Shot_2021-04-29_at_17 13 16-removebg-preview 2 | 3 | 4 | [![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-) 5 | 6 | 7 | --- 8 | 9 | This is example repo for the [Ultimate React Hook Form Youtube video](https://www.youtube.com/watch?v=U-iz8b4RExA) 10 | 11 | The form features include: 12 | 13 | - multi-step / wizard form 14 | - form field validation on blur ( using yup ) 15 | - form field masking / normalisation 16 | - dependent form fields 17 | - custom form inputs 18 | - using react-hook-form with material-ui 19 | - file uploads using the dropzone ( using the react dropzone ) 20 | 21 | ## Contributors ✨ 22 | 23 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |

Njokosi

🐛

Maksim Ivanov

🚧 💻
34 | 35 | 36 | 37 | 38 | 39 | 40 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "completed", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@hookform/resolvers": "^0.1.0", 7 | "@material-ui/core": "^4.11.0", 8 | "@material-ui/icons": "^4.9.1", 9 | "@testing-library/jest-dom": "^4.2.4", 10 | "@testing-library/react": "^9.3.2", 11 | "@testing-library/user-event": "^7.1.2", 12 | "express-rate-limit": "^5.2.6", 13 | "libphonenumber-js": "^1.7.56", 14 | "material-ui": "^0.20.2", 15 | "react": "^16.13.1", 16 | "react-confetti": "^6.0.0", 17 | "react-dom": "^16.13.1", 18 | "react-dropzone": "^11.0.3", 19 | "react-hook-form": "^6.4.0", 20 | "react-router-dom": "^5.2.0", 21 | "react-scripts": "3.4.3", 22 | "sweetalert2": "^9.17.1", 23 | "yup": "^0.29.3" 24 | }, 25 | "devDependencies": { 26 | "concurrently": "^5.2.0", 27 | "cors": "^2.8.5", 28 | "express": "^4.17.1", 29 | "express-fileupload": "^1.1.7-alpha.3", 30 | "morgan": "^1.10.0" 31 | }, 32 | "scripts": { 33 | "start": "react-scripts start", 34 | "build": "react-scripts build", 35 | "test": "react-scripts test", 36 | "eject": "react-scripts eject", 37 | "start:server": "node ./server.js", 38 | "dev": "concurrently --kill-others \"npm run start:server\" \"npm run start\"" 39 | }, 40 | "eslintConfig": { 41 | "extends": "react-app" 42 | }, 43 | "browserslist": { 44 | "production": [ 45 | ">0.2%", 46 | "not dead", 47 | "not op_mini all" 48 | ], 49 | "development": [ 50 | "last 1 chrome version", 51 | "last 1 firefox version", 52 | "last 1 safari version" 53 | ] 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/satansdeer/ultimate-react-hook-form-form/5c70ad9c4ecd7d1875ecdb59ae2167c8b5b82a46/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/satansdeer/ultimate-react-hook-form-form/5c70ad9c4ecd7d1875ecdb59ae2167c8b5b82a46/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/satansdeer/ultimate-react-hook-form-form/5c70ad9c4ecd7d1875ecdb59ae2167c8b5b82a46/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 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const fs = require("fs"); 3 | const fileUpload = require("express-fileupload"); 4 | const cors = require("cors"); 5 | const morgan = require("morgan"); 6 | const rateLimit = require("express-rate-limit"); 7 | 8 | const app = express(); 9 | 10 | const limiter = rateLimit({ 11 | windowMs: 15 * 60 * 1000, // 15 minutes 12 | max: 100 // limit each IP to 100 requests per windowMs 13 | }); 14 | 15 | app.use( 16 | fileUpload({ 17 | createParentPath: true, 18 | }) 19 | ); 20 | 21 | app.use(cors()); 22 | app.use(express.json()); 23 | app.use(express.urlencoded({ extended: true })); 24 | app.use(morgan("dev")); 25 | 26 | app.post("/", limiter, async (req, res) => { 27 | try { 28 | if (req.files && req.files.files) { 29 | [req.files.files].flat().map((file) => { 30 | file.mv("./uploads/" + file.name); 31 | }); 32 | } 33 | 34 | fs.writeFile("./uploads/data.json", JSON.stringify(req.body), "utf8", () => { 35 | res.send({ 36 | status: true, 37 | message: "Data is uploaded", 38 | }); 39 | }); 40 | } catch (e) { 41 | res.status(500).send(e.message); 42 | } 43 | }); 44 | 45 | const port = process.env.PORT || 4000; 46 | 47 | app.listen(port, () => console.log(`Server is running on port ${port}`)); 48 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { BrowserRouter as Router, Route, Switch } from "react-router-dom"; 3 | import { Step1 } from "./Step1"; 4 | import { Step2 } from "./Step2"; 5 | import { Step3 } from "./Step3"; 6 | import { Result } from "./Result"; 7 | import { Header } from "./components/Header"; 8 | 9 | function App() { 10 | return ( 11 | <> 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | ); 23 | } 24 | 25 | export default App; 26 | -------------------------------------------------------------------------------- /src/DataContext.js: -------------------------------------------------------------------------------- 1 | import React, { createContext, useState, useContext } from 'react' 2 | 3 | const DataContext = createContext() 4 | 5 | export const DataProvider = ({children}) => { 6 | const [data, setData] = useState({}); 7 | 8 | const setValues = (values) => { 9 | setData(prevData => ({ 10 | ...prevData, 11 | ...values 12 | })) 13 | } 14 | 15 | return 16 | {children} 17 | 18 | } 19 | 20 | export const useData = () => useContext(DataContext) -------------------------------------------------------------------------------- /src/Result.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import Confetti from "react-confetti"; 3 | import Swal from "sweetalert2"; 4 | import { makeStyles } from "@material-ui/core/styles"; 5 | import { Link } from "react-router-dom"; 6 | import { useData } from "./DataContext"; 7 | import { MainContainer } from "./components/MainContainer"; 8 | import { PrimaryButton } from "./components/PrimaryButton"; 9 | import Table from "@material-ui/core/Table"; 10 | import TableBody from "@material-ui/core/TableBody"; 11 | import TableCell from "@material-ui/core/TableCell"; 12 | import TableContainer from "@material-ui/core/TableContainer"; 13 | import TableHead from "@material-ui/core/TableHead"; 14 | import TableRow from "@material-ui/core/TableRow"; 15 | import Paper from "@material-ui/core/Paper"; 16 | import Typography from "@material-ui/core/Typography"; 17 | import List from "@material-ui/core/List"; 18 | import ListItem from "@material-ui/core/ListItem"; 19 | import ListItemIcon from "@material-ui/core/ListItemIcon"; 20 | import ListItemText from "@material-ui/core/ListItemText"; 21 | import InsertDriveFile from "@material-ui/icons/InsertDriveFile"; 22 | 23 | const useStyles = makeStyles({ 24 | root: { 25 | marginBottom: "30px", 26 | }, 27 | table: { 28 | marginBottom: "30px", 29 | }, 30 | }); 31 | 32 | export const Result = () => { 33 | const [success, setSuccess] = useState(false); 34 | const styles = useStyles(); 35 | const { data } = useData(); 36 | 37 | const entries = Object.entries(data).filter((entry) => entry[0] !== "files"); 38 | const { files } = data; 39 | 40 | const onSubmit = async () => { 41 | const formData = new FormData(); 42 | if (data.files) { 43 | data.files.forEach((file) => { 44 | formData.append("files", file, file.name); 45 | }); 46 | } 47 | 48 | entries.forEach((entry) => { 49 | formData.append(entry[0], entry[1]); 50 | }); 51 | 52 | const res = await fetch("http://localhost:4000/", { 53 | method: "POST", 54 | body: formData, 55 | }); 56 | 57 | if (res.status === 200) { 58 | Swal.fire("Great job!", "You've passed the challenge!", "success"); 59 | setSuccess(true); 60 | } 61 | }; 62 | 63 | if (success) { 64 | return ; 65 | } 66 | 67 | return ( 68 | <> 69 | 70 | 71 | 📋 Form Values 72 | 73 | 74 | 75 | 76 | 77 | Field 78 | Value 79 | 80 | 81 | 82 | {entries.map((entry) => ( 83 | 84 | 85 | {entry[0]} 86 | 87 | {entry[1].toString()} 88 | 89 | ))} 90 | 91 |
92 |
93 | {files && ( 94 | <> 95 | 96 | 📦 Files 97 | 98 | 99 | {files.map((f, index) => ( 100 | 101 | 102 | 103 | 104 | 105 | 106 | ))} 107 | 108 | 109 | )} 110 | Submit 111 | Start over 112 |
113 | 114 | ); 115 | }; 116 | -------------------------------------------------------------------------------- /src/Step1.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { useHistory } from "react-router-dom"; 3 | import { useData } from "./DataContext"; 4 | import Typography from "@material-ui/core/Typography"; 5 | import { useForm } from "react-hook-form"; 6 | import { yupResolver } from "@hookform/resolvers/yup"; 7 | import { PrimaryButton } from "./components/PrimaryButton"; 8 | import { MainContainer } from "./components/MainContainer"; 9 | import { Form } from "./components/Form"; 10 | import { Input } from "./components/Input"; 11 | import * as yup from "yup"; 12 | 13 | const schema = yup.object().shape({ 14 | firstName: yup 15 | .string() 16 | .matches(/^([^0-9]*)$/, "First name should not contain numbers") 17 | .required("First name is a required field"), 18 | lastName: yup 19 | .string() 20 | .matches(/^([^0-9]*)$/, "Last name should not contain numbers") 21 | .required("Last name is a required field"), 22 | }); 23 | 24 | export const Step1 = () => { 25 | const { setValues, data } = useData(); 26 | const history = useHistory(); 27 | const { register, handleSubmit, errors } = useForm({ 28 | defaultValues: { firstName: data.firstName, lastName: data.lastName }, 29 | mode: "onBlur", 30 | resolver: yupResolver(schema), 31 | }); 32 | 33 | const onSubmit = (data) => { 34 | history.push("./step2"); 35 | setValues(data); 36 | }; 37 | 38 | return ( 39 | 40 | 41 | 🦄 Step 1 42 | 43 |
44 | 53 | 62 | Next 63 |
64 |
65 | ); 66 | }; 67 | -------------------------------------------------------------------------------- /src/Step2.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { useHistory } from "react-router-dom"; 3 | import { useForm } from "react-hook-form"; 4 | import { yupResolver } from "@hookform/resolvers"; 5 | import { useData } from "./DataContext"; 6 | import FormControlLabel from "@material-ui/core/FormControlLabel"; 7 | import Typography from "@material-ui/core/Typography"; 8 | import Checkbox from "@material-ui/core/Checkbox"; 9 | import { PrimaryButton } from "./components/PrimaryButton"; 10 | import { MainContainer } from "./components/MainContainer"; 11 | import { Form } from "./components/Form"; 12 | import { Input } from "./components/Input"; 13 | import * as yup from "yup"; 14 | import { parsePhoneNumberFromString } from 'libphonenumber-js' 15 | 16 | 17 | const schema = yup.object().shape({ 18 | email: yup 19 | .string() 20 | .email("Email should have correct format") 21 | .required("Email is a required field"), 22 | }); 23 | 24 | const normalizePhoneNumber = (value) => { 25 | const phoneNumber = parsePhoneNumberFromString(value) 26 | if(!phoneNumber){ 27 | return value 28 | } 29 | 30 | return ( 31 | phoneNumber.formatInternational() 32 | ); 33 | }; 34 | 35 | export const Step2 = () => { 36 | const { setValues, data } = useData(); 37 | const history = useHistory(); 38 | const { register, handleSubmit, watch, errors } = useForm({ 39 | defaultValues: { 40 | email: data.email, 41 | hasPhone: data.hasPhone, 42 | phoneNumber: data.phoneNumber, 43 | }, 44 | mode: "onBlur", 45 | resolver: yupResolver(schema), 46 | }); 47 | const hasPhone = watch("hasPhone"); 48 | 49 | const onSubmit = (data) => { 50 | history.push("./step3"); 51 | setValues(data); 52 | }; 53 | 54 | return ( 55 | 56 | 57 | 🦄 Step 2 58 | 59 |
60 | 70 | 71 | 74 | } 75 | label="Do you have a phone" 76 | /> 77 | 78 | {hasPhone && ( 79 | { 86 | event.target.value = normalizePhoneNumber(event.target.value); 87 | }} 88 | /> 89 | )} 90 | Next 91 | 92 |
93 | ); 94 | }; 95 | -------------------------------------------------------------------------------- /src/Step3.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { useHistory } from "react-router-dom"; 3 | import { useForm } from "react-hook-form"; 4 | import { useData } from "./DataContext"; 5 | import { MainContainer } from "./components/MainContainer"; 6 | import { FileInput } from "./components/FileInput"; 7 | import { PrimaryButton } from "./components/PrimaryButton"; 8 | import Typography from "@material-ui/core/Typography"; 9 | import { Form } from "./components/Form"; 10 | 11 | export const Step3 = () => { 12 | const history = useHistory(); 13 | const { data, setValues } = useData(); 14 | const { control, handleSubmit } = useForm({ 15 | defaultValues: { 16 | files: data.files, 17 | }, 18 | }); 19 | 20 | const onSubmit = (data) => { 21 | history.push("./result"); 22 | setValues(data); 23 | }; 24 | 25 | return ( 26 | 27 | 28 | 🦄 Step 3 29 | 30 |
31 | 32 | Next 33 | 34 |
35 | ); 36 | }; 37 | -------------------------------------------------------------------------------- /src/components/FileInput.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Dropzone from "react-dropzone"; 3 | import { Controller } from "react-hook-form"; 4 | import { makeStyles } from "@material-ui/core/styles"; 5 | import Paper from "@material-ui/core/Paper"; 6 | import CloudUpload from "@material-ui/icons/CloudUpload"; 7 | import List from "@material-ui/core/List"; 8 | import ListItem from "@material-ui/core/ListItem"; 9 | import ListItemIcon from "@material-ui/core/ListItemIcon"; 10 | import ListItemText from "@material-ui/core/ListItemText"; 11 | import InsertDriveFile from "@material-ui/icons/InsertDriveFile"; 12 | 13 | const useStyles = makeStyles((theme) => ({ 14 | root: { 15 | backgroundColor: "#eee", 16 | textAlign: "center", 17 | cursor: "pointer", 18 | color: "#333", 19 | padding: "10px", 20 | marginTop: "20px", 21 | }, 22 | icon: { 23 | marginTop: "16px", 24 | color: "#888888", 25 | fontSize: "42px", 26 | }, 27 | })); 28 | 29 | export const FileInput = ({ control, name }) => { 30 | const styles = useStyles(); 31 | 32 | return ( 33 | ( 38 | <> 39 | 40 | {({ getRootProps, getInputProps }) => ( 41 | 46 | 47 | 48 |

Drag 'n' drop files here, or click to select files

49 |
50 | )} 51 |
52 | 53 | {value.map((f, index) => ( 54 | 55 | 56 | 57 | 58 | 59 | 60 | ))} 61 | 62 | 63 | )} 64 | /> 65 | ); 66 | }; 67 | -------------------------------------------------------------------------------- /src/components/Form.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { makeStyles } from "@material-ui/core/styles"; 3 | 4 | const useStyles = makeStyles((theme) => ({ 5 | root: { 6 | width: "100%", 7 | marginTop: theme.spacing(1), 8 | }, 9 | })); 10 | 11 | export const Form = ({children, ...props}) => { 12 | const styles = useStyles(); 13 | 14 | return ( 15 |
16 | {children} 17 |
18 | ); 19 | }; 20 | -------------------------------------------------------------------------------- /src/components/Header.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Typography from "@material-ui/core/Typography"; 3 | import { makeStyles } from "@material-ui/core/styles"; 4 | 5 | const useStyles = makeStyles((theme) => ({ 6 | root: { 7 | margin: theme.spacing(3, 0, 2), 8 | fontFamily: "Permanent Marker", 9 | textAlign: "center", 10 | fontSize: "40px", 11 | color: "deeppink", 12 | textShadow: "1px 1px darkmagenta", 13 | }, 14 | })); 15 | 16 | export const Header = () => { 17 | const styles = useStyles(); 18 | 19 | return ( 20 | 21 | The Ultimate Form Challenge 22 | 23 | ); 24 | }; 25 | -------------------------------------------------------------------------------- /src/components/Input.js: -------------------------------------------------------------------------------- 1 | import React, { forwardRef } from "react"; 2 | import TextField from "@material-ui/core/TextField"; 3 | 4 | export const Input = forwardRef((props, ref) => { 5 | return ( 6 | 13 | ); 14 | }); 15 | -------------------------------------------------------------------------------- /src/components/MainContainer.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Container from "@material-ui/core/Container"; 3 | import { makeStyles } from "@material-ui/core/styles"; 4 | 5 | const useStyles = makeStyles((theme) => ({ 6 | root: { 7 | marginTop: theme.spacing(4), 8 | display: "flex", 9 | flexDirection: "column", 10 | alignItems: "center", 11 | }, 12 | })); 13 | 14 | export const MainContainer = ({ children, ...props }) => { 15 | const styles = useStyles(); 16 | 17 | return ( 18 | 24 | {children} 25 | 26 | ); 27 | }; 28 | -------------------------------------------------------------------------------- /src/components/PrimaryButton.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Button from "@material-ui/core/Button"; 3 | import { makeStyles } from "@material-ui/core/styles"; 4 | 5 | const useStyles = makeStyles((theme) => ({ 6 | root: { 7 | margin: theme.spacing(3, 0, 2), 8 | }, 9 | })); 10 | 11 | export const PrimaryButton = ({ children, ...props }) => { 12 | const styles = useStyles(); 13 | 14 | return ( 15 | 25 | ); 26 | }; 27 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | @import url("https://fonts.googleapis.com/css?family=Permanent+Marker&display=swap"); 2 | 3 | body { 4 | margin: 0; 5 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 6 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 7 | sans-serif; 8 | -webkit-font-smoothing: antialiased; 9 | -moz-osx-font-smoothing: grayscale; 10 | } 11 | 12 | code { 13 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 14 | monospace; 15 | } 16 | 17 | h1 { 18 | font-family: "Permanent Marker", sans-serif; 19 | text-align: center; 20 | margin-bottom: -40px; 21 | font-size: 40px; 22 | color: deeppink; 23 | text-shadow: 1px 1px darkmagenta; 24 | } 25 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | import { DataProvider } from './DataContext' 6 | import * as serviceWorker from "./serviceWorker"; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | 12 | 13 | , 14 | document.getElementById("root") 15 | ); 16 | 17 | // If you want your app to work offline and load faster, you can change 18 | // unregister() to register() below. Note this comes with some pitfalls. 19 | // Learn more about service workers: https://bit.ly/CRA-PWA 20 | serviceWorker.unregister(); 21 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | --------------------------------------------------------------------------------