Skip to content

Commit f7e936b

Browse files
authored
feat: make the search input work with enter and add * to end when submitting (#5)
1 parent c4c1046 commit f7e936b

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/components/databrowser/components/sidebar/search-input.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useState } from "react"
12
import { useDatabrowserStore } from "@/store"
23
import { IconX } from "@tabler/icons-react"
34

@@ -6,14 +7,27 @@ import { Input } from "@/components/ui/input"
67

78
export const SearchInput = () => {
89
const { setSearchKey, search } = useDatabrowserStore()
10+
const [state, setState] = useState(search.key)
11+
12+
const submit = (value: string) => {
13+
if (value.trim() !== "" && !value.includes("*")) value = `${value}*`
14+
setSearchKey(value)
15+
setState(value)
16+
}
917

1018
return (
1119
<div className="relative grow">
1220
<Input
1321
placeholder="Search"
1422
className={"rounded-l-none border-zinc-300 font-normal"}
15-
onChange={(e) => setSearchKey(e.target.value)}
16-
value={search.key}
23+
onKeyDown={(e) => {
24+
if (e.key === "Enter") submit(e.currentTarget.value)
25+
}}
26+
onChange={(e) => {
27+
setState(e.currentTarget.value)
28+
if (e.currentTarget.value.trim() === "") submit("")
29+
}}
30+
value={state}
1731
/>
1832
{search.key && (
1933
<Button

src/components/databrowser/hooks/use-keys.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,7 @@ const KeysContext = createContext<
2323
export const FETCH_KEYS_QUERY_KEY = "use-fetch-keys"
2424

2525
export const KeysProvider = ({ children }: PropsWithChildren) => {
26-
const { search: searchState } = useDatabrowserStore()
27-
28-
const search = useMemo(
29-
() => ({
30-
key: searchState.key.includes("*") ? searchState.key : `*${searchState.key}*`,
31-
type: searchState.type,
32-
}),
33-
[searchState]
34-
)
26+
const { search } = useDatabrowserStore()
3527

3628
const { fetchKeys, resetCache } = useFetchKeys(search)
3729
const pageRef = useRef(0)

0 commit comments

Comments
 (0)