Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnChange handler is not working. #1385

Closed
kaifresh opened this issue Mar 16, 2019 · 5 comments
Closed

OnChange handler is not working. #1385

kaifresh opened this issue Mar 16, 2019 · 5 comments

Comments

@kaifresh
Copy link

kaifresh commented Mar 16, 2019

馃悰 Bug report

Current Behavior

I have seen examples of onChange handlers attached to Formik <Field /> components, which appear to fire on input changes

In keeping with these examples, I have added an onChange handler to a <Field /> component. The problem is that this handler function is not being called when the child input changes (it is a <select>).

Expected behavior

When a new option on the <select> is chosen, the handler should fiire

Reproducible example

<Field name={id} onChange={(e: any)=>console.log(e)}>

Suggested solution(s)

Explain how to correctly supply an onChange handler to a Formik form.

Your environment

Software Version(s)
Formik "formik": "^1.5.1",
React "react": "^16.4.2",
TypeScript
Browser chrome 72.0.3626.121
npm/Yarn yarn 1.13.0
Operating System mac os 10.14
@jelling
Copy link

jelling commented Sep 16, 2019

onChange is working for me when the onChange handler is in the scope of the Form object but not when in it is passed in as a property. However, handleSubmit is passed in as a property and it works.

const MyForm = props => {
  const { values, touched, errors, handleChange, handleBlur, handleSubmit } = props;

  const localOnChange = () => console.log('local onchange called'); // this works

  return (
    <form onSubmit={handleSubmit} > // handleSubmit is passed in via props and works
      <input type="text" onChange={localOnChange} onBlur={handleBlur} value={values.school} name="school" />
      <input type="text" onChange={handleChange} onBlur={handleBlur} value={values.degreeType} name="degreeType" />
      <button type="submit">Submit</button>
    </form>
  );
};

const FooWithFormik = props => {
  const validate = values => {
    const errors = {};

    if (!values.school) {
      errors.name = 'Required';
    }

    return errors;
  };

  return props.makeForm(validate, MyForm);
};

export default FooWithFormik;

@jelling
Copy link

jelling commented Sep 16, 2019

#1817

@timurcatakli
Copy link

Can you try this?

    <Component
      onChange={event => field.onChange(field.name)(event)}
      minEditorHeight={100}
      maxEditorHeight={100}
    />

@Elmar101
Copy link

<Formik initialValues={{
title: data?.title ,
description: data?.description,
price: data?.price ,
image: data?.image
}}
validationSchema = {validationSchema}
onSubmit={handleSubmit}
>
{
({handleSubmit, errors , touched, handleChange , handleBlur, values, isSubmitting,setFieldValue}: FormikProps)=>{
return (
<>



Title
<Input
title="title"
type="text"
onChange={(e:React.ChangeEvent)=> setFieldValue("title",e.target.value)}
onBlur={handleBlur}
value={values.title}
disabled={isSubmitting}
isInvalid = {touched.title && !!errors.title}
/>
{touched.title && errors.title && {errors.title} }

                            <FormControl mt={4}>
                                <FormLabel> Description </FormLabel>
                                <Textarea
                                    title="description"
                                    name="description"
                                    value={values.description}
                                    onChange={(e)=>setFieldValue("description",e.target.value)}
                                    onBlur={handleBlur}
                                    disabled={isSubmitting}
                                />

@theboyofdream
Copy link

When dealing with dates setValues worked for me.

<DateTimePickerInput
                  label="End Date"
                  value={values.endDate}
                  minDate={values.startDate}
                  mode={'date'}
                  onChange={date => {
                    setValues({...values, endDate: date});
                    handleChange('endDate');
                  }}
                  style={{flex: 1}}
                  errorText={errors.endDate && `${format(errors.endDate as Date, 'dd/MM/yyyy')} Invalid`}
                />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants