Skip to content

Commit

Permalink
Use onSubmit event on the form
Browse files Browse the repository at this point in the history
Avoid manually checking the `enter` key on the coupon field. Instead, make the browser do all the heavy lifting of listening to the submit event.
  • Loading branch information
bespoyasov committed Jan 21, 2022
1 parent 86dea9d commit 1631356
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ function Coupon({ onEnter }) {
name="coupon"
value={coupon}
onChange={(e) => setCoupon(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
onEnter(e);
}
}}
/>
);
}
Expand Down Expand Up @@ -116,13 +110,11 @@ function App() {
if (!error) {
if (status === "idle") {
return (
<form>
<form onSubmit={handleSubmitByValidatingDataAndCreatingOrder}>
<UserInfo user={user} />
<ProductList products={cart.products} />
<Coupon onEnter={handleSubmitByValidatingDataAndCreatingOrder} />
<button onClick={handleSubmitByValidatingDataAndCreatingOrder}>
Purchase
</button>
<Coupon />
<button>Purchase</button>
</form>
);
} else if (status === "loading") {
Expand Down

0 comments on commit 1631356

Please sign in to comment.