Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically show hide Formik form field depending on another form field

Tags:

reactjs

formik

I have a form written in Formik. I need to show/hide a 'text' field depending on the value (option) selected of the 'select' field. How can I achieve this?

<Formik
    initialValues={{transactionCategory, name}}
    onSubmit={this.onSubmit}
    validateOnChange={false}
    validateOnBlur={false}
    validate={this.validate}
    enableReinitialize={true}
>
    {(props) => (
        <Form>

            <fieldset className="form-group">
                <label>Category</label>
                <Field name="transactionCategory" component="select">
                    <option value="Admission">Admission</option>
                    <option value="Class_Fee">Class Fee</option>
                    <option value="Staff_Payment">Staff Payment</option>
                    <option value="Other">Other</option>
                </Field>
            </fieldset>

            <fieldset className="form-group">
                <label>Name</label>
                <Field className="form-control" type="text" name="name"/>
            </fieldset>

            <button className="btn btn-success" type="submit">Save</button>
            &nbsp;
            <button type="reset" className="btn btn-secondary">Reset</button>
        </Form>
    )}
</Formik>

When I select the value 'Other' from the 'Category' options, the 'Name' field should be hidden.

like image 472
Dushan Avatar asked Oct 16 '19 10:10

Dushan


People also ask

Can Formik be nested?

The name props in Formik can use lodash-like dot paths to reference nested Formik values. This means that you do not need to flatten out your form's values anymore.

What does Formik handleChange do?

Using Formik's handleChange The handleChange method updates the form values based on the input's name attribute that was changed.


2 Answers

Conditionally render name field by using transactionCategory value

<Formik
    initialValues={{ transactionCategory, name }}
    onSubmit={this.onSubmit}
    validateOnChange={false}
    validateOnBlur={false}
    validate={this.validate}
    enableReinitialize={true}
>
    {props => (
        <Form>
            <fieldset className="form-group">
                <label>Category</label>
                <Field name="transactionCategory" component="select">
                    <option value="Admission">Admission</option>
                    <option value="Class_Fee">Class Fee</option>
                    <option value="Staff_Payment">Staff Payment</option>
                    <option value="Other">Other</option>
                </Field>
            </fieldset>

            {props.values.transactionCategory !== "Other" && (
                <fieldset className="form-group">
                    <label>Name</label>
                    <Field className="form-control" type="text" name="name" />
                </fieldset>
            )}

            <button className="btn btn-success" type="submit">
                Save
            </button>
            &nbsp;
            <button type="reset" className="btn btn-secondary">
                Reset
            </button>
        </Form>
    )}
</Formik>;
like image 171
wkgalla Avatar answered Sep 18 '22 13:09

wkgalla


write onSelect/onchange event of option and based on the Option's selected value. Update the state field.

this.setState({isName:true})

now you can use this.state.isName in condition rendering to show and hide the field.

like image 41
Harsh Mishra Avatar answered Sep 16 '22 13:09

Harsh Mishra