Skip to content

Commit 5663fef

Browse files
committed
Add exercise 6
1 parent 95e9e5a commit 5663fef

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/exercise/06.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44
import * as React from 'react'
55

66
function UsernameForm({onSubmitUsername}) {
7-
// 🐨 add a submit event handler here (`handleSubmit`).
8-
// 💰 Make sure to accept the `event` as an argument and call
9-
// `event.preventDefault()` to prevent the default behavior of form submit
10-
// events (which refreshes the page).
11-
// 📜 https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
12-
//
13-
// 🐨 get the value from the username input (using whichever method
14-
// you prefer from the options mentioned in the instructions)
15-
// 💰 For example: event.target.elements[0].value
16-
// 🐨 Call `onSubmitUsername` with the value of the input
7+
const [username, setUsername] = React.useState('')
8+
const handleSubmit = event => {
9+
event.preventDefault()
1710

18-
// 🐨 add the onSubmit handler to the <form> below
11+
onSubmitUsername(username)
12+
}
1913

20-
// 🐨 make sure to associate the label to the input.
21-
// to do so, set the value of 'htmlFor' prop of the label to the id of input
14+
const handleChange = event => {
15+
const {value} = event.target
16+
setUsername(value.toLowerCase())
17+
}
2218
return (
23-
<form>
19+
<form onSubmit={handleSubmit}>
2420
<div>
25-
<label>Username:</label>
26-
<input type="text" />
21+
<label htmlFor="usernameInput">Username:</label>
22+
<input
23+
id="usernameInput"
24+
type="text"
25+
onChange={handleChange}
26+
value={username}
27+
/>
2728
</div>
2829
<button type="submit">Submit</button>
2930
</form>

0 commit comments

Comments
 (0)