1

I am trying to push an checkbox Object in Formik, but it is not working. I have taken reference from https://codesandbox.io/s/formik-example-forked-owodd?file=/index.js

but I want to push object inside initialValue.

Below is my way of doing it

{userManagement.map((permissions) => (
                              <li className="col-sm-3 col-xs-6" key={permissions.task}>
                                <label className="checkbox">
                                  <input
                                    name="permission"
                                    type="checkbox"
                                    value={permissions}
                                    onBlur={(e) => {
                                      if (e.currentTarget.checked) {
                                        push({ permission: permissions });
                                        console.log(values.permission);
                                      } else {
                                        const idx = values.permission.indexOf(permissions.value);
                                        remove(idx);
                                      }
                                    }}
                                  />
                                  <span />
                                  {permissions.label}
                                </label>
                              </li>
                            ))}

1 Answer 1

1

My solution towards this is:

                      <FieldArray
                        name="permission"
                        render={({ push, remove }) => (
                          <ul className="sub-rights-area row">
                            {permissions.map((perm) => (
                              <li className="col-sm-3 col-xs-6" key={perm.task}>
                                <label className="checkbox">
                                  <input
                                    name="permission"
                                    type="checkbox"
                                    value={perm.task}
                                    checked={values.permission.map((e) => e.task).includes(perm.task)}
                                    onChange={(e) => {
                                      if (e.target.checked) {
                                        push({ status: perm.status, task: perm.task });
                                        console.log(values.permission);
                                      } else {
                                        const index = values.permission
                                          .map(function (e) {
                                            return e.task;
                                          })
                                          .indexOf(perm.task);
                                        remove(index);
                                      }
                                    }}
                                  />
                                  <span />
                                  {perm.name}
                                </label>
                              </li>
                            ))}
                          </ul>
                        )}
                      />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.