sokko-g/website/src/features/toolboxes/modules/TableGridParts.jsx

93 lines
4.1 KiB
JavaScript

// Rôle : rend les en-têtes et lignes de cellules du module Table.
import { useInlineEdit } from "../../../hooks/useInlineEdit.js";
import { cellAddress } from "./tableFormulaEngine.js";
import { isCellInBounds, isCellSelected } from "./tableUtils.js";
export function TableRow({ rowIndex, columns, cells, evaluatedCells, editingCell, highlightedCell, textContent, rowLabel, selection, movePreview, onRowLabelChange, onFocus, onBlur, onCellChange, onFormulaSelectionStart, onFormulaSelectionExtend, onStartMoveSelection, onMoveSelection, onSelectCell, onExtendSelection, onPasteSelection, draggingSelectionRef, onCellKeyDown, onStartEditing, registerInput }) {
return (
<>
<HeaderLabelInput
className="table-row-header table-header-input"
value={rowLabel || String(rowIndex + 1)}
fallback={String(rowIndex + 1)}
onSave={(value) => onRowLabelChange(rowIndex, value)}
aria-label={`${textContent.rowLabel || "Intitulé de ligne"} ${rowIndex + 1}`}
title={`${rowIndex + 1} - ${textContent.rowFormulaTitle || "Référence conservée dans les formules"}`}
/>
{Array.from({ length: columns }, (_, columnIndex) => {
const address = cellAddress(rowIndex, columnIndex);
const rawValue = cells[address] || "";
const evaluated = evaluatedCells[address];
const isEditing = editingCell === address;
const isFormula = rawValue.startsWith("=");
const displayValue = isEditing ? rawValue : evaluated?.display || rawValue;
const className = [
"table-cell-input",
isEditing ? "is-editing" : "",
isFormula ? "is-formula" : "",
evaluated?.error && evaluated.error !== "empty" && !isEditing ? "has-error" : "",
isCellSelected(selection, rowIndex, columnIndex) ? "is-selected" : "",
isCellInBounds(movePreview, rowIndex, columnIndex) ? "is-move-target" : "",
highlightedCell === address ? "is-referenced" : ""
].filter(Boolean).join(" ");
return (
<input
key={address}
ref={(element) => registerInput(address, element)}
className={className}
value={displayValue}
onMouseDown={(event) => {
if (onFormulaSelectionStart(rowIndex, columnIndex)) {
event.preventDefault();
} else if (!event.shiftKey && onStartMoveSelection(rowIndex, columnIndex)) {
event.preventDefault();
} else {
draggingSelectionRef.current = true;
onSelectCell(rowIndex, columnIndex, event.shiftKey);
}
}}
onDoubleClick={() => onStartEditing(address, null, { select: true })}
onMouseEnter={() => {
if (onFormulaSelectionExtend(rowIndex, columnIndex)) return;
if (onMoveSelection(rowIndex, columnIndex)) return;
if (draggingSelectionRef.current) onExtendSelection(rowIndex, columnIndex);
}}
onFocus={() => onFocus(address)}
onBlur={() => onBlur(address)}
onChange={(event) => onCellChange(address, event.target.value)}
onKeyDown={(event) => onCellKeyDown(event, rowIndex, columnIndex, address, isEditing)}
onPaste={(event) => onPasteSelection(event, rowIndex, columnIndex, isEditing)}
aria-label={`${textContent.cellLabel || "Cellule"} ${address}`}
title={evaluated?.error && evaluated.error !== "empty" ? textContent.errorTitle || "Formule invalide" : address}
inputMode="text"
readOnly={!isEditing}
spellCheck="false"
/>
);
})}
</>
);
}
export function HeaderLabelInput({ className, value, fallback, onSave, ...props }) {
const inlineEdit = useInlineEdit({
value,
onCommit: onSave,
transform: (draft) => String(draft || ""),
blurOnEscape: true
});
return (
<input
{...inlineEdit.getInputProps({
...props,
className,
onFocus: (event) => {
if (value === fallback) window.requestAnimationFrame(() => event.target.select());
},
spellCheck: "false"
})}
/>
);
}