Open this example in the interactive library
Live preview
Source code
// components/MultiSelectSummary.tsx
import { useState } from "react";
export default function MultiSelectSummary({
options = ["Clay", "Slate", "Ink"],
}: { options?: string[] } = {}) {
const [selected, set] = useState<string[]>([]);
return (
<fieldset>
<legend>Materials</legend>
{options.map((v) => (
<label key={v}>
<input
type="checkbox"
checked={selected.includes(v)}
onChange={(e) =>
set((old) =>
e.target.checked ? [...old, v] : old.filter((x) => x !== v),
)
}
/>
{v}
</label>
))}
<p>{selected.join(", ") || "None selected"}</p>
</fieldset>
);
}
