Skip to content

Commit bce4199

Browse files
author
=
committed
Implement Search
1 parent 49a3e80 commit bce4199

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

src/App.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
import React from "react";
2-
import logo from "./logo.svg";
3-
import "./App.css";
2+
import Search from "./components/Search3";
43

54
function App() {
6-
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
14-
Learn React
15-
</a>
16-
<button className="btn btn-large btn-success">Try me</button>
17-
</header>
18-
</div>
19-
);
5+
return <Search />;
206
}
217

228
export default App;

src/components/Search.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react";
2+
3+
class Search extends React.Component {
4+
state= {
5+
username: '',
6+
}
7+
8+
handleUserNameChange = e => {
9+
const value = e.target.value;
10+
11+
this.setState({
12+
username: value,
13+
});
14+
};
15+
16+
render() {
17+
const { username } = this.state;
18+
19+
return <input value={username} onChange={this.handleUserNameChange} type="text" name="username" placeholder="Enter username" />;
20+
}
21+
}
22+
23+
export default Search;

src/components/Search2.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react";
2+
3+
class Search2 extends React.Component {
4+
inputRef = React.createRef();
5+
6+
handleClick = () => {
7+
const value = this.inputRef.current.value;
8+
9+
alert(`The value of the input field is ${value}`);
10+
};
11+
12+
render() {
13+
return (
14+
<div>
15+
<input ref={this.inputRef} type="text" name="username" placeholder="Enter username" />
16+
<button onClick={this.handleClick}>Click Me</button>
17+
</div>
18+
);
19+
}
20+
}
21+
22+
export default Search2;

src/components/Search3.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from "react";
2+
3+
class Search3 extends React.Component {
4+
handleKeyDown = e => {
5+
if (e.keyCode === 13) {
6+
const value = e.target.value;
7+
alert(`The value of the input field is ${value}`);
8+
}
9+
};
10+
11+
render() {
12+
return <input onKeyDown={this.handleKeyDown} type="text" name="username" placeholder="Enter username" />;
13+
}
14+
}
15+
16+
export default Search3;

0 commit comments

Comments
 (0)