on:change not updating AMP State through input field if keyboard is not closed
What's the issue?
We're trying to have an input field which on change causes an AMP State variable to be updated as well. Then clicking on a button causes us to use this updated AMP State to dynamically construct a URL.
The issue is that, if I enter a value in the field but don't close the keyboard and then try clicking on a model, the AMP state is not updated. This happens even though the input field is tied to an on:change event.
Only closing the keyboard causes the amp state to be updated. Is it possible to include state changes even when the keyboard is open and tied directly to individual keystrokes as that should be the behavior with on change.
How do we reproduce the issue?
Create an input field and tie it to an AMP State element via an onchange event. Type in values without closing the mobile keyboard. The console should show no updates to the state unless the keyboard is closed.
What browsers are affected?
All browsers and devices,
Which AMP version is affected?
This was seen as recently as Version 1533253141109 but it likely exists prior to this as well.
The change event only triggers when you remove focus from the input, which is why the binding is not updating until you close the keyboard.
Instead of binding to change, you can use input-throttled or input-debounced. For example:
<input on="input-debounced:AMP.setState({ option: event.value })">
Debounced will update the binding after the user has stopped typing for a bit. Throttled will update periodically (but not on every character typed necessarily).
Perhaps this can be made more clear through documentation.
Created a PR to update the documentation around setState for <amp-bind> that should hopefully make this more clear by linking to a page with more detail around the various events.
Thanks so much for the prompt response. Is it not possible to change the behavior of on:change such that it responds to immediate user input. Using debounce or input-throttled is less than ideal given the delay associated with both of those and the perception for the user. Other elements work in a similar fashion given that as soon as the element is edited, the appropriate state bound to it is modified.
Is it not possible to change the behavior of on:change such that it responds to immediate user input.
For change, I think we want to match the semantics of the change DOM event, which is on blur for inputs. Adding an input event that is not throttled/debounced should be fine if there is a case where you need things to be very responsive to the user's input.
Understood. Let me go back to confirm if the two recommended events work and if not, maybe we can file an FR for this. Thanks again for the prompt feedback!