-
I have the following example with a function called function digits (n)
{
var rx = new RegExp((n ?? `^\\d+$`) || `^\\d{${n}}$`)
return str => (str == null || str == '') ? null : rx.test(str)
}
const { label, input } = van.tags;
function field (state, validation)
{
let text = van.state('');
van.derive(() => { console.log(`text: ${text.val}`); });
let valid = van.derive(() => validation(text.val));
van.derive(() => { console.log(`valid: ${valid.val}`); });
van.derive(() => { if (valid.val) state.val = text.val; });
return input({type: 'text', value: text,
oninput: e => { text.val = e.target.value; }});
}
let serno = van.state();
van.derive(() => { console.log (`serno: ${serno.val}`); });
van.add(document.body,
field(serno, digits(3))); This is what happens, when I run my example: When I start the example the console shows:
Now I enter 1 and the console shows:
Now I enter 2 and the console shows:
Now I enter 3 and the console shows:
So far so good, but now I enter backspace, which causes the following error:
The reason may be the fact, that the The question is: can the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I tried experimenting with your problem for a bit and found that you are calling this function twice (it outputs both true and false) when you backspace. van.derive(() => { if (valid.val) state.val = text.val; } the reason why it outputs twice is because in your oninput function, you are setting the text value in the oninput while also having the text value in this function: van.derive(() => { if (valid.val) state.val = text.val; }); So what basically happens is:
So, I guess the solution would be to either just combine the function for oninput and validation or... you could add the text value to a non-state variable first, then add it to that function (so the derived function wouldn't be called when text.val changes). Like this: function digits (n)
{
var rx = new RegExp((n ?? `^\\d+$`) || `^\\d{${n}}$`)
return str => (str == null || str == '') ? null : rx.test(str)
}
const { label, input } = van.tags;
function field (state, validation)
{
let text = van.state('');
let text_temp = text.val;
van.derive(() => {
text_temp = text.val
console.log(`text: ${text.val}`);
});
let valid = van.derive(() => validation(text.val));
van.derive(() => { console.log(`valid: ${valid.val}`); });
van.derive(() => {
if (valid.val) state.val = text_temp; //a non-state variable
});
return input({type: 'text', value: text,
oninput: e => { text.val = e.target.value; }});
}
let serno = van.state();
van.derive(() => { console.log (`serno: ${serno.val}`); });
van.add(document.body,
field(serno, digits(3))); |
Beta Was this translation helpful? Give feedback.
-
My suggestion is: don't define van.derive(() => { if (validation(text.val)) state.val = text.val; }); Sometimes, the simplest solution could be the best one :-) |
Beta Was this translation helpful? Give feedback.
-
I can think of 2 ways to solve the problem while keeping
let valid = van.derive(() => ({
result: validation(text.val)
for: text.val
}))
van.derive(() => {
if (valid.val.result) state.val = valid.val.for
})
let valid = van.derive(() => validation(text.val))
van.derive(() => {
if (valid.val) setTimeout(() => state.val = text.val)
}) Let me know if one of the solutions can work for you. |
Beta Was this translation helpful? Give feedback.
I can think of 2 ways to solve the problem while keeping
valid
a state.state
asynchronously:Let me know if one of the solutions can work for you.