Użyj Formik wyboru 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>
);
};
Mystic Dev