Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md (setup and step-by-step recipes) and a README in each main folder, with an index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to JSDoc @file blocks, define a three-level header template in website/AGENTS.md and check the header in tests/file-headers.test.mjs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
255 lines
11 KiB
JavaScript
255 lines
11 KiB
JavaScript
/**
|
|
* @file Rend les sections et items de checklist avec édition inline et quantités.
|
|
*/
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { CategoryAutocompleteInput } from "../../../components/CategoryAutocompleteInput.jsx";
|
|
import { Icon } from "../../../components/Icon.jsx";
|
|
import { useInlineEdit } from "../../../hooks/useInlineEdit.js";
|
|
import {
|
|
getSectionHideWhenComplete,
|
|
isSectionComplete,
|
|
keepChecklistSection
|
|
} from "./checklistUtils.js";
|
|
|
|
export function ChecklistSection({ section, context, data, reorder, onRenameSection, onUpdateSection, onSaveSections, onUpdateItem, onMoveItemToCategory, onDeleteItem, categoryOptions = [] }) {
|
|
const showTitle = Boolean(section.title);
|
|
const sectionComplete = showTitle && isSectionComplete(section, context);
|
|
const hideWhenComplete = getSectionHideWhenComplete(section, data.hideCompletedSections);
|
|
const isAutoCollapsed = sectionComplete && hideWhenComplete;
|
|
const isCollapsed = Boolean(section.collapsed) || isAutoCollapsed;
|
|
const visibleItems = data.hideCompletedItems
|
|
? section.items.filter((item) => context.clampQty(item.qtyCurrent, item.qtyTarget) < item.qtyTarget)
|
|
: section.items;
|
|
const textContent = context.moduleText?.checklist || {};
|
|
const className = [
|
|
"checklist-section",
|
|
showTitle ? "is-grouped" : "",
|
|
sectionComplete ? "is-complete" : "",
|
|
isCollapsed ? "is-collapsed" : "",
|
|
reorder.isGroupDragging(section.id) ? "is-dragging" : "",
|
|
reorder.isGroupDropTarget(section.id) ? "is-drop-target" : "",
|
|
reorder.getDropPlacement("group", section.id) === "after" ? "drop-after" : ""
|
|
].filter(Boolean).join(" ");
|
|
const titleEdit = useInlineEdit({
|
|
value: section.title,
|
|
onCommit: (title) => onRenameSection(section.id, title)
|
|
});
|
|
const [editingItemId, setEditingItemId] = useState("");
|
|
|
|
if (sectionComplete && data.hideCompletedSectionsFully) return null;
|
|
|
|
function saveItems(items) {
|
|
onUpdateSection(section.id, (entry) => ({ ...entry, items }));
|
|
}
|
|
|
|
function toggleSectionCollapsed() {
|
|
const nextCollapsed = !isCollapsed;
|
|
const nextSections = data.sections.map((entry) => {
|
|
if (entry.id === section.id) {
|
|
const nextSection = { ...entry };
|
|
if (nextCollapsed) {
|
|
if (sectionComplete) {
|
|
nextSection.hideWhenComplete = true;
|
|
delete nextSection.collapsed;
|
|
} else {
|
|
nextSection.collapsed = true;
|
|
}
|
|
} else {
|
|
delete nextSection.collapsed;
|
|
if (sectionComplete) nextSection.hideWhenComplete = false;
|
|
}
|
|
return nextSection;
|
|
}
|
|
if (data.hideCompletedSections && !nextCollapsed && entry.title && isSectionComplete(entry, context)) return { ...entry, hideWhenComplete: true };
|
|
return entry;
|
|
});
|
|
const nextCompletedSections = nextSections.filter((entry) => entry.title && isSectionComplete(entry, context));
|
|
const allHidden = nextCompletedSections.length > 0 && nextCompletedSections.every((entry) => getSectionHideWhenComplete(entry, false));
|
|
onSaveSections(nextSections.filter(keepChecklistSection), { hideCompletedSections: allHidden });
|
|
}
|
|
|
|
return (
|
|
<li className={className} {...reorder.getGroupProps({ groupId: section.id })}>
|
|
{showTitle && (
|
|
<div className="checklist-section-header">
|
|
<button
|
|
className="checklist-drag-handle nds-icon-button"
|
|
data-size="xs"
|
|
data-variant="secondary"
|
|
type="button"
|
|
onPointerDown={(event) => reorder.groupReorder.startDrag(event, { groupId: section.id })}
|
|
aria-label={`${textContent.reorderSectionTitle || "Déplacer la catégorie"} ${section.title}`}
|
|
title={textContent.reorderTitle || "Déplacer"}
|
|
>
|
|
<Icon name="drag" />
|
|
</button>
|
|
<h3>
|
|
<input
|
|
{...titleEdit.getInputProps({
|
|
className: "checklist-section-title-input",
|
|
type: "text",
|
|
title: textContent.sectionEditTitle || "Renommer la catégorie",
|
|
"aria-label": textContent.sectionEditTitle || "Renommer la catégorie"
|
|
})}
|
|
/>
|
|
</h3>
|
|
<div className="checklist-section-actions">
|
|
<span>{section.items.filter((item) => context.clampQty(item.qtyCurrent, item.qtyTarget) >= item.qtyTarget).length} / {section.items.length}</span>
|
|
<button
|
|
className="checklist-section-collapse-button nds-icon-button"
|
|
data-size="xs"
|
|
type="button"
|
|
onClick={toggleSectionCollapsed}
|
|
aria-expanded={!isCollapsed}
|
|
title={isCollapsed ? textContent.showCompletedSectionTitle || "Afficher cette catégorie" : textContent.hideCompletedSectionTitle || "Réduire cette catégorie"}
|
|
aria-label={isCollapsed ? textContent.showCompletedSectionTitle || "Afficher cette catégorie" : textContent.hideCompletedSectionTitle || "Réduire cette catégorie"}
|
|
>
|
|
<Icon name={isCollapsed ? "chevron-down" : "chevron-up"} />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
{!isCollapsed && (
|
|
<ul className="checklist-section-items">
|
|
{visibleItems.map((item) => (
|
|
<ChecklistItem
|
|
key={item.id}
|
|
item={item}
|
|
section={section}
|
|
context={context}
|
|
reorder={reorder}
|
|
items={section.items}
|
|
save={saveItems}
|
|
onUpdateItem={onUpdateItem}
|
|
onMoveItemToCategory={onMoveItemToCategory}
|
|
onDeleteItem={onDeleteItem}
|
|
categoryOptions={categoryOptions}
|
|
editing={editingItemId === item.id}
|
|
onToggleEditing={() => setEditingItemId(editingItemId === item.id ? "" : item.id)}
|
|
onCloseEditing={() => setEditingItemId("")}
|
|
/>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</li>
|
|
);
|
|
}
|
|
|
|
function ChecklistItem({ item, section, context, reorder, items, save, onUpdateItem, onMoveItemToCategory, onDeleteItem, categoryOptions, editing, onToggleEditing, onCloseEditing }) {
|
|
const itemRef = useRef(null);
|
|
const done = context.clampQty(item.qtyCurrent, item.qtyTarget) >= item.qtyTarget;
|
|
const textContent = context.moduleText?.checklist || {};
|
|
const className = [
|
|
"checklist-item",
|
|
done ? "is-complete" : "",
|
|
reorder.isItemDragging(item.id) ? "is-dragging" : "",
|
|
reorder.isItemDropTarget(item.id) ? "is-drop-target" : "",
|
|
reorder.getDropPlacement("item", item.id) === "after" ? "drop-after" : ""
|
|
].filter(Boolean).join(" ");
|
|
const labelEdit = useInlineEdit({
|
|
value: item.label,
|
|
transform: (draft) => String(draft || "").trim() || item.label,
|
|
onCommit: (label) => onUpdateItem(item.id, (entry) => ({ ...entry, label })),
|
|
autoFocus: editing,
|
|
focusKey: item.id
|
|
});
|
|
const categoryEdit = useInlineEdit({
|
|
value: section.title,
|
|
onCommit: (title) => onMoveItemToCategory(item.id, title)
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!editing) return undefined;
|
|
function handleOutsidePointerDown(event) {
|
|
if (!itemRef.current?.contains(event.target)) onCloseEditing();
|
|
}
|
|
document.addEventListener("pointerdown", handleOutsidePointerDown);
|
|
return () => document.removeEventListener("pointerdown", handleOutsidePointerDown);
|
|
}, [editing, onCloseEditing]);
|
|
|
|
function updateItem(updater) {
|
|
save(items.map((entry) => entry.id === item.id ? updater(entry) : entry));
|
|
}
|
|
|
|
function toggleChecked() {
|
|
updateItem((entry) => ({
|
|
...entry,
|
|
qtyCurrent: done ? 0 : entry.qtyTarget
|
|
}));
|
|
}
|
|
|
|
return (
|
|
<li ref={itemRef} className={className} {...reorder.getItemProps({ itemId: item.id, groupId: section.id })}>
|
|
<div className={`checklist-item-main ${editing ? "is-editing" : ""}`}>
|
|
<button
|
|
className="checklist-drag-handle nds-icon-button"
|
|
data-size="xs"
|
|
data-variant="secondary"
|
|
type="button"
|
|
onPointerDown={(event) => reorder.itemReorder.startDrag(event, item.id)}
|
|
aria-label={`${textContent.reorderTitle || "Déplacer"} ${item.label}`}
|
|
title={textContent.reorderTitle || "Déplacer"}
|
|
>
|
|
<Icon name="drag" />
|
|
</button>
|
|
{item.qtyTarget === 1 ? (
|
|
<input
|
|
className="nds-checkbox"
|
|
data-size="sm"
|
|
type="checkbox"
|
|
checked={done}
|
|
onChange={(event) => updateItem((entry) => ({ ...entry, qtyTarget: 1, qtyCurrent: event.target.checked ? 1 : 0 }))}
|
|
aria-label={`Terminer ${item.label}`}
|
|
/>
|
|
) : (
|
|
<div className="checklist-qty-controls nds-quantity-stepper" data-size="md" aria-label={`Quantité ${item.label}`}>
|
|
<button type="button" onClick={() => updateItem((entry) => ({ ...entry, qtyCurrent: context.clampQty(entry.qtyCurrent - 1, entry.qtyTarget) }))} aria-label={textContent.decrementLabel || "Retirer une quantité"}>-</button>
|
|
<label className="checklist-qty-current">
|
|
<input
|
|
type="number"
|
|
className="nds-quantity-stepper-input"
|
|
max={item.qtyTarget}
|
|
value={context.clampQty(item.qtyCurrent, item.qtyTarget)}
|
|
style={{ "--qty-current-digits": String(context.clampQty(item.qtyCurrent, item.qtyTarget)).length }}
|
|
onChange={(event) => updateItem((entry) => ({ ...entry, qtyCurrent: context.clampQty(event.target.value, entry.qtyTarget) }))}
|
|
aria-label={`${textContent.currentQuantityLabel || "Quantité actuelle"} ${item.label}`}
|
|
/>
|
|
<span>/ {item.qtyTarget}</span>
|
|
</label>
|
|
<button type="button" onClick={() => updateItem((entry) => ({ ...entry, qtyCurrent: context.clampQty(entry.qtyCurrent + 1, entry.qtyTarget) }))} aria-label={textContent.incrementLabel || "Ajouter une quantité"}>+</button>
|
|
</div>
|
|
)}
|
|
{editing ? (
|
|
<>
|
|
<input
|
|
{...labelEdit.getInputProps({
|
|
className: "checklist-label-input nds-inline-input",
|
|
type: "text",
|
|
"aria-label": textContent.itemEditLabel || "Libellé"
|
|
})}
|
|
/>
|
|
<CategoryAutocompleteInput
|
|
{...categoryEdit.getInputProps({
|
|
className: "checklist-category-input nds-inline-input",
|
|
placeholder: textContent.sectionPlaceholder || "Catégorie",
|
|
"aria-label": textContent.categoryEditLabel || "Catégorie"
|
|
})}
|
|
options={categoryOptions}
|
|
/>
|
|
</>
|
|
) : (
|
|
<button className="checklist-item-summary" type="button" onClick={toggleChecked} title={item.label}>
|
|
<span>{item.label}</span>
|
|
</button>
|
|
)}
|
|
</div>
|
|
<button type="button" className="checklist-edit-button nds-icon-button" data-size="sm" onClick={onToggleEditing} aria-pressed={editing} aria-label={`${textContent.editTitle || "Modifier"} ${item.label}`} title={textContent.editTitle || "Modifier"}>
|
|
<Icon name="edit" />
|
|
</button>
|
|
<button className="checklist-delete-button nds-icon-button" data-size="sm" data-variant="danger" onClick={() => onDeleteItem(item.id)} aria-label={`${textContent.deleteTitle || "Supprimer"} ${item.label}`} title={textContent.deleteTitle || "Supprimer"}>
|
|
<Icon name="trash" />
|
|
</button>
|
|
</li>
|
|
);
|
|
}
|