formik icon indicating copy to clipboard operation
formik copied to clipboard

holding down enter key submits form over and over

Open jeran-urban opened this issue 3 years ago • 1 comments

Bug report

Current Behavior

if you have a formik form on the same page as the destination for the rendered results, and you click enter, it allows you to submit the form. If you hold down the enter key, it submits over and over until you take your finger off the enter key. (I think this may be forms in general, and not solely formik forms?)

Expected behavior

the form should submit once, and not repeatedly while holding down the enter key

Reproducible example

any default formik setup

Suggested solution(s)

can the onSubmit trigger on key up of enter or trigger in some way that it won't continue to submit while holding down enter?

using the following code for now as a workaround:

const [submitOnEnterDisabled, setSubmitOnEnterDisabled] = useState(false);
const ENTER_KEY_CODE = 13

// SpecialLibrary props
onSubmit={(values) => {
	if (!submitOnEnterDisabled){
		submit(values);
	}
}}

<Form 
	
	onKeyDown={(e) => {
		if (e.keyCode === ENTER_KEY_CODE) {
			if (!submitOnEnterDisabled){
				submit(values);
		    }
			setSubmitOnEnterDisabled(true);
		}
	}}
	  
	onKeyUp={(e) => {
	    if (e.keyCode === ENTER_KEY_CODE) {
			setSubmitOnEnterDisabled(false);
	    }
	}}
>

<button type="submit" onSubmit={submit}>Search</button>

Additional context

Your environment

Software Version(s)
Formik 2.2.9
React 17.0.2
TypeScript n/a
Browser chrome
npm/Yarn npm
Operating System windows

jeran-urban avatar Jun 22 '22 16:06 jeran-urban

This is because html vanilla forms check keyDown instead of checking for a full click / keyUp

TamirCode avatar Aug 09 '22 08:08 TamirCode