import { useState } from "react";
export function ChecklistModule({ toolboxId, moduleId, context, editing }) {
const data = context.normalizeChecklistData(context.getModuleData(toolboxId, moduleId, { items: [] }));
const [label, setLabel] = useState("");
const [qty, setQty] = useState(1);
function save(items) {
context.setModuleData(toolboxId, moduleId, { items });
}
function addItem(event) {
event.preventDefault();
const cleanLabel = label.trim();
if (!cleanLabel) return;
save([...data.items, { id: context.uid("item"), label: cleanLabel, qtyTarget: Math.max(1, Number(qty) || 1), qtyCurrent: 0 }]);
setLabel("");
setQty(1);
}
return (
<>
{editing && (
)}
{data.items.map((item) => (
))}
>
);
}
function ChecklistItem({ item, context, items, save }) {
const done = context.clampQty(item.qtyCurrent, item.qtyTarget) >= item.qtyTarget;
function updateItem(updater) {
save(items.map((entry) => entry.id === item.id ? updater(entry) : entry));
}
return (
{item.qtyTarget === 1 ? (
updateItem((entry) => ({ ...entry, qtyTarget: 1, qtyCurrent: event.target.checked ? 1 : 0 }))}
aria-label={`Terminer ${item.label}`}
/>
) : (
{context.clampQty(item.qtyCurrent, item.qtyTarget)}/{item.qtyTarget}
)}
{item.label}
);
}