forked from kentcdodds/beginners-guide-to-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20-dynamic-forms.html
37 lines (33 loc) · 1.21 KB
/
20-dynamic-forms.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
<body>
<div id="root"></div>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/babel.js"></script>
<script type="text/babel">
function UsernameForm() {
const [username, setUsername] = React.useState('')
const isLowerCase = username === username.toLowerCase()
const error = isLowerCase ? null : 'Username must be lower case'
function handleSubmit(event) {
event.preventDefault()
alert(`You entered: ${username}`)
}
function handleChange(event) {
setUsername(event.target.value)
}
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="usernameInput">Username:</label>
<input id="usernameInput" type="text" onChange={handleChange} />
</div>
<div style={{color: 'red'}}>{error}</div>
<button disabled={Boolean(error)} type="submit">
Submit
</button>
</form>
)
}
ReactDOM.render(<UsernameForm />, document.getElementById('root'))
</script>
</body>