-
|
Hi, myState.val = newVal + 1;
setTimeout(() => myState.val = newVal, 0);But that feels... bad to say the least 😅 import van, { State } from "vanjs-core";
const Fraction = (numer: State<number>, denom: State<number>) => {
const { input, div } = van.tags;
const numerator = input(
{
class: "card fraction-num",
type: "text",
onkeyup: evt => {
if (evt.key === "Enter") {
numer.val = parseInt(evt.target?.value ?? "") - 1;
updateSize();
evt.preventDefault();
}
},
value: () => numer.val + 1,
oninput: evt => evt.target.size = evt.target.value.length,
});
var updateSize = () => { numerator.size = (numer.val + 1).toString().length; numerator.value = numer.val + 1 + ""; };
van.derive(updateSize);
return div({ class: "fraction" },
numerator,
div("/"),
div(() => denom.val),
);
};
export default Fraction;Here's the repo with my full code, the component in question is the Fraction component: https://github.com/FlynnD273/CrochetCounter |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
I just happened to read issue #465, it looks like the following code would actually work without flickering: // ...
if (evt.key === "Enter") {
const val = parseInt(evt.target?.value ?? "") - 1;
numer.val = val + 1;
Promise.resolve().then(() => numer.val = val);
evt.preventDefault();
}This seems... less bad than setTimeout, especially since it doesn't wait a full frame to update. I don't really want to run two updates though, so if there's a better way to do this I'm all ears. |
Beta Was this translation helpful? Give feedback.
-
|
I feel that for your use case, a more natural abstraction is to have a state to represent the string value of the input box, and another state for the parsed number from the string state (you can use |
Beta Was this translation helpful? Give feedback.
-
|
Oh I see. And then I would just use the string state in lockstep with the Input's value, exactly like the example in the docs? I think that makes sense. Thanks! |
Beta Was this translation helpful? Give feedback.
I feel that for your use case, a more natural abstraction is to have a state to represent the string value of the input box, and another state for the parsed number from the string state (you can use
van.deriveto achieve that). The width of the input box should depend on the string state, not the parsed number state.