reactstrap Bootstrap 4 components that allow a great deal of flexibility and prebuilt validation https://reactstrap.github.io/
Install bootstrap
npm install bootstrap
C:\reactdev\myreactdev>npm install bootstrap
Install reactstrap
npm install reactstrap
C:\reactdev\myreactdev>npm install reactstrap
import bootstrap to src/index.js
import 'bootstrap/dist/css/bootstrap.min.css';
1 2 3 4 5 6 7 8 9 10 | //src/index.js import React from 'react' ; import ReactDOM from 'react-dom' ; import App from './App' ; import 'bootstrap/dist/css/bootstrap.min.css' ; ReactDOM.render( <App />, document.getElementById( 'root' ) ) |
1 2 3 4 5 6 7 8 9 10 11 12 | //public/index.html <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "utf-8" /> <meta name= "viewport" content= "width=device-width, initial-scale=1" /> <title>ReactJS </title> </head> <body> <div id= "root" ></div> </body> </html> |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | //src/App.js import { Component } from 'react' ; import { Form, FormFeedback, FormGroup, FormText, Label, Input, Button, } from 'reactstrap' ; import './App.css' ; class App extends Component { constructor(props) { super(props); this.state = { email: '' , password: '' , validate: { emailState: '' , }, }; this.handleChange = this.handleChange.bind(this); } handleChange = (event) => { const { target } = event; const value = target.type === 'checkbox' ? target.checked : target.value; const { name } = target; this.setState({ [name]: value, }); }; validateEmail(e) { const emailRex = /^(([^<>()[\]\\.,;:\s@ "]+(\.[^<>()[\]\\.,;:\s@" ]+)*)|( ".+" ))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; const { validate } = this.state; if (emailRex.test(e.target.value)) { validate.emailState = 'has-success' ; } else { validate.emailState = 'has-danger' ; } this.setState({ validate }); } submitForm(e) { e.preventDefault(); console.log(`Email: ${this.state.email}`); } render() { const { email, password } = this.state; return ( <div className= "App" > <h2>Sign In</h2> <Form className= "form" onSubmit={(e) => this.submitForm(e)}> <FormGroup> <Label>Username</Label> <Input type= "email" name= "email" id= "exampleEmail" placeholder= "example@example.com" valid={this.state.validate.emailState === "has-success" } invalid={this.state.validate.emailState === "has-danger" } value={email} onChange={(e) => { this.validateEmail(e); this.handleChange(e); }} /> <FormFeedback> Uh oh! Looks like there is an issue with your email. Please input a correct email. </FormFeedback> <FormFeedback valid> That 's a tasty looking email you' ve got there. </FormFeedback> <FormText>Your username is most likely your email.</FormText> </FormGroup> <FormGroup> <Label for = "examplePassword" >Password</Label> <Input type= "password" name= "password" id= "examplePassword" placeholder= "********" value={password} onChange={(e) => this.handleChange(e)} /> </FormGroup> <br/> <Button>Submit</Button> </Form> </div> ); } } export default App; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //src/App.css .App { border: 2px solid #d3d3d3; border-radius: .5em; margin-bottom: 1em; margin-left: auto; margin-right: auto; margin-top: 100px; padding: 1em; text-align: left; width: 600px; } .form { padding: 1em; } label { font-weight: 600; } |