sheet-happens 0.0.9 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,15 +4,22 @@
4
4
 
5
5
  [![NPM](https://img.shields.io/npm/v/sheet-happens.svg)](https://www.npmjs.com/package/sheet-happens) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
6
6
 
7
+ ![Sheeeeeit](https://media.giphy.com/media/in6mnJNYjGKpq/source.gif)
8
+
9
+ ## About
10
+ Canvas-based spreadsheet component for React. <br />
11
+ Super fast and responsive regardless of the dataset size because it draws only the data user currently sees.
12
+
13
+ ![sheet-happens](https://raw.githubusercontent.com/farseerdev/sheet-happens/main/example/src/assets/sheethappensimg.png)
14
+
7
15
  ## Install
8
16
 
9
17
  ```bash
10
18
  npm install --save sheet-happens
11
19
  ```
12
20
 
13
- ![Alt Text](https://media.giphy.com/media/in6mnJNYjGKpq/source.gif)
14
-
15
21
  ## Usage
22
+ To use our Sheet in your project, just import the component and its style and render it:
16
23
 
17
24
  ```jsx
18
25
  import React, { Component } from 'react'
@@ -27,6 +34,27 @@ class Example extends Component {
27
34
  }
28
35
  ```
29
36
 
37
+ Of course, you can also display some data in it:
38
+ ```jsx
39
+ const [data, setData] = useState([[1,2,3], [10,20,30]]);
40
+
41
+ const displayData = (x, y) => {
42
+ return data?.[y]?.[x]?.toFixed?.(2);
43
+ };
44
+
45
+ return (
46
+ <div className="sheet-box">
47
+ <Sheet
48
+ sourceData={data}
49
+ displayData={displayData}
50
+ />
51
+ </div>
52
+ );
53
+ ```
54
+
55
+ Go to our [example page](https://farseerdev.github.io/sheet-happens/) to learn more about [features](https://farseerdev.github.io/sheet-happens#features) it has and checkout [documentation](https://farseerdev.github.io/sheet-happens#documentation) for detailed list of props you can send to this component. <br />
56
+
57
+ Along with descriptions, page also contains multiple Sheet components you can use to try out is it a good match for your needs. Edit some values, paste some data, drag columns and rows for resize, and so on.
30
58
 
31
59
 
32
60
  ## License
package/dist/index.d.ts CHANGED
@@ -44,6 +44,7 @@ export interface SheetProps {
44
44
  sourceData?: CellProperty<string | number | null>;
45
45
  displayData?: CellProperty<CellContentType>;
46
46
  editData?: CellProperty<string>;
47
+ editKeys?: CellProperty<string>;
47
48
  inputComponent?: (x: number, y: number, props: SheetInputProps, commitEditingCell?: () => void) => ReactElement | undefined;
48
49
  onSelectionChanged?: (x1: number, y1: number, x2: number, y2: number) => void;
49
50
  onRightClick?: (e: SheetMouseEvent) => void;
package/dist/index.js CHANGED
@@ -641,45 +641,49 @@ function Sheet(props) {
641
641
  editValue = _useState6[0],
642
642
  setEditValue = _useState6[1];
643
643
 
644
- var _useState7 = React.useState(false),
645
- arrowKeyCommitMode = _useState7[0],
646
- setArrowKeyCommitMode = _useState7[1];
644
+ var _useState7 = React.useState(''),
645
+ editKey = _useState7[0],
646
+ setEditKey = _useState7[1];
647
647
 
648
648
  var _useState8 = React.useState(false),
649
- shiftKeyDown = _useState8[0],
650
- setShiftKeyDown = _useState8[1];
649
+ arrowKeyCommitMode = _useState8[0],
650
+ setArrowKeyCommitMode = _useState8[1];
651
651
 
652
652
  var _useState9 = React.useState(false),
653
- knobDragInProgress = _useState9[0],
654
- setKnobDragInProgress = _useState9[1];
653
+ shiftKeyDown = _useState9[0],
654
+ setShiftKeyDown = _useState9[1];
655
655
 
656
656
  var _useState10 = React.useState(false),
657
- selectionInProgress = _useState10[0],
658
- setSelectionInProgress = _useState10[1];
657
+ knobDragInProgress = _useState10[0],
658
+ setKnobDragInProgress = _useState10[1];
659
659
 
660
- var _useState11 = React.useState(null),
661
- columnResize = _useState11[0],
662
- setColumnResize = _useState11[1];
660
+ var _useState11 = React.useState(false),
661
+ selectionInProgress = _useState11[0],
662
+ setSelectionInProgress = _useState11[1];
663
663
 
664
664
  var _useState12 = React.useState(null),
665
- rowResize = _useState12[0],
666
- setRowResize = _useState12[1];
665
+ columnResize = _useState12[0],
666
+ setColumnResize = _useState12[1];
667
667
 
668
- var _useState13 = React.useState(false),
669
- rowSelectionInProgress = _useState13[0],
670
- setRowSelectionInProgress = _useState13[1];
668
+ var _useState13 = React.useState(null),
669
+ rowResize = _useState13[0],
670
+ setRowResize = _useState13[1];
671
671
 
672
672
  var _useState14 = React.useState(false),
673
- columnSelectionInProgress = _useState14[0],
674
- setColumnSelectionInProgress = _useState14[1];
673
+ rowSelectionInProgress = _useState14[0],
674
+ setRowSelectionInProgress = _useState14[1];
675
675
 
676
- var _useState15 = React.useState({
676
+ var _useState15 = React.useState(false),
677
+ columnSelectionInProgress = _useState15[0],
678
+ setColumnSelectionInProgress = _useState15[1];
679
+
680
+ var _useState16 = React.useState({
677
681
  x: -1,
678
682
  y: -1,
679
683
  hitTarget: null
680
684
  }),
681
- buttonClickMouseDownCoordinates = _useState15[0],
682
- setButtonClickMouseDownCoordinates = _useState15[1];
685
+ buttonClickMouseDownCoordinates = _useState16[0],
686
+ setButtonClickMouseDownCoordinates = _useState16[1];
683
687
 
684
688
  var _useResizeObserver = useResizeObserver({
685
689
  ref: canvasRef
@@ -715,9 +719,23 @@ function Sheet(props) {
715
719
  var editData = React.useMemo(function () {
716
720
  return createCellPropFunction(props.editData, '');
717
721
  }, [props.editData]);
722
+ var editKeys = React.useMemo(function () {
723
+ return createCellPropFunction(props.editKeys, '');
724
+ }, [props.editKeys]);
718
725
  var cellStyle = React.useMemo(function () {
719
726
  return createCellPropFunction(props.cellStyle, defaultCellStyle);
720
727
  }, [props.cellStyle]);
728
+ React.useEffect(function () {
729
+ var currEditKey = editKeys ? editKeys(editCell.x, editCell.y) : '';
730
+
731
+ if (currEditKey !== editKey) {
732
+ setEditCell({
733
+ x: -1,
734
+ y: -1
735
+ });
736
+ setEditKey('');
737
+ }
738
+ }, [editKeys]);
721
739
  var columnSizes = React.useMemo(function () {
722
740
  return calculateRowsOrColsSizes(freezeColumns, cellWidth, rowHeaderWidth, dataOffset.x, canvasWidth);
723
741
  }, [props.freezeColumns, cellWidth, dataOffset.x, canvasWidth]);
@@ -1114,10 +1132,26 @@ function Sheet(props) {
1114
1132
  var td = _step18.value;
1115
1133
 
1116
1134
  if (td.nodeName === 'TD') {
1135
+ var str = '';
1136
+
1137
+ if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
1138
+ var p = td.children[0];
1139
+
1140
+ if (p.children.length !== 0 && p.children[0].nodeName === 'FONT') {
1141
+ str = p.children[0].innerHTML;
1142
+ } else {
1143
+ str = p.innerHTML;
1144
+ }
1145
+ } else {
1146
+ str = td.innerHTML;
1147
+ }
1148
+
1149
+ str = str.replaceAll('\n', '');
1150
+ str = str.replaceAll(/\s\s+/g, ' ');
1117
1151
  changes.push({
1118
1152
  y: y,
1119
1153
  x: x,
1120
- value: td.innerHTML
1154
+ value: str
1121
1155
  });
1122
1156
  x++;
1123
1157
  }
@@ -1249,6 +1283,7 @@ function Sheet(props) {
1249
1283
  x: -1,
1250
1284
  y: -1
1251
1285
  });
1286
+ setEditKey('');
1252
1287
  };
1253
1288
 
1254
1289
  var startEditingCell = function startEditingCell(editCell) {
@@ -1263,7 +1298,9 @@ function Sheet(props) {
1263
1298
  val = editDataValue;
1264
1299
  }
1265
1300
 
1301
+ var editDataKey = editKeys ? editKeys(editCell.x, editCell.y) : '';
1266
1302
  setEditCell(editCell);
1303
+ setEditKey(editDataKey);
1267
1304
  setEditValue(val);
1268
1305
  };
1269
1306
 
@@ -1399,7 +1436,7 @@ function Sheet(props) {
1399
1436
  var scrollToP2 = true;
1400
1437
 
1401
1438
  if (x < rowHeaderWidth) {
1402
- sel2.x = 100;
1439
+ sel2.x = dataOffset.x + 100;
1403
1440
  scrollToP2 = false;
1404
1441
  setRowSelectionInProgress(true);
1405
1442
  } else {
@@ -1407,7 +1444,7 @@ function Sheet(props) {
1407
1444
  }
1408
1445
 
1409
1446
  if (y < columnHeaderHeight) {
1410
- sel2.y = 100;
1447
+ sel2.y = dataOffset.y + 100;
1411
1448
  scrollToP2 = false;
1412
1449
  setColumnSelectionInProgress(true);
1413
1450
  } else {
@@ -1420,6 +1457,7 @@ function Sheet(props) {
1420
1457
  x: -1,
1421
1458
  y: -1
1422
1459
  });
1460
+ setEditKey('');
1423
1461
  };
1424
1462
 
1425
1463
  var onMouseUp = function onMouseUp(e) {
@@ -1665,7 +1703,14 @@ function Sheet(props) {
1665
1703
  y2 = selection.y1;
1666
1704
  }
1667
1705
 
1668
- if (Math.abs(cell.x - (x1 + x2) * 0.5) < Math.abs(cell.y - (y1 + y2) * 0.5)) {
1706
+ var xCellDiff = 0;
1707
+ if (cell.x < x1) xCellDiff = cell.x - x1;
1708
+ if (cell.x > x2) xCellDiff = x2 - cell.x;
1709
+ var yCellDiff = 0;
1710
+ if (cell.y < y1) yCellDiff = cell.y - y1;
1711
+ if (cell.y > y2) yCellDiff = y2 - cell.y;
1712
+
1713
+ if (xCellDiff > yCellDiff) {
1669
1714
  if (cell.y < y1) {
1670
1715
  y1 = cell.y;
1671
1716
  } else {
@@ -1689,7 +1734,9 @@ function Sheet(props) {
1689
1734
  };
1690
1735
 
1691
1736
  var onDoubleClick = function onDoubleClick(e) {
1692
- if (!e.target || !(e.target instanceof Element)) {
1737
+ e.preventDefault();
1738
+
1739
+ if (!e.target || !(e.target instanceof Element) || shiftKeyDown) {
1693
1740
  return;
1694
1741
  }
1695
1742
 
@@ -1720,6 +1767,7 @@ function Sheet(props) {
1720
1767
  x: -1,
1721
1768
  y: -1
1722
1769
  });
1770
+ setEditKey('');
1723
1771
  return;
1724
1772
  }
1725
1773
 
@@ -1893,6 +1941,18 @@ function Sheet(props) {
1893
1941
  y1 = selection.y1,
1894
1942
  y2 = selection.y2;
1895
1943
 
1944
+ if (x1 > x2) {
1945
+ var _ref = [x2, x1];
1946
+ x1 = _ref[0];
1947
+ x2 = _ref[1];
1948
+ }
1949
+
1950
+ if (y1 > y2) {
1951
+ var _ref2 = [y2, y1];
1952
+ y1 = _ref2[0];
1953
+ y2 = _ref2[1];
1954
+ }
1955
+
1896
1956
  if (!(y > columnHeaderHeight && x > rowHeaderWidth)) {
1897
1957
  return;
1898
1958
  }