diff --git a/components/admin/extra-sheet-editor.tsx b/components/admin/extra-sheet-editor.tsx index ce76d98..4acecc6 100644 --- a/components/admin/extra-sheet-editor.tsx +++ b/components/admin/extra-sheet-editor.tsx @@ -12,7 +12,7 @@ import { Rows3Icon, Trash2Icon, } from "lucide-react"; -import Spreadsheet, { type ColumnIndicatorProps, type DataEditorProps, type Point, type Selection } from "react-spreadsheet"; +import Spreadsheet, { type ColumnIndicatorProps, type DataEditorProps, type Point } from "react-spreadsheet"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { DeleteConfirmationDialog } from "@/components/admin/delete-confirmation-dialog"; @@ -125,8 +125,8 @@ export function ExtraSheetEditor({ const [removeOpen, setRemoveOpen] = useState(false); const [cellDialogOpen, setCellDialogOpen] = useState(false); const [cellDraft, setCellDraft] = useState(""); - const [selection, setSelection] = useState(null); - const colorSelectionRef = useRef(null); + const [selectedCount, setSelectedCount] = useState(1); + const selectedPointsRef = useRef([active]); const matrix = useMemo(() => extraSheetToMatrix(value), [value]); const activeColumn = value.columns[active.column]; const activeRow = value.rows[active.row]; @@ -135,12 +135,6 @@ export function ExtraSheetEditor({ index !== (renaming ? active.column : -1) && column.label.toLocaleLowerCase() === normalizedLabel, ); - const selectedPoints = useCallback(() => { - const range = selection?.toRange(matrix); - return range ? Array.from(range) : [active]; - }, [active, matrix, selection]); - const selectedCount = selectedPoints().length; - const openAddColumn = () => { setRenaming(false); setColumnLabel(nextExtraSheetColumnLabel(value.columns)); @@ -225,16 +219,12 @@ export function ExtraSheetEditor({ aria-label="เลือกสีพื้นหลังเซลล์" className="absolute inset-0 cursor-pointer opacity-0" value={activeRow?.backgrounds?.[activeColumn?.id ?? ""] ?? DEFAULT_CELL_BACKGROUND} - onPointerDown={() => { - colorSelectionRef.current = selectedPoints(); - }} onChange={(event) => { onChange(updateExtraSheetCells( value, - colorSelectionRef.current ?? selectedPoints(), + selectedPointsRef.current, { backgroundColor: event.target.value }, )); - colorSelectionRef.current = null; }} /> @@ -242,7 +232,7 @@ export function ExtraSheetEditor({ type="button" variant="ghost" size="sm" - onClick={() => onChange(updateExtraSheetCells(value, selectedPoints(), { backgroundColor: null }))} + onClick={() => onChange(updateExtraSheetCells(value, selectedPointsRef.current, { backgroundColor: null }))} > ล้างสีพื้นหลัง @@ -343,8 +333,18 @@ export function ExtraSheetEditor({ ColumnIndicator={columnIndicator} DataViewer={LiteralDataViewer} DataEditor={dataEditor} - onActivate={setActive} - onSelect={setSelection} + onActivate={(point) => { + setActive(point); + setSelectedCount(1); + selectedPointsRef.current = [point]; + }} + onSelect={(nextSelection) => { + const range = nextSelection.toRange(matrix); + if (range) { + selectedPointsRef.current = Array.from(range); + setSelectedCount(range.size()); + } + }} onChange={(next) => onChange(matrixToExtraSheet( value, next as ExtraSheetMatrix, @@ -367,7 +367,7 @@ export function ExtraSheetEditor({