-
-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cursor jumps to end of search params bound input #3162
Comments
I've added a failing test here: #3174 I'm probably not the right person to fix it simply because I understand why startTransition is now the default, but I can see this being a tricky one to balance the need for startTransition with the need for binding inputs successfully. Also startTransition is probably not the whole story here, in theory that shouldn't affect binding but I've seen it do so before |
as the router state is outside of react, React seems to not be able to track the cursor position as it would with a local This works around that: const Zod = () => {
const search = Route.useSearch({
select: (search) => search.search ?? '',
})
const navigate = useNavigate({ from: Route.fullPath })
const [state, setState] = React.useState(search)
return (
<>
<Header title="Zod" />
<Content>
<Search
search={state}
onChange={(v) => {setState(v); navigate({search: { search: v } })}}
/>
<React.Suspense>
<Users search={search} />
</React.Suspense>
</Content>
</>
)
} A different solution is to use an uncontrolled input via export const Search = ({ search, onChange }: SearchProps) => {
return (
<div className="flex gap-2 align-center items-center">
<label>Search</label>
<input
type="search"
className="border p-1 px-2 rounded"
defaultValue={search}
onChange={(e) => onChange(e.target.value)}
></input>
</div>
)
} |
So maybe the bugfix is the react-router package needs to have its own in-react cache which synchronises out to the router-core? Possibly with useOptimistic instead of useState? Would you accept a PR for that? |
Which project does this relate to?
Router
Describe the bug
It's common to bind a Filter input to search params so that URLs can be shared, and a user should be able to type anywhere in the input and it behave predictably.
Current behaviour of this isn't predictable though, if you type into the end of an input it behaves fine, but if you type into the middle, the cursor jumps to the end.
I suspect this is because startTransition is called by default under the hood now, but there doesn't appear to be a way to disable this behaviour or any past github issue which describes a workaround or fix
Your Example Website or App
Example project: search-validator-adapters
Steps to Reproduce the Bug or Issue
Expected behavior
Typing anywhere in the input types where your cursor is and retains cursor position
Screenshots or Videos
Platform
Additional context
No response
The text was updated successfully, but these errors were encountered: