I have a React Search Bar and I'm trying to prevent a form submission or re render if a user clicks enter.
I read up on react synthetic events and applied that logic to both an obSubmit handler for the form element and the input element, neither one is working when applied individually to each element or when applied to both and I'm not sure why.
Expected behavior: If a user clicks enter nothing will happen, no clearing of the input text or form submission
Current behavior: User clicks enter, text field is cleared and form is submitted triggering a re render
Here's a snapshot of my current Component:
const handleSubmit = event => {
event.preventDefault();
};
return (
<form onSubmit={handleSubmit}>
<StyledInput className={"inputWithIcon"}>
<Input
type="text"
value={text}
onChange={handleChange}
placeholder="Search"
onSubmit={e => {
e.preventDefault();
}}
/>
<div className="left-icon">
<svg viewBox="0 0 24 24" width="36px" height="36px">
...
</svg>
</div>
<button className="right-icon" onClick={clearInput}>
<svg width="24pt" height="24pt" viewBox="0 0 24 24" version="1.1">
...
</svg>
</button>
</StyledInput>
</form>
)
And a CodeSandbox demonstrating the issue: