Skip to content

Commit

Permalink
Fix edit search query issue (#454)
Browse files Browse the repository at this point in the history
* Fix edit search query issue

* Update useEffect to avoid missing dep warning

---------

Co-authored-by: Cole Bemis <colebemis@github.com>
  • Loading branch information
AnushDeokar and colebemis authored Dec 15, 2024
1 parent 7dbf8fe commit e4b6a23
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
41 changes: 29 additions & 12 deletions src/components/search-input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react"
import { useGetter } from "../hooks/getter"
import { cx } from "../utils/cx"
import { IconButton } from "./icon-button"
import { ClearIcon16, SearchIcon16 } from "./icons"
Expand All @@ -12,10 +13,32 @@ type SearchInputProps = Omit<React.ComponentPropsWithoutRef<"input">, "onChange"
export function SearchInput({
shortcut,
placeholder = "Search…",
value,
onChange,
...props
}: SearchInputProps) {
const ref = React.useRef<HTMLInputElement>(null)
const [inputValue, setInputValue] = React.useState(value || "")
const getInputValue = useGetter(inputValue)

React.useEffect(() => {
if (value !== getInputValue()) {
setInputValue(value || "")
}
}, [value, getInputValue])

function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
const newValue = event.target.value
setInputValue(newValue)
onChange?.(newValue)
}

function clearInput() {
setInputValue("")
onChange?.("")
ref.current?.focus()
}

return (
<div className="relative">
<div className="absolute inset-y-0 left-0 grid aspect-square place-items-center text-text-secondary">
Expand All @@ -25,34 +48,28 @@ export function SearchInput({
ref={ref}
className={cx(
"focus-ring h-10 w-full rounded-lg bg-bg-secondary pl-10 [-webkit-appearance:none] [font-variant-numeric:inherit] placeholder:text-text-secondary coarse:h-12 coarse:pl-12 [&:not(:focus-visible)]:hover:ring-1 [&:not(:focus-visible)]:hover:ring-inset [&:not(:focus-visible)]:hover:ring-border-secondary",
props.value ? "pr-10 coarse:pr-12" : "pr-3 coarse:pr-4",
value ? "pr-10 coarse:pr-12" : "pr-3 coarse:pr-4",
)}
type="search"
value={inputValue}
placeholder={placeholder}
onChange={(e) => onChange?.(e.target.value)}
onChange={handleChange}
{...props}
/>
{shortcut && !props.value ? (
{shortcut && !inputValue ? (
<div
aria-hidden
className="absolute inset-y-0 right-0 flex items-center pr-3 coarse:hidden"
>
<Keys keys={shortcut} />
</div>
) : null}
{props.value ? (
{inputValue ? (
<div
aria-hidden
className="absolute inset-y-0 right-0 grid aspect-square place-items-center"
>
<IconButton
aria-label="Clear"
tabIndex={-1}
onClick={() => {
onChange?.("")
ref.current?.focus()
}}
>
<IconButton aria-label="Clear" tabIndex={-1} onClick={clearInput}>
<ClearIcon16 />
</IconButton>
</div>
Expand Down
9 changes: 9 additions & 0 deletions src/hooks/getter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useCallback, useRef, useEffect } from "react"

export function useGetter<T>(value: T) {
const valueRef = useRef(value)
useEffect(() => {
valueRef.current = value
}, [value])
return useCallback(() => valueRef.current, [])
}
9 changes: 1 addition & 8 deletions src/routes/notes_.$.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
widthAtom,
} from "../global-state"
import { useEditorSettings } from "../hooks/editor-settings"
import { useGetter } from "../hooks/getter"
import { useIsScrolled } from "../hooks/is-scrolled"
import { useDeleteNote, useNoteById, useSaveNote } from "../hooks/note"
import { useSearchNotes } from "../hooks/search"
Expand Down Expand Up @@ -119,14 +120,6 @@ function RouteComponent() {

const toggleModeShortcut = ["⌥", "⇥"]

function useGetter<T>(value: T) {
const valueRef = useRef(value)
useEffect(() => {
valueRef.current = value
}, [value])
return useCallback(() => valueRef.current, [])
}

function renderTemplate(template: Template, args: Record<string, unknown> = {}) {
let text = ejs.render(template.body, args)
text = removeFrontmatterComments(text)
Expand Down

0 comments on commit e4b6a23

Please sign in to comment.