Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

useformik checkbox multiselect

import React from "react";
import { useFormik } from "formik";

export default function App() {
  return <ProjectsForm></ProjectsForm>;
}

const tags = ["one", "two", "three"];

const ProjectsForm = () => {
  const formik = useFormik({
    enableReinitialize: true,
    initialValues: {
      tags: []
    },
    onSubmit: (values) => {
      console.log(values);
    }
  });

  const handleChange = (e) => {
    const { checked, name } = e.target;
    if (checked) {
      formik.setFieldValue("tags", [...formik.values.tags, name]);
    } else {
      formik.setFieldValue(
        "tags",
        formik.values.tags.filter((v) => v !== name)
      );
    }
  };

  return (
    <form onSubmit={formik.handleSubmit}>
      {tags.map((tag) => (
        <div key={tag}>
          <input
            id={tag}
            type="checkbox"
            name={tag}
            checked={formik.values.tags.includes(tag)}
            onChange={handleChange}
          />
          <label htmlFor={tag}>{tag}</label>
        </div>
      ))}
      <button type="submit">Submit</button>
    </form>
  );
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: find element that has certain text javascript 
Javascript :: ex: splide carousel 
Javascript :: can be converted to number js 
Javascript :: vue for start at index 
Javascript :: notify jquery 
Javascript :: export mongo to csv node 
Javascript :: slice string javascript if has @ 
Javascript :: javascript add fields dynamically 
Javascript :: javaScript getMonth() Method 
Javascript :: length of array 
Javascript :: react useeffect hooks 
Javascript :: Split string on the first white space occurrence 
Javascript :: js event handlers 
Javascript :: closure 
Javascript :: Show Modal Dialog on Jetpack Compose 
Javascript :: sum array elements in javascript 
Javascript :: insert a string in another js 
Javascript :: convert json to 2d array 
Javascript :: javascript addeventlistener 
Javascript :: jquery alertify 
Javascript :: click binding angular 8 
Javascript :: electron . not working 
Javascript :: how to install react fullpage using npm 
Javascript :: convert Component Did mount into useEffect 
Javascript :: process.env type 
Javascript :: currency conversion to locale string js 
Javascript :: prisma decimal 
Javascript :: binary sort js 
Javascript :: get all objects from s3 bucket nodejs 
Javascript :: convert text to qr code in angular 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =