Submit input field when pressing Return#1537
Conversation
# Conflicts: # app/templates/styleguide.html
jdeanwallace
left a comment
There was a problem hiding this comment.
Automated comment from CodeApprove ➜
⏳ Approval Pending (1 unresolved comments)
Approval will be granted automatically when all comments are resolved
In: Discussion
(optional) Submitting an input field when pressing the Return key is kind of already supported in HTML, but it requires the input to be wrapped in a <form> element. So as an alternative to adding a keydown listener, we could wrap the <input> and <button> in a <form> element to achieve the same thing in a native way, however more verbosely.
For example:
<form>
<input type="text"/>
<button type="button">
Change and Restart
</button>
</form>
<script>
document.querySelector("form").addEventListener("submit", function(event) {
event.preventDefault();
// Do fetch and stuff.
})
</script>This would obviously change our coding style going forward, but I think it might be good to make use of native functionality.
👀 @jotaen4tinypilot it's your turn please take a look
jdeanwallace
left a comment
There was a problem hiding this comment.
Automated comment from CodeApprove ➜
⏳ Approval Pending (1 unresolved comments)
Approval will be granted automatically when all comments are resolved
In: Discussion
Oh, small correction, button should be type="submit".
👀 @jotaen4tinypilot it's your turn please take a look
jotaen4tinypilot
left a comment
There was a problem hiding this comment.
Automated comment from CodeApprove ➜
In: Discussion
Yeah, interesting question. I’m personally torn on using <form>’s in single page apps. On the one hand, they can bring useful behaviour, such as in the case we have here. On the other hand, the default behaviour can also sometimes be undesired (e.g., auto-submit on ENTER with external redirect to the current page URL, unless you prevent that); code-wise, it may also become tricky if the markup becomes more complex, because you might have to wrap the subtree into a <form> at a potentially very broad level (if the UI controls aren’t as closely together in the code as here).
I might be biased here, though. At least from my experience with React I think it’s quite common to not use <form> elements, but to wire things manually, to have most control over the behaviour and control flows.
Maybe/Potentially worth to discuss anyway. (E.g. in the next dev meeting or so?)
jdeanwallace
left a comment
There was a problem hiding this comment.
Automated comment from CodeApprove ➜
Approved: I have approved this change on CodeApprove and all of my comments have been resolved.
Community-specific part of #897. Users can now submit a new hostname by pressing the “Return” key.
It seems that the
Element.click()method is exactly what we need in terms of behaviour, and I thought it made more sense to make the implementation so that it “delegates” to the button click-handler, rather than repeating the internal logic of the latter.To me, the event handler would be expressive and straightforward enough to be inlined.
I also added a brief explanation in the style guide, to capture our intent.
