Consider preserving cursor position and selection when writing to value on an input
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
Currently, if you use `@bind` with a property whose setter mutates the property value to something other than the incoming `value`, then Blazor's diffing algorithm will see that it has to update the `value` on the ``. That's good, but as a side effect, any selection and cursor position info will be discarded, and the cursor will move to the end of the textbox.
Sometimes this may be desirable, but sometimes it won't (e.g., https://github.com/dotnet/aspnetcore/issues/20067#issuecomment-602568745).
Things we could do:
* By default, we could have the JS-side code preserve the selection/cursor position around writes to `element.value`. That would be good if you do want to preserve cursor position, but pretty awful if you don't (e.g., if your property setter appends a character to the end of the string, you'd now be at the N-1th position).
* Or we could somehow have a way of signalling it (`@bind:preserveSelection`), though that still doesn't help if you have more complex rules about how the cursor position should be updated
* Or we just document a pattern for controlling it via JS interop
* Or we provide some JS interop helper for modifying cursor position *after* the next render completes
* Or we have a built-in concept of selection state via some object you can associate with the input, so we apply the selection state as an aspect of rendering (e.g., ``)
Note that this applies both to Blazor WebAssembly and Blazor Server.
Contributor guide
Assessment
This issue has not been assessed yet.