Skip to content

Submit input field when pressing Return#1537

Merged
jotaen4tinypilot merged 3 commits intomasterfrom
submit-on-enter
Jul 27, 2023
Merged

Submit input field when pressing Return#1537
jotaen4tinypilot merged 3 commits intomasterfrom
submit-on-enter

Conversation

@jotaen4tinypilot
Copy link
Contributor

@jotaen4tinypilot jotaen4tinypilot commented Jul 27, 2023

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.
Review on CodeApprove

# Conflicts:
#	app/templates/styleguide.html
Copy link
Contributor

@jdeanwallace jdeanwallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@jdeanwallace jdeanwallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

@jotaen4tinypilot jotaen4tinypilot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@jdeanwallace jdeanwallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated comment from CodeApprove ➜

Approved: I have approved this change on CodeApprove and all of my comments have been resolved.

@jdeanwallace jdeanwallace dismissed their stale review July 27, 2023 19:25

Review approved on CodeApprove

@jotaen4tinypilot jotaen4tinypilot merged commit 0e40e22 into master Jul 27, 2023
@jotaen4tinypilot jotaen4tinypilot deleted the submit-on-enter branch July 27, 2023 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants