|
4 | 4 | import * as React from 'react'
|
5 | 5 |
|
6 | 6 | 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() |
17 | 10 |
|
18 |
| - // 🐨 add the onSubmit handler to the <form> below |
| 11 | + onSubmitUsername(username) |
| 12 | + } |
19 | 13 |
|
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 | + } |
22 | 18 | return (
|
23 |
| - <form> |
| 19 | + <form onSubmit={handleSubmit}> |
24 | 20 | <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 | + /> |
27 | 28 | </div>
|
28 | 29 | <button type="submit">Submit</button>
|
29 | 30 | </form>
|
|
0 commit comments