Format text input / transformation
igorsegallafa opened this issue · 3 comments
Is there anyway to use the format expression on text input?
I tried something like this, but didn't work:
<input class="input" type="text" data-value="input" data-event-change="input = input | format(2)" autofocus/>
Should I do this transformation entirely in C++? If so, what is the best way for that? I think to use data-event-change or data-event-textinput for that, although, I can't get the updated value from input (probably the handler is fired before the changes).
With this code here I would be worried about what looks like a binding loop. When the value updates, there will be an event, which updates the value again, new event... The loop might be broken by that I believe it does not emit the change if it happens to have the same value. But in general I think such binding loops should be avoided.
I think ideally we shouldn't emit change events when changing it programmatically, I believe that's how HTML works.
But regardless, there is a sort of double-assignment here. If you want to respond to changes, you should rather use a data-attr-value combined with data-event-change, and remove data-value. You could also make use of ev.value inside the event-change expression.
Oh thank you for the help, it works. If someone needs something similar, here is the solution:
<input class="input" type="text" data-attr-value="input" data-event-change="input_change" autofocus/>
auto Value = Event.GetParameter<Rml::String>("value", "");
m_Input = String::FormatNumber(Value.c_str(), 2).ToString();
Model.DirtyVariable("input");Reflecting a bit more on this one, I believe the initial issue here might have been a result of this one: #668
And the binding loop might be avoided altogether if we don't emit events when programmatically setting the value, as described here: #702
So all in all, when we address those suggested changes, the initial code here should hopefully work fine :)