Skip to content

Commit

Permalink
Add exercise 06 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithin S. Sabu committed Oct 31, 2020
1 parent 976b705 commit a8ad92e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/06.extra-3.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import {render, screen} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import App from '../final/06.extra-3'
// import App from '../exercise/06'
// import App from '../final/06.extra-3'
import App from '../exercise/06'

beforeAll(() => {
jest.spyOn(global, 'alert').mockImplementation(() => {})
Expand Down
38 changes: 19 additions & 19 deletions src/exercise/06.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// Basic Forms
// http://localhost:3000/isolated/exercise/06.js

import { useRef } from "react";
import React from 'react'

function UsernameForm({onSubmitUsername}) {
// 🐨 add a submit event handler here (`handleSubmit`).
// πŸ’° Make sure to accept the `event` as an argument and call
// `event.preventDefault()` to prevent the default behavior of form submit
// events (which refreshes the page).
//
// 🐨 get the value from the username input (using whichever method
// you prefer from the options mentioned in the instructions)
// πŸ’° For example: event.target.elements[0].value
// 🐨 Call `onSubmitUsername` with the value of the input

// 🐨 add the onSubmit handler to the <form> below

// 🐨 replace input's name attribute with id attribute
// 🐨 make sure to associate the label to the input.
// to do so, set the value of 'htmlFor' prop of the label to the id of input
const inputRef = useRef(null);
const [username, setUsername] = React.useState('');

const handleChange = (e) => {
setUsername(e.target.value.toLowerCase());
}

const handleSubmit = (e) => {
e.preventDefault();

const username = inputRef.current.value;
onSubmitUsername(username);
}

return (
<form>
<form onSubmit={handleSubmit}>
<div>
<label>Username:</label>
<input name="username" type="text" />
<label htmlFor="username">Username:</label>
<input id="username" type="text" ref={inputRef} onChange={handleChange} value={username}/>
</div>
<button type="submit">Submit</button>
</form>
Expand All @@ -35,4 +35,4 @@ function App() {
return <UsernameForm onSubmitUsername={onSubmitUsername} />
}

export default App
export default App

0 comments on commit a8ad92e

Please sign in to comment.