Replies: 4 comments
-
|
If you are using effects that react to property changes, you could maybe use an internal flag along the lines of The effect has to make sure to always capture the value property as a dependency before exiting. $effect.pre(() => {
value; // dependency
if (updating)
return;
// [create local formatted string for input]
});Though, even if you don't have such a mechanism, the values should converge since the formatting is deterministic. But effects that update state should be avoided in general. For a simple component where you just have a value and formatted value, a derived that can be overwritten on input might be enough. You can also interact with the input element directly if the underlying state is unaffected. What does seem a bit tricky with the derived approach, is updating the value live on input because the formatted value would try to change while the user is still typing. Maybe a There are also several Svelte component libraries out there, you could check if any of them have implemented something similar. |
Beta Was this translation helpful? Give feedback.
-
|
See one of these, which are the same solution with a slightly different approach: DEMO: Keep caret in correct position after formatting the textbox's value - Bindable caretPos property These solve an extra problem: How to get to the input element when the input element is provided by a custom Input component. You can forget about that part if it doesn't apply to you. The solution is getter/setter binding + setting the caret to the right position. This code does this for a specific format, so you'll need to come up with the algorithm for your formatted value. |
Beta Was this translation helpful? Give feedback.
-
|
When you bind value directly to , Svelte will overwrite the input field each time your internal value changes. So Maybe need to separate:
I can be wrong also, But maybe just it try |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for all that input! @webJose pointed me to another problem which I overlooked when hacking together a solution: Setting the position of the caret to the correct place. While his solution works with his format, it can get a bit more tricky with decimal inputs... I'm not there yet... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to wrap my head around how to have a nice decimal input field for my little svelte project. I think, I'm trapped in kind of a situation where I can achieve only two out of three requirements:
,1to0,10and so on...Is there a way to achieve this or am I trying to do something too complicated?
I have tried:
But either I ran into some kind of infinite loops or things are overwriting each other...
I'd really appreciate if someone could hint me if this is possible and how to do this. Thank you very much!
Beta Was this translation helpful? Give feedback.
All reactions