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

Format text input / transformation #701

Closed
igorsegallafa opened this issue Oct 17, 2024 · 3 comments
Closed

Format text input / transformation #701

igorsegallafa opened this issue Oct 17, 2024 · 3 comments
Labels

Comments

@igorsegallafa
Copy link
Contributor

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).

@mikke89 mikke89 added data binding support Usage questions labels Oct 17, 2024
@mikke89
Copy link
Owner

mikke89 commented Oct 17, 2024

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.

@igorsegallafa
Copy link
Contributor Author

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");

@mikke89
Copy link
Owner

mikke89 commented Oct 19, 2024

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 :)

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

No branches or pull requests

2 participants