-
Notifications
You must be signed in to change notification settings - Fork 10
/
onchange.stories.tsx
79 lines (76 loc) · 2.32 KB
/
onchange.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from "react";
import { GridSheet } from "../../../src";
import { constructInitialCells } from "../../../src/lib/structs";
import { createTableRef } from "../../../src/components/Tabular";
import {HistoryType} from "../../../src/types";
export default {
title: "Table operations",
};
export const SheetOnChange = () => {
const [diff, setDiff] = React.useState<Record<string, any>>({});
const [histories, setHistories] = React.useState<HistoryType[]>([]);
const tableRef = createTableRef();
return (
<>
<div style={{ display: "flex" }}>
<div style={{ flex: 1 }}>
<GridSheet
tableRef={tableRef}
initialCells={constructInitialCells({
matrices: {
A1: [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
],
},
cells: {},
ensured: {
numRows: 50,
numCols: 50,
},
})}
options={{
sheetWidth: 500,
onChange: (table, positions) => {
setDiff(
table.getObjectFlatten({
filter: (cell) =>
!!cell?.changedAt &&
cell.changedAt > table.lastChangedAt!,
})
);
const histories = table.getHistories();
setHistories(histories);
const h = histories[histories.length - 1];
if (h?.operation === "UPDATE") {
console.log(
"histories",
table.getAddressesByIds(h.diffAfter)
);
}
},
}}
/>
<div>Diff:</div>
<pre id="diff">{JSON.stringify(diff)}</pre>
</div>
<ul className="histories">
{histories.map((history, i) => (
<li key={i}>
[{history.operation}]
{(() => {
if (history.operation === "UPDATE") {
return JSON.stringify(
tableRef.current?.table!.getAddressesByIds(
history.diffAfter
)
);
}
})()}
</li>
))}
</ul>
</div>
</>
);
};