-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearch.js
More file actions
56 lines (49 loc) · 2.02 KB
/
Search.js
File metadata and controls
56 lines (49 loc) · 2.02 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React, { useState, useContext } from 'react'
import GithubContext from '../../context/github/githubContext';
import AlertContext from '../../context/alert/alertContext';
import AlertState from '../../context/alert/AlertState';
const Search = () => {
const githubContext = useContext(GithubContext);
const alertContext = useContext(AlertContext); // calling alertContext to use alert Context
const [text, setText] = useState('');
const onChange = (e) => {
setText(e.target.value) //e.target name is used beacause if we want onChange function
}
const onSubmit = (e) => {
e.preventDefault();
if(text === ''){
alertContext.setAlert('Please enter something','light');
}
else{
githubContext.searchUsers(text);
setText('');
}
}
return (
<div>
<form className='form' onSubmit={onSubmit}>
<input type="text"
name="text"
placeholder="Search Users...."
value={text}
onChange={(e)=>onChange(e)}
/>
<input type="submit"
value="Search"
className="btn btn-dark btn-block"
/>
</form>
{/* if their is no user the clear button will not appear as thier is no user to clear*/}
{githubContext.users.length > 0 && (
<button
className='btn btn-light btn-block'
onClick={githubContext.clearUsers}
>
Clear
</button>
)
}
</div>
)
}
export default Search