/
useSearch.tsx
48 lines (42 loc) · 1.56 KB
/
useSearch.tsx
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
import React, {useState} from "react"
import {Box, Input, InputGroup, InputRightAddon} from "@chakra-ui/react"
import {Search2Icon} from "@chakra-ui/icons"
export const useSearch = () => {
const [inputSearch, setInputSearch] = useState("")
const [activeInputSearch, setActiveInputSearch] = useState("")
const searchProps = {
inputProps: {
onChange: (e: React.ChangeEvent ) => {
setInputSearch((e.target as HTMLInputElement).value)
},
onKeyPress: (e: React.KeyboardEvent<HTMLDivElement> ) => {
if(e.key=== "Enter"){ setActiveInputSearch(inputSearch) }
},
value: inputSearch,
},
buttonProps: {
onClick: () => {
setActiveInputSearch(inputSearch)
//callback()
},
cursor: "pointer",
},
}
const reset = () => {
setActiveInputSearch("")
setInputSearch("")
}
return [searchProps, activeInputSearch, reset] as const
}
export const Search = ({ inputProps, buttonProps }: {inputProps: object, buttonProps: object}) => {
return (
<Box key={"searchInput"}>
<InputGroup borderColor={"purple"}>
<Input type={"text"} name={"search"} placeholder={"Search"} {...inputProps} borderRadius={"0px"} />
<InputRightAddon {...buttonProps} backgroundColor={"purple"} borderRadius={"0px"}>
<Search2Icon color={"white"} />
</InputRightAddon>
</InputGroup>
</Box>
)
}