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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field type="checkbox", recommended way? #602

Closed
gabor opened this issue Apr 26, 2018 · 7 comments
Closed

Field type="checkbox", recommended way? #602

gabor opened this issue Apr 26, 2018 · 7 comments

Comments

@gabor
Copy link

gabor commented Apr 26, 2018

Bug, Feature, or Question?

Question

the question

when using a <Field component="input" type="checkbox"> component to represent a checkbox, it seems i have to manually maintain the checked attribute, like this:

<Field name="f1" component="input" type="checkbox" checked={props.values.f1} />

is this the recommended approach?
i am just unsure, because the value attribute is maintained automatically, unlike checked.

@Andreyco
Copy link
Collaborator

Depends on your use case, of course...
This is my approach https://codesandbox.io/s/328038x19q

@c10b10
Copy link

c10b10 commented Apr 5, 2019

What about using a FieldArray for this purpose?

@huylift
Copy link

huylift commented Jun 18, 2019

Can anyone give a simpler answer to the original question?

@Andreyco 's example is more complex: it supports an array of checkboxes.

@ameenaliu
Copy link

I believed it is maintained automatically like value. I tried with a simple use case like @Andreyco and it works; here - https://codesandbox.io/s/sleepy-shannon-lpe81.

@mikemclin
Copy link

It is (mostly) maintained automatically. You still have to manage the checked state from the initial values. Here is a fork of @ameenaliu code sandbox: https://codesandbox.io/s/ecstatic-monad-58kjq

@dennisat
Copy link

I couldn't believe that the checkbox would be so hard working with Formik.

Anyway, I found a very simple way to work with it.

This is the MUI checkbox but I think with will work for native input checkbox as well.

                    <Checkbox
                      name="autoLogin"
                      value="true"
                      checked={autoLogin}
                    />

The autoLogin is a boolean variable.
The value surprisingly should be hardcoded as a string "true".

@pelupotter
Copy link

Hello, I leave you an example of Checkbox with SUIR, greetings!

<Field name='external' label='Client is External?' component={Checkbox} checked={values.external} onChange={() => setFieldValue('external', !values.external)} />

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

9 participants