import { useState } from "react"; import type { WidgetProps } from "./types"; import { ReservedControls } from "./ReservedControls"; export function MultiselectWidget({ descriptor, onRespond }: WidgetProps) { const options = descriptor.options ?? []; const initialChecked = new Set(options.filter((o) => o.selected).map((o) => o.id)); const [checked, setChecked] = useState>(initialChecked); const allChecked = options.length > 0 && options.every((o) => checked.has(o.id)); function toggle(id: string) { setChecked((prev) => { const next = new Set(prev); if (next.has(id)) next.delete(id); else next.add(id); return next; }); } const isDisabled = descriptor.allow_empty === false && checked.size === 0; return (
{descriptor.title && (

{descriptor.title}

)}
{/* Prominent select-all control at the head of the list. */}
{checked.size} / {options.length} {descriptor.selection_label ?? "selected"}
{options.map((o) => ( ))}
onRespond({ id: descriptor.id, control: c, ...(t !== undefined ? { text: t } : {}) })} />
); }