src/index.js
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App />, document.getElementById('root') );public/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>React App</title> </head> <body> <div id="root"></div> </body> </html>src/App.js
//src/App.js import React from 'react'; //React Forms class App extends React.Component { constructor(props) { super(props); this.state = { username: '' }; } myChangeHandler = (event) => { this.setState({username: event.target.value}); } render() { return ( <form> <h1>Hello {this.state.username}</h1> <p>Enter your name:</p> <input type='text' onChange={this.myChangeHandler} /> </form> ); } } export default App;src/App.js
//import React from 'react'; import React, { useReducer, useState } from 'react'; import './App.css'; const formReducer = (state, event) => { if(event.reset) { return { apple: '', count: 0, name: '', 'gift-wrap': false, } } return { ...state, [event.name]: event.value } } function App() { const [formData, setFormData] = useReducer(formReducer, { count: 100, }); const [submitting, setSubmitting] = useState(false); const handleSubmit = event => { event.preventDefault(); setSubmitting(true); setTimeout(() => { setSubmitting(false); setFormData({ reset: true }) }, 3000); } const handleChange = event => { const isCheckbox = event.target.type === 'checkbox'; setFormData({ name: event.target.name, value: isCheckbox ? event.target.checked : event.target.value, }) } const mystyle = { color: "#495057", backgroundColor: "#fff", border: "1px solid #ced4da", padding: ".375rem .75rem" }; return( <div className="wrapper"> <h1 style={{color: "red"}}>ReactJS Form and CSS</h1> {submitting && <div> You are submitting the following: <ul> {Object.entries(formData).map(([name, value]) => ( <li key={name}><strong>{name}</strong>: {value.toString()}</li> ))} </ul> </div> } <form onSubmit={handleSubmit}> <fieldset disabled={submitting}> <label> <p>Name</p> <input style={mystyle} name="name" onChange={handleChange} value={formData.name || ''}/> </label> </fieldset> <fieldset disabled={submitting}> <label> <p>Apples</p> <select name="apple" onChange={handleChange} value={formData.apple || ''}> <option value="">--Please choose an option--</option> <option value="fuji">Fuji</option> <option value="Jonagold">Jonagold</option> <option value="Cameo">Cameo</option> <option value="Empire">Empire</option> <option value="McIntosh">McIntosh</option> <option value="Golden Delicious">Golden Delicious</option> </select> </label> <label> <p>Count</p> <input type="number" name="count" onChange={handleChange} step="1" value={formData.count || ''}/> </label> <label> <p>Gift Wrap</p> <input type="checkbox" name="gift-wrap" onChange={handleChange} checked={formData['gift-wrap'] || false} disabled={formData.apple !== 'fuji'}/> </label> </fieldset> <button className="button" type="submit" disabled={submitting}>Submit</button> </form> </div> ) } export default App;src/App.css
//src/App.css body { background:#fff; font-family: Arial; } .wrapper { padding: 5px 20px; } .wrapper fieldset { margin: 20px 0; } .button { display: inline-block; position: relative; margin: 10px; padding: 10px; text-align: center; text-decoration: none; font: bold 12px/25px Arial, sans-serif; width: 300px; color: #3e5706; background: #a5cd4e; text-shadow: 1px 1px 1px rgba(255,255,255, .22); -webkit-border-radius: 30px; -moz-border-radius: 30px; border-radius: 30px; -webkit-box-shadow: 1px 1px 1px rgba(0,0,0, .29), inset 1px 1px 1px rgba(255,255,255, .44); -moz-box-shadow: 1px 1px 1px rgba(0,0,0, .29), inset 1px 1px 1px rgba(255,255,255, .44); box-shadow: 1px 1px 1px rgba(0,0,0, .29), inset 1px 1px 1px rgba(255,255,255, .44); -webkit-transition: all 0.15s ease; -moz-transition: all 0.15s ease; -o-transition: all 0.15s ease; -ms-transition: all 0.15s ease; transition: all 0.15s ease; }