Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  35] [ 4]  / answers: 1 / hits: 25818  / 5 Years ago, thu, january 24, 2019, 12:00:00

I have the following form written in React using Formik:



import React, { FunctionComponent } from 'react';
import { NavLink } from 'react-router-dom';
import { object, string } from 'yup';
import { Formik, FormikActions, Field, FormikProps } from 'formik';
import SimpleInput from './Fields/SimpleInput';
import FieldError from './Fields/FieldError';

interface FormValues {
email: string;
password: string;
}
interface OwnProps {
onSubmit: (data: FormValues) => any;
}

const validationSchema = object().shape({
email: string()
.email('Please enter a valid email address')
.required('Email is a required field'),
password: string()
.min(8)
.required('Password is a required field'),
});

type Props = OwnProps;

const LoginForm: FunctionComponent<Props> = ({ onSubmit }) => {
const initialValues = {
email: '',
password: '',
};

const onFormSubmit = async (values: FormValues, { setSubmitting }: FormikActions<FormValues>) => {
await onSubmit(values);
setSubmitting(false);
};

return (
<Formik
onSubmit={onFormSubmit}
initialValues={initialValues}
validationSchema={validationSchema}
render={({ handleSubmit, isSubmitting }: FormikProps<FormValues>) => (
<form className=ipx-form sign onSubmit={handleSubmit}>
<h1>Sign in</h1>
<div className=ipx-register-here>
( Don&#39;t have an account? ) &nbsp;
<NavLink to=/register>Register here</NavLink>
</div>
<br />
<Field name=email type=email component={SimpleInput} label=Email Address placeholder=Email />
<FieldError name=email />
<br />
<br />
<div className=fields>
<Field name=password type=password component={SimpleInput} label=Password placeholder=Password />
<FieldError name=password />
</div>
<br />
Forgot <NavLink to=/forgot-password>password?</NavLink>
<br />
<button className=button ipx-submit-button id=ipx-login-submit type=submit disabled={isSubmitting}>
<span className=ladda-label>Sign in</span>
</button>
</form>
)}
/>
);
};

export default LoginForm;


Which works fine if I click the button to submit the form (it dispatches the Redux action and logs the user in), however when I try to submit the form with a Return/Enter key press it fails to catch the event. I tried logging the event in the onSubmit prop of the <form> element but no event is triggered on Enter press at all. This same form was previously written using redux-form and the Return key functionality worked as it should.



I initially thought it might be due to the asynchronous form handling but I switched to a regular synchronous function and it did not work as well.



Has anyone ever experienced something similar, and if so, please share any fixes.



Thank you!



Codesandbox


More From » reactjs

 Answers
4

I was able to solve this on your Codesandbox by removing the {...rest} props spread you were applying in the SimpleInput component. This added some funky attributes that seemed to be interfering with the standard form field behavior.



You can see the proper submit-on-enter behavior here: https://codesandbox.io/s/dark-star-r0liq



In my own code, I am using a standard HTML <form> instead of the Formik <Form>, so when I ran into this issue, I had to ensure my submit button had both type=submit attribute, as well as the onClick handler hooked up to Formik's handleSubmit method. Then the submit-on-enter behavior started working.



<button
onClick={formProps.handleSubmit}
type=submit
/>



Related official repo issue: https://github.com/jaredpalmer/formik/issues/1418


[#52718] Monday, January 21, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;