grid-cell-selection 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -32,9 +32,9 @@ function App() {
32
32
  <tr key={row}>
33
33
  {columns.map((column, col) => (
34
34
  <td
35
- onMouseEnter={(event) => handleMouseEnter({ row, col }, event)}
36
- onMouseDown={(event) => handleMouseDown({ row, col }, event)}
37
- className={`${isCellSelected({ row, col }) ? "selected" : ""}`}
35
+ onMouseEnter={(event) => handleMouseEnter({ id: `${row}-${col}`, row, col }, event)}
36
+ onMouseDown={(event) => handleMouseDown({ id: `${row}-${col}`, row, col }, event)}
37
+ className={`${isCellSelected({ id: `${row}-${col}`, row, col }) ? "selected" : ""}`}
38
38
  >
39
39
  {column}
40
40
  {row}
@@ -63,3 +63,7 @@ function App() {
63
63
  ### Multiple Range Selection
64
64
 
65
65
  - Hold Ctrl (or Cmd on Mac) or Shift to select multiple ranges
66
+
67
+ ### ID Prop
68
+
69
+ If you provide an array of all cells, the hook will include the cell IDs when tracking the selected cells.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { CellIdentifier } from "../types";
3
- export declare const useGridCellSelection: () => {
3
+ export declare const useGridCellSelection: (allCells?: CellIdentifier[]) => {
4
4
  selectedCells: Map<string, CellIdentifier>;
5
5
  isCellSelected: (cell: CellIdentifier) => boolean;
6
6
  handleMouseDown: (cell: CellIdentifier, event: import("react").MouseEvent<Element, MouseEvent>) => void;
package/dist/index.esm.js CHANGED
@@ -55,7 +55,7 @@ var useMouseDragSelection = function (toggleCellSelection) {
55
55
  var getCellKey = function (cell) {
56
56
  return "".concat(cell.row, "-").concat(cell.col);
57
57
  };
58
- var useGridCellSelection = function () {
58
+ var useGridCellSelection = function (allCells) {
59
59
  var _a = useState({
60
60
  selectedCells: new Map(),
61
61
  }), selectionState = _a[0], setSelectionState = _a[1];
@@ -67,7 +67,7 @@ var useGridCellSelection = function () {
67
67
  var selectedCells = new Map(prevState.selectedCells);
68
68
  var cellKey = getCellKey(cell);
69
69
  if (prevState.startCell && !newSelection) {
70
- handleRangeSelection(selectedCells, prevState.startCell, cell);
70
+ handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);
71
71
  }
72
72
  else if ((shiftKey || ctrlKey) && newSelection) {
73
73
  handleSingleOrMultipleSelection(selectedCells, cellKey, cell);
@@ -97,7 +97,7 @@ var useGridCellSelection = function () {
97
97
  };
98
98
  };
99
99
  // Updated helper functions
100
- function handleRangeSelection(selectedCells, startCell, endCell) {
100
+ function handleRangeSelection(selectedCells, startCell, endCell, allCells) {
101
101
  var action = selectedCells.has(getCellKey(startCell)) ? "set" : "delete";
102
102
  var _a = [startCell.row, startCell.col], startRow = _a[0], startCol = _a[1];
103
103
  var _b = [endCell.row, endCell.col], endRow = _b[0], endCol = _b[1];
@@ -105,16 +105,28 @@ function handleRangeSelection(selectedCells, startCell, endCell) {
105
105
  var maxRow = Math.max(startRow, endRow);
106
106
  var minCol = Math.min(startCol, endCol);
107
107
  var maxCol = Math.max(startCol, endCol);
108
- for (var row = minRow; row <= maxRow; row++) {
109
- for (var col = minCol; col <= maxCol; col++) {
108
+ var _loop_1 = function (row) {
109
+ var _loop_2 = function (col) {
110
110
  var cellKey = getCellKey({ row: row, col: col });
111
111
  if (action === "set") {
112
- selectedCells.set(cellKey, { row: row, col: col });
112
+ if (allCells) {
113
+ var cell = allCells.find(function (c) { return c.row === row && c.col === col; });
114
+ cell && selectedCells.set(cellKey, cell);
115
+ }
116
+ else {
117
+ selectedCells.set(cellKey, { id: cellKey, row: row, col: col });
118
+ }
113
119
  }
114
120
  else {
115
121
  selectedCells.delete(cellKey);
116
122
  }
123
+ };
124
+ for (var col = minCol; col <= maxCol; col++) {
125
+ _loop_2(col);
117
126
  }
127
+ };
128
+ for (var row = minRow; row <= maxRow; row++) {
129
+ _loop_1(row);
118
130
  }
119
131
  }
120
132
  function handleSingleOrMultipleSelection(selectedCells, cellKey, cell) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useState } from \"react\";\nimport { CellIdentifier } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent) => {\n setIsDragging(true);\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent) => {\n if (isDragging) {\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n","import { useState } from \"react\";\nimport { CellIdentifier, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = () => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp } = useMouseDragSelection(toggleCellSelection);\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n selectedCells.set(cellKey, { row, col });\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier\n) {\n if (selectedCells.has(cellKey)) {\n selectedCells.delete(cellKey);\n } else {\n selectedCells.set(cellKey, cell);\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAM,qBAAqB,GAAG,UACnC,mBAA+G,EAAA;IAEzG,IAAA,EAAA,GAA8B,QAAQ,CAAC,KAAK,CAAC,EAA5C,UAAU,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,aAAa,GAAA,EAAA,CAAA,CAAA,CAAmB,CAAC;AAEpD,IAAA,IAAM,eAAe,GAAG,UAAC,IAAoB,EAAE,KAAuB,EAAA;QACpE,aAAa,CAAC,IAAI,CAAC,CAAC;AACpB,QAAA,mBAAmB,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ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,GAAG,EAAA,GAAA,EAAE,CAAC,CAAC;YACzC,IAAI,MAAM,KAAK,KAAK,EAAE;AACpB,gBAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,GAAG,EAAA,GAAA,EAAE,GAAG,EAAA,GAAA,EAAE,CAAC,CAAC;AAC1C,aAAA;AAAM,iBAAA;AACL,gBAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,aAAA;AACF,SAAA;AACF,KAAA;AACH,CAAC;AAED,SAAS,+BAA+B,CACtC,aAA0C,EAC1C,OAAe,EACf,IAAoB,EAAA;AAEpB,IAAA,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;AAC9B,QAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,KAAA;AAAM,SAAA;AACL,QAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAClC,KAAA;AACH;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useState } from \"react\";\nimport { CellIdentifier } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent) => {\n setIsDragging(true);\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent) => {\n if (isDragging) {\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n","import { useState } from \"react\";\nimport { CellIdentifier, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = (allCells?: CellIdentifier[]) => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp } = useMouseDragSelection(toggleCellSelection);\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier,\n allCells?: CellIdentifier[]\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n if (allCells) {\n const cell = allCells.find((c) => c.row === row && c.col === col);\n cell && selectedCells.set(cellKey, cell);\n } else {\n selectedCells.set(cellKey, { id: cellKey, row, col });\n }\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier\n) {\n if (selectedCells.has(cellKey)) {\n selectedCells.delete(cellKey);\n } else {\n selectedCells.set(cellKey, cell);\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAM,qBAAqB,GAAG,UACnC,mBAA+G,EAAA;IAEzG,IAAA,EAAA,GAA8B,QAAQ,CAAC,KAAK,CAAC,EAA5C,UAAU,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,aAAa,GAAA,EAAA,CAAA,CAAA,CAAmB,CAAC;AAEpD,IAAA,IAAM,eAAe,GAAG,UAAC,IAAoB,EAAE,KAAuB,EAAA;QACpE,aAAa,CAAC,IAAI,CAAC,CAAC;AACpB,QAAA,mBAAmB,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;AAClF,KAAC,CAAC;AAEF,IAAA,IAAM,gBAAgB,GAAG,UAAC,IAAoB,EAAE,KAAuB,EAAA;AACrE,QAAA,IAAI,UAAU,EAAE;AACd,YAAA,mBAAmB,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;AAClF,SAAA;AACH,KAAC,CAAC;AAEF,IAAA,IAAM,aAAa,GAAG,YAAA;QACpB,aAAa,CAAC,KAAK,CAAC,CAAC;AACvB,KAAC,CAAC;IAEF,OAAO;AACL,QAAA,eAAe,EAAA,eAAA;AACf,QAAA,gBAAgB,EAAA,gBA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;AACL,gBAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,aAAA;;QAXH,KAAK,IAAI,GAAG,GAAG,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,EAAE,EAAA;oBAAlC,GAAG,CAAA,CAAA;AAYX,SAAA;;IAbH,KAAK,IAAI,GAAG,GAAG,MAAM,EAAE,GAAG,IAAI,MAAM,EAAE,GAAG,EAAE,EAAA;gBAAlC,GAAG,CAAA,CAAA;AAcX,KAAA;AACH,CAAC;AAED,SAAS,+BAA+B,CACtC,aAA0C,EAC1C,OAAe,EACf,IAAoB,EAAA;AAEpB,IAAA,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;AAC9B,QAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,KAAA;AAAM,SAAA;AACL,QAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAClC,KAAA;AACH;;;;"}
package/dist/index.js CHANGED
@@ -59,7 +59,7 @@ var useMouseDragSelection = function (toggleCellSelection) {
59
59
  var getCellKey = function (cell) {
60
60
  return "".concat(cell.row, "-").concat(cell.col);
61
61
  };
62
- var useGridCellSelection = function () {
62
+ var useGridCellSelection = function (allCells) {
63
63
  var _a = react.useState({
64
64
  selectedCells: new Map(),
65
65
  }), selectionState = _a[0], setSelectionState = _a[1];
@@ -71,7 +71,7 @@ var useGridCellSelection = function () {
71
71
  var selectedCells = new Map(prevState.selectedCells);
72
72
  var cellKey = getCellKey(cell);
73
73
  if (prevState.startCell && !newSelection) {
74
- handleRangeSelection(selectedCells, prevState.startCell, cell);
74
+ handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);
75
75
  }
76
76
  else if ((shiftKey || ctrlKey) && newSelection) {
77
77
  handleSingleOrMultipleSelection(selectedCells, cellKey, cell);
@@ -101,7 +101,7 @@ var useGridCellSelection = function () {
101
101
  };
102
102
  };
103
103
  // Updated helper functions
104
- function handleRangeSelection(selectedCells, startCell, endCell) {
104
+ function handleRangeSelection(selectedCells, startCell, endCell, allCells) {
105
105
  var action = selectedCells.has(getCellKey(startCell)) ? "set" : "delete";
106
106
  var _a = [startCell.row, startCell.col], startRow = _a[0], startCol = _a[1];
107
107
  var _b = [endCell.row, endCell.col], endRow = _b[0], endCol = _b[1];
@@ -109,16 +109,28 @@ function handleRangeSelection(selectedCells, startCell, endCell) {
109
109
  var maxRow = Math.max(startRow, endRow);
110
110
  var minCol = Math.min(startCol, endCol);
111
111
  var maxCol = Math.max(startCol, endCol);
112
- for (var row = minRow; row <= maxRow; row++) {
113
- for (var col = minCol; col <= maxCol; col++) {
112
+ var _loop_1 = function (row) {
113
+ var _loop_2 = function (col) {
114
114
  var cellKey = getCellKey({ row: row, col: col });
115
115
  if (action === "set") {
116
- selectedCells.set(cellKey, { row: row, col: col });
116
+ if (allCells) {
117
+ var cell = allCells.find(function (c) { return c.row === row && c.col === col; });
118
+ cell && selectedCells.set(cellKey, cell);
119
+ }
120
+ else {
121
+ selectedCells.set(cellKey, { id: cellKey, row: row, col: col });
122
+ }
117
123
  }
118
124
  else {
119
125
  selectedCells.delete(cellKey);
120
126
  }
127
+ };
128
+ for (var col = minCol; col <= maxCol; col++) {
129
+ _loop_2(col);
121
130
  }
131
+ };
132
+ for (var row = minRow; row <= maxRow; row++) {
133
+ _loop_1(row);
122
134
  }
123
135
  }
124
136
  function handleSingleOrMultipleSelection(selectedCells, cellKey, cell) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useState } from \"react\";\nimport { CellIdentifier } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent) => {\n setIsDragging(true);\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent) => {\n if (isDragging) {\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n","import { useState } from \"react\";\nimport { CellIdentifier, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = () => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp } = useMouseDragSelection(toggleCellSelection);\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n selectedCells.set(cellKey, { row, col });\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier\n) {\n if (selectedCells.has(cellKey)) {\n selectedCells.delete(cellKey);\n } else {\n selectedCells.set(cellKey, cell);\n }\n}\n"],"names":["useState"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAM,qBAAqB,GAAG,UACnC,mBAA+G,EAAA;IAEzG,IAAA,EAAA,GAA8BA,cAAQ,CAAC,KAAK,CAAC,EAA5C,UAAU,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,aAAa,GAAA,EAAA,CAAA,CAAA,CAAmB,CAAC;AAEpD,IAAA,IAAM,eAAe,GAAG,UAAC,IAAoB,EAAE,KAAuB,EAAA;QACpE,aAAa,CAAC,IAAI,CAAC,CAAC;AACpB,QAAA,mBAAmB,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;AAClF,KAAC,CAAC;AAEF,IAAA,IAAM,gBAAgB,GAAG,UAAC,IAAoB,EAAE,KAAuB,EAAA;AACrE,QAAA,IAAI,UAAU,EAAE;AACd,YAAA,mBAAmB,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,QAA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MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,aAAA;AACF,SAAA;AACF,KAAA;AACH,CAAC;AAED,SAAS,+BAA+B,CACtC,aAA0C,EAC1C,OAAe,EACf,IAAoB,EAAA;AAEpB,IAAA,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;AAC9B,QAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,KAAA;AAAM,SAAA;AACL,QAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAClC,KAAA;AACH;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/hooks/useMouseDragSelection.ts","../src/hooks/useCellSelection.ts"],"sourcesContent":["import { useState } from \"react\";\nimport { CellIdentifier } from \"../types\";\n\nexport const useMouseDragSelection = (\n toggleCellSelection: (cell: CellIdentifier, ctrlKey: boolean, shiftKey: boolean, newSelection: boolean) => void\n) => {\n const [isDragging, setIsDragging] = useState(false);\n\n const handleMouseDown = (cell: CellIdentifier, event: React.MouseEvent) => {\n setIsDragging(true);\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, true);\n };\n\n const handleMouseEnter = (cell: CellIdentifier, event: React.MouseEvent) => {\n if (isDragging) {\n toggleCellSelection(cell, event.ctrlKey || event.metaKey, event.shiftKey, false);\n }\n };\n\n const handleMouseUp = () => {\n setIsDragging(false);\n };\n\n return {\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n","import { useState } from \"react\";\nimport { CellIdentifier, SelectionState } from \"../types\";\nimport { useMouseDragSelection } from \"./useMouseDragSelection\";\n\nconst getCellKey = (cell: Omit<CellIdentifier, \"id\">): string => {\n return `${cell.row}-${cell.col}`;\n};\n\nexport const useGridCellSelection = (allCells?: CellIdentifier[]) => {\n const [selectionState, setSelectionState] = useState<SelectionState>({\n selectedCells: new Map<string, CellIdentifier>(),\n });\n\n const toggleCellSelection = (cell: CellIdentifier, ctrlKey = false, shiftKey = false, newSelection = false) => {\n setSelectionState((prevState) => {\n const selectedCells = new Map(prevState.selectedCells);\n const cellKey = getCellKey(cell);\n\n if (prevState.startCell && !newSelection) {\n handleRangeSelection(selectedCells, prevState.startCell, cell, allCells);\n } else if ((shiftKey || ctrlKey) && newSelection) {\n handleSingleOrMultipleSelection(selectedCells, cellKey, cell);\n } else {\n if (selectedCells.has(cellKey)) {\n selectedCells.clear();\n } else {\n selectedCells.clear();\n selectedCells.set(cellKey, cell);\n }\n }\n\n return {\n ...prevState,\n selectedCells,\n startCell: newSelection ? cell : prevState.startCell,\n };\n });\n };\n\n const isCellSelected = (cell: CellIdentifier) => {\n return selectionState.selectedCells.has(getCellKey(cell));\n };\n\n const { handleMouseDown, handleMouseEnter, handleMouseUp } = useMouseDragSelection(toggleCellSelection);\n\n return {\n selectedCells: selectionState.selectedCells,\n isCellSelected,\n handleMouseDown,\n handleMouseEnter,\n handleMouseUp,\n };\n};\n\n// Updated helper functions\nfunction handleRangeSelection(\n selectedCells: Map<string, CellIdentifier>,\n startCell: CellIdentifier,\n endCell: CellIdentifier,\n allCells?: CellIdentifier[]\n) {\n const action = selectedCells.has(getCellKey(startCell)) ? \"set\" : \"delete\";\n const [startRow, startCol] = [startCell.row, startCell.col];\n const [endRow, endCol] = [endCell.row, endCell.col];\n\n const minRow = Math.min(startRow, endRow);\n const maxRow = Math.max(startRow, endRow);\n const minCol = Math.min(startCol, endCol);\n const maxCol = Math.max(startCol, endCol);\n\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n const cellKey = getCellKey({ row, col });\n if (action === \"set\") {\n if (allCells) {\n const cell = allCells.find((c) => c.row === row && c.col === col);\n cell && selectedCells.set(cellKey, cell);\n } else {\n selectedCells.set(cellKey, { id: cellKey, row, col });\n }\n } else {\n selectedCells.delete(cellKey);\n }\n }\n }\n}\n\nfunction handleSingleOrMultipleSelection(\n selectedCells: Map<string, CellIdentifier>,\n cellKey: string,\n cell: CellIdentifier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AAE,GAAG,IAAI,MAAM,EAAE,GAAG,EAAE,EAAA;gBAAlC,GAAG,CAAA,CAAA;AAcX,KAAA;AACH,CAAC;AAED,SAAS,+BAA+B,CACtC,aAA0C,EAC1C,OAAe,EACf,IAAoB,EAAA;AAEpB,IAAA,IAAI,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;AAC9B,QAAA,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/B,KAAA;AAAM,SAAA;AACL,QAAA,aAAa,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAClC,KAAA;AACH;;;;;"}
@@ -1,4 +1,5 @@
1
1
  export type CellIdentifier = {
2
+ id: string;
2
3
  row: number;
3
4
  col: number;
4
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grid-cell-selection",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A React hook for grid cell selection",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",