Skip to content
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

Number input decimal cursor jump #278

Open
phll2 opened this issue Feb 4, 2025 · 6 comments
Open

Number input decimal cursor jump #278

phll2 opened this issue Feb 4, 2025 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@phll2
Copy link

phll2 commented Feb 4, 2025

Issue Description

When using a number input field, if a user enters a decimal number and then deletes the decimal part, the cursor unexpectedly jumps to the beginning of the input.

Steps to Reproduce

  1. Enter a decimal number (e.g., "1.25") into the number input field.
  2. Delete the decimal part (e.g., delete ".25").
  3. Observe that the cursor jumps to the beginning of the input instead of staying at the end.

Expected Behavior

The cursor should remain at its current position after deleting the decimal part.

Actual Behavior

The cursor jumps to the beginning of the input after deleting the decimal part.

Possible Cause

This issue may be caused by the number type being converted (from float to integer) when the decimal is removed, triggering a re-render of the input field. This re-render might be resetting the cursor position.

Can be observed: https://modularforms.dev/solid/playground/special

@fabian-hiller
Copy link
Owner

Have you saw this guide? Maybe this is the fix: https://modularforms.dev/solid/guides/controlled-fields#number-input-example

@fabian-hiller fabian-hiller self-assigned this Feb 4, 2025
@fabian-hiller fabian-hiller added the question Further information is requested label Feb 4, 2025
@phll2
Copy link
Author

phll2 commented Feb 4, 2025

I have yeah, it doesn't seem to be the solution unfortunately

@fabian-hiller
Copy link
Owner

Do you have an idea how to fix this?

@phll2
Copy link
Author

phll2 commented Feb 4, 2025

const getValue = createMemo<number | undefined>((prevValue) => {
   return Number.isInteger(props.value) ? `${props.value}.0` : prevValue;
 });

This is my best effort so far, it's not a solution (and causes it's own issues), but it points to the re-render being caused when the browser changes the value from a float to an integer

@phll2
Copy link
Author

phll2 commented Feb 4, 2025

it's interesting in that after it's been through it's initial cycle (adding the 0), it then seems to work with out jumping or adding the zero, I just can't work out why

@fabian-hiller
Copy link
Owner

Thanks for your research. Since Modular Forms only provides the data layer, you should be able to customize the integration with the HTML input element to avoid this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants