Skip to content

Commit

Permalink
feat: make the search input work with enter and add * to end when sub…
Browse files Browse the repository at this point in the history
…mitting (#5)
  • Loading branch information
ytkimirti authored Jan 13, 2025
1 parent c4c1046 commit f7e936b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
18 changes: 16 additions & 2 deletions src/components/databrowser/components/sidebar/search-input.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from "react"
import { useDatabrowserStore } from "@/store"
import { IconX } from "@tabler/icons-react"

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

export const SearchInput = () => {
const { setSearchKey, search } = useDatabrowserStore()
const [state, setState] = useState(search.key)

const submit = (value: string) => {
if (value.trim() !== "" && !value.includes("*")) value = `${value}*`
setSearchKey(value)
setState(value)
}

return (
<div className="relative grow">
<Input
placeholder="Search"
className={"rounded-l-none border-zinc-300 font-normal"}
onChange={(e) => setSearchKey(e.target.value)}
value={search.key}
onKeyDown={(e) => {
if (e.key === "Enter") submit(e.currentTarget.value)
}}
onChange={(e) => {
setState(e.currentTarget.value)
if (e.currentTarget.value.trim() === "") submit("")
}}
value={state}
/>
{search.key && (
<Button
Expand Down
10 changes: 1 addition & 9 deletions src/components/databrowser/hooks/use-keys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ const KeysContext = createContext<
export const FETCH_KEYS_QUERY_KEY = "use-fetch-keys"

export const KeysProvider = ({ children }: PropsWithChildren) => {
const { search: searchState } = useDatabrowserStore()

const search = useMemo(
() => ({
key: searchState.key.includes("*") ? searchState.key : `*${searchState.key}*`,
type: searchState.type,
}),
[searchState]
)
const { search } = useDatabrowserStore()

const { fetchKeys, resetCache } = useFetchKeys(search)
const pageRef = useRef(0)
Expand Down

0 comments on commit f7e936b

Please sign in to comment.