Skip to content

Commit fe9d0a4

Browse files
committed
Add types
1 parent 39c10a5 commit fe9d0a4

File tree

1 file changed

+74
-17
lines changed
  • specifyweb/frontend/js_src/lib/components/WorkBench

1 file changed

+74
-17
lines changed

specifyweb/frontend/js_src/lib/components/WorkBench/hooks.ts

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type Handsontable from 'handsontable';
2-
import type { Events } from 'handsontable/pluginHooks';
2+
import type { Hooks } from 'handsontable';
33
import type { Action } from 'handsontable/plugins/undoRedo';
44
import React from 'react';
55

@@ -32,7 +32,7 @@ export function useHotHooks({
3232
readonly checkDeletedFail: (statusCode: number) => boolean;
3333
readonly isReadOnly: boolean;
3434
readonly isResultsOpen: boolean;
35-
}): Partial<Events> {
35+
}): Partial<Hooks> {
3636
let sortConfigIsSet: boolean = false;
3737
const loading = React.useContext(LoadingContext);
3838

@@ -45,7 +45,13 @@ export function useHotHooks({
4545
* cases
4646
*
4747
*/
48-
afterRenderer: (td, visualRow, visualCol, property, _value) => {
48+
afterRenderer: (
49+
td: HTMLTableCellElement,
50+
visualRow: number,
51+
visualCol: number,
52+
property: number | string,
53+
_value: unknown
54+
) => {
4955
if (workbench.hot === undefined) {
5056
td.classList.add('text-gray-500');
5157
return;
@@ -83,7 +89,11 @@ export function useHotHooks({
8389
},
8490

8591
// Make HOT use defaultValues for validation if cell is empty
86-
beforeValidate: (value, _visualRow, property) => {
92+
beforeValidate: (
93+
value: unknown,
94+
_visualRow: number,
95+
property: number | string
96+
) => {
8797
if (Boolean(value) || workbench.hot === undefined) return value;
8898

8999
const visualCol = workbench.hot.propToCol(property);
@@ -93,10 +103,10 @@ export function useHotHooks({
93103
},
94104

95105
afterValidate: (
96-
isValid,
97-
value: string | null = '',
98-
visualRow,
99-
property
106+
isValid: boolean,
107+
value: string | null,
108+
visualRow: number,
109+
property: number | string
100110
) => {
101111
if (workbench.hot === undefined) return;
102112
const visualCol = workbench.hot.propToCol(property);
@@ -158,7 +168,15 @@ export function useHotHooks({
158168
* arguments that are inconvenient to work with
159169
*
160170
*/
161-
beforeChange: (unfilteredChanges, source) => {
171+
beforeChange: (
172+
unfilteredChanges: readonly (readonly [
173+
number,
174+
number | string,
175+
unknown,
176+
unknown,
177+
])[],
178+
source: string
179+
) => {
162180
if (source !== 'CopyPaste.paste') return true;
163181

164182
const filteredChanges = unfilteredChanges.filter(
@@ -181,7 +199,15 @@ export function useHotHooks({
181199
return false;
182200
},
183201

184-
afterChange: (unfilteredChanges, source) => {
202+
afterChange: (
203+
unfilteredChanges: readonly (readonly [
204+
number,
205+
number | string,
206+
unknown,
207+
unknown,
208+
])[],
209+
source: string
210+
) => {
185211
if (
186212
![
187213
'edit',
@@ -307,7 +333,11 @@ export function useHotHooks({
307333
* beforeCreateRow, instead of afterCreateRow
308334
*
309335
*/
310-
beforeCreateRow: (visualRowStart, amount, source) => {
336+
beforeCreateRow: (
337+
visualRowStart: number,
338+
amount: number,
339+
source?: string
340+
) => {
311341
const addedRows = Array.from(
312342
{ length: amount },
313343
(_, index) =>
@@ -332,7 +362,12 @@ export function useHotHooks({
332362
return true;
333363
},
334364

335-
beforeRemoveRow: (visualRowStart, amount, _, source) => {
365+
beforeRemoveRow: (
366+
visualRowStart: number,
367+
amount: number,
368+
_,
369+
source?: string
370+
) => {
336371
if (workbench.hot === undefined) return;
337372
// Get indexes of removed rows in reverse order
338373
const removedRows = Array.from({ length: amount }, (_, index) =>
@@ -363,7 +398,10 @@ export function useHotHooks({
363398
* finding all lower level ranks of that tree (within the same -to-many)
364399
* and sorting them in the same direction
365400
*/
366-
beforeColumnSort: (currentSortConfig, newSortConfig) => {
401+
beforeColumnSort: (
402+
currentSortConfig: readonly Handsontable.plugins.ColumnSorting.Config[],
403+
newSortConfig: readonly Handsontable.plugins.ColumnSorting.Config[]
404+
) => {
367405
workbench.cells.indexedCellMeta = undefined;
368406

369407
if (
@@ -455,7 +493,10 @@ export function useHotHooks({
455493
},
456494

457495
// Cache sort config to preserve column sort order across sessions
458-
afterColumnSort: async (_previousSortConfig, sortConfig) => {
496+
afterColumnSort: async (
497+
_previousSortConfig: readonly Handsontable.plugins.ColumnSorting.Config[],
498+
sortConfig: readonly Handsontable.plugins.ColumnSorting.Config[]
499+
) => {
459500
if (workbench.hot === undefined) return;
460501
const physicalSortConfig = sortConfig.map((rest) => ({
461502
...rest,
@@ -468,12 +509,20 @@ export function useHotHooks({
468509
);
469510
},
470511

471-
beforeColumnMove: (_columnIndexes, _finalIndex, dropIndex) =>
512+
beforeColumnMove: (
513+
_columnIndexes: readonly number[],
514+
_finalIndex: number,
515+
dropIndex?: number
516+
) =>
472517
!isResultsOpen &&
473518
(dropIndex !== undefined || workbench.hot !== undefined),
474519

475520
// Save new visualOrder on the back end
476-
afterColumnMove: (_columnIndexes, _finalIndex, dropIndex) => {
521+
afterColumnMove: (
522+
_columnIndexes: readonly number[],
523+
_finalIndex: number,
524+
dropIndex?: number
525+
) => {
477526
// An ugly fix for jQuery's dialogs conflicting with HOT
478527
if (dropIndex === undefined || workbench.hot == undefined) return;
479528
workbench.cells.indexedCellMeta = undefined;
@@ -500,7 +549,15 @@ export function useHotHooks({
500549
},
501550

502551
// Do not scroll the viewport to the last column after inserting a row
503-
afterPaste: (data, coords) => {
552+
afterPaste: (
553+
data: readonly (readonly string[])[],
554+
coords: readonly {
555+
readonly startRow: number;
556+
readonly startCol: number;
557+
readonly endRow: number;
558+
readonly endCol: number;
559+
}[]
560+
) => {
504561
const lastCoords = coords.at(-1);
505562
if (
506563
typeof lastCoords === 'object' &&

0 commit comments

Comments
 (0)