├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json └── index.html ├── src ├── setupTests.js ├── App.js ├── App.test.js ├── controls │ ├── index.js │ ├── Input.js │ ├── Button.js │ ├── Checkbox.js │ ├── RadioGroup.js │ ├── Select.js │ └── DatePicker.js ├── index.css ├── reportWebVitals.js ├── index.js ├── App.css ├── components │ └── useForm.js ├── logo.svg └── pages │ └── Employee.js ├── .gitignore ├── package.json ├── README.md └── .eslintcache /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/Build-a-Perfect-React-Form-with-Hook/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/Build-a-Perfect-React-Form-with-Hook/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodAffection/Build-a-Perfect-React-Form-with-Hook/HEAD/public/logo512.png -------------------------------------------------------------------------------- /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'; 6 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import './App.css'; 2 | import { Container } from '@material-ui/core'; 3 | import Employee from './pages/Employee'; 4 | 5 | function App() { 6 | return ( 7 | 8 | 9 | 10 | ); 11 | } 12 | 13 | export default App; 14 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /src/controls/index.js: -------------------------------------------------------------------------------- 1 | export { default as Button } from './Button' 2 | export { default as Checkbox } from './Checkbox' 3 | export { default as DatePicker } from './DatePicker' 4 | export { default as Input } from './Input' 5 | export { default as RadioGroup } from './RadioGroup' 6 | export { default as Select } from './Select' -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/controls/Input.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { TextField } from '@material-ui/core'; 3 | 4 | export default function Input(props) { 5 | 6 | const { name, label, value, variant, onChange } = props; 7 | return ( 8 | 15 | ) 16 | } 17 | -------------------------------------------------------------------------------- /src/controls/Button.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { Button as MuiButton } from "@material-ui/core"; 3 | 4 | export default function Button(props) { 5 | 6 | const { text, color, variant, onClick, ...other } = props 7 | 8 | return ( 9 | 14 | {text} 15 | 16 | ) 17 | } 18 | -------------------------------------------------------------------------------- /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 reportWebVitals from './reportWebVitals'; 6 | 7 | ReactDOM.render( 8 | , 9 | document.getElementById('root') 10 | ); 11 | 12 | // If you want to start measuring performance in your app, pass a function 13 | // to log results (for example: reportWebVitals(console.log)) 14 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 15 | reportWebVitals(); 16 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/controls/Checkbox.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { FormControl, FormControlLabel, Checkbox as MuiCheckbox } from '@material-ui/core'; 3 | 4 | export default function Checkbox(props) { 5 | 6 | const { name, label, value, onChange } = props; 7 | 8 | 9 | const convertToDefEventPara = (name, value) => ({ 10 | target: { 11 | name, value 12 | } 13 | }) 14 | 15 | return ( 16 | 17 | onChange(convertToDefEventPara(name, e.target.checked))} 23 | />} 24 | label={label} 25 | /> 26 | 27 | ) 28 | } 29 | -------------------------------------------------------------------------------- /src/controls/RadioGroup.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { FormControl, FormLabel, RadioGroup as MuiRadioGroup, FormControlLabel, Radio } from '@material-ui/core'; 3 | 4 | export default function RadioGroup(props) { 5 | 6 | const { name, label, value, onChange, items } = props; 7 | 8 | return ( 9 | 10 | {label} 11 | 15 | { 16 | items.map( 17 | item => ( 18 | } label={item.title} /> 19 | ) 20 | ) 21 | } 22 | 23 | 24 | ) 25 | } 26 | -------------------------------------------------------------------------------- /src/controls/Select.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { FormControl, InputLabel, Select as MuiSelect, MenuItem, FormHelperText } from '@material-ui/core'; 3 | 4 | export default function Select(props) { 5 | 6 | const { name, label, value, varient, onChange, options } = props; 7 | 8 | return ( 9 | 10 | {label} 11 | 16 | None 17 | { 18 | options.map( 19 | item => ({item.title}) 20 | ) 21 | } 22 | 23 | 24 | ) 25 | } 26 | -------------------------------------------------------------------------------- /src/controls/DatePicker.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { MuiPickersUtilsProvider, KeyboardDatePicker } from "@material-ui/pickers"; 3 | import DateFnsUtils from "@date-io/date-fns"; 4 | 5 | export default function DatePicker(props) { 6 | 7 | const { name, label, value, dateFormat, onChange } = props 8 | 9 | 10 | const convertToDefEventPara = (name, value) => ({ 11 | target: { 12 | name, value 13 | } 14 | }) 15 | 16 | return ( 17 | 18 | onChange(convertToDefEventPara(name, date))} 24 | /> 25 | 26 | ) 27 | } 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-material-form", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@date-io/date-fns": "^1.3.13", 7 | "@material-ui/core": "^4.11.2", 8 | "@material-ui/pickers": "^3.2.10", 9 | "@testing-library/jest-dom": "^5.11.6", 10 | "@testing-library/react": "^11.2.2", 11 | "@testing-library/user-event": "^12.5.0", 12 | "date-fns": "^2.16.1", 13 | "react": "^17.0.1", 14 | "react-dom": "^17.0.1", 15 | "react-scripts": "4.0.1", 16 | "web-vitals": "^0.2.4" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": [ 26 | "react-app", 27 | "react-app/jest" 28 | ] 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/components/useForm.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react' 2 | import { makeStyles } from "@material-ui/core"; 3 | 4 | export function useForm(modelObject) { 5 | 6 | const [values, setValues] = useState(modelObject); 7 | 8 | const handleInputChange = e => { 9 | const { name, value } = e.target 10 | setValues({ 11 | ...values, 12 | [name]: value 13 | }) 14 | } 15 | 16 | const resetForm = () => { 17 | setValues(modelObject); 18 | } 19 | 20 | return { 21 | values, 22 | setValues, 23 | handleInputChange, 24 | resetForm 25 | } 26 | } 27 | 28 | const useStyles = makeStyles(theme => ({ 29 | root: { 30 | '& .MuiFormControl-root': { 31 | width: '80%', 32 | margin: theme.spacing(1) 33 | } 34 | } 35 | })) 36 | 37 | export function Form(props) { 38 | 39 | const classes = useStyles(); 40 | const { children, onSubmit, ...other } = props; 41 | return ( 42 |
46 | {children} 47 |
48 | ) 49 | } 50 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Build a Perfect React Form with React Hook 2 | 3 | This is a demo project for article here : https://bit.ly/3p7pyuW, where explain how to build react form by applying best practices. So that you can keep your application more clean and organized. 4 | 5 | ## How it works ? 6 | 7 | Article : https://bit.ly/3p7pyuW 8 | Video Tutorial of the same : https://youtu.be/-XKaSCU0ZLM 9 | 10 | Video Tutorial for Material UI Form Design 13 | 14 | 15 | 16 | | :bar_chart: | List of Tutorials | | :moneybag: | Support Us | 17 | |--------------------------:|:---------------------|---|---------------------:|:-------------------------------------| 18 | | Angular |http://bit.ly/2KQN9xF | |Paypal | https://goo.gl/bPcyXW | 19 | | Asp.Net Core |http://bit.ly/30fPDMg | |Amazon Affiliate | https://geni.us/JDzpE | 20 | | React |http://bit.ly/325temF | | 21 | | Python |http://bit.ly/2ws4utg | | :point_right: | Follow Us | 22 | | Node.js |https://goo.gl/viJcFs | |Website |http://www.codaffection.com | 23 | | Asp.Net MVC |https://goo.gl/gvjUJ7 | |YouTube |https://www.youtube.com/codaffection | 24 | | Flutter |https://bit.ly/3ggmmJz| |Facebook |https://www.facebook.com/codaffection | 25 | | Web API |https://goo.gl/itVayJ | |Twitter |https://twitter.com/CodAffection | 26 | | MEAN Stack |https://goo.gl/YJPPAH | | 27 | | C# Tutorial |https://goo.gl/s1zJxo | | 28 | | Asp.Net WebForm |https://goo.gl/GXC2aJ | | 29 | | C# WinForm |https://goo.gl/vHS9Hd | | 30 | | MS SQL |https://goo.gl/MLYS9e | | 31 | | Crystal Report |https://goo.gl/5Vou7t | | 32 | | CG Exercises in C Program |https://goo.gl/qEWJCs | | 33 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/pages/Employee.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { useForm, Form } from "../components/useForm"; 3 | import { Button, Checkbox, DatePicker, Input, RadioGroup, Select } from "../controls"; 4 | import { Grid } from "@material-ui/core"; 5 | 6 | const genderList = [ 7 | { id: 'male', title: 'Male' }, 8 | { id: 'female', title: 'Female' }, 9 | { id: 'other', title: 'Other' }, 10 | ] 11 | 12 | const departmentList = [ 13 | { id: '1', title: 'Development' }, 14 | { id: '2', title: 'Marketing' }, 15 | { id: '3', title: 'Accounting' }, 16 | { id: '4', title: 'HR' }, 17 | ] 18 | 19 | const modelObject = { 20 | id: 0, 21 | fullName: '', 22 | email: '', 23 | mobile: '', 24 | city: '', 25 | gender: 'male', 26 | departmentId: '', 27 | hireDate: new Date(), 28 | isPermanent: false 29 | } 30 | 31 | export default function Employee() { 32 | 33 | const { 34 | values, 35 | setValues, 36 | handleInputChange, 37 | resetForm 38 | } = useForm(modelObject); 39 | 40 | const handleSubmit = e => { 41 | e.preventDefault() 42 | if (validate()) { 43 | console.log(values); 44 | resetForm() 45 | } 46 | } 47 | 48 | return ( 49 |
50 | 51 | 52 | 58 | 64 | 70 | 76 | 77 | 78 | 79 | 86 |