-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseRows.ts
164 lines (139 loc) · 4.8 KB
/
useRows.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {useState, useCallback, useRef, useEffect, useMemo} from 'react';
import {FormApi} from 'final-form';
export interface UseRowsProps<RowType> {
generateRow: (_?: any) => RowType;
form: FormApi;
initialRows?: RowType[];
}
export interface UseRowsInstance<RowType, RowFields extends {} = {}> {
rows: RowType[];
getRows: () => RowType[];
getRowIndex: (row: RowType) => number;
update: (rows: RowType[]) => void;
push: (row: RowType, values?: RowFields) => void;
remove: (row: RowType) => void;
unshift: (row?: RowType, values?: RowFields) => void;
copy: (row: RowType, newRow?: RowType, position?: number) => void;
drag: (dragRow: RowType, hoverRow: RowType) => void;
generate: (_?: any) => RowType;
getDirty: () => boolean;
// TODO: implement methods like in https://final-form.org/docs/final-form-arrays/api
concat: () => void;
insert: () => void;
move: () => void;
pop: () => void;
removeBatch: () => void;
shift: () => void;
swap: () => void;
}
// TODO: useRows must return an existing instance of rows
export function useRows<RowType extends string = string, RowFields extends {} = {}>({
generateRow,
form,
initialRows,
}: UseRowsProps<RowType>): UseRowsInstance<RowType, RowFields> {
const {change, getState} = form;
const [rows, setRows] = useState(initialRows ?? (Object.keys(getState().values) as RowType[]));
const rowsRef = useRef(rows);
const dirtyRef = useRef(false);
const generate = useMemo(() => generateRow, [generateRow]);
useEffect(() => {
setRows(initialRows ? initialRows : []);
}, [initialRows]);
// store rows without updates
useEffect(() => {
dirtyRef.current = rows !== initialRows;
rowsRef.current = rows;
}, [initialRows, rows]);
// TODO: will be implemented
const concat = useCallback(() => {}, []);
// TODO: will be implemented
const insert = useCallback(() => {}, []);
// TODO: will be implemented
const move = useCallback(() => {}, []);
// TODO: will be implemented
const pop = useCallback(() => {}, []);
const push = useCallback(() => {
const newRow = generate();
setRows((currentRows) => [...currentRows, newRow]);
}, [generate]);
const remove = useCallback(
(deletingRow: string) => {
setRows((currentRows) => currentRows.filter((item) => item !== deletingRow));
change(deletingRow, null);
},
[change],
);
// TODO: will be implemented
const removeBatch = useCallback(() => {}, []);
// TODO: will be implemented
const shift = useCallback(() => {}, []);
// TODO: will be implemented
const swap = useCallback(() => {}, []);
const unshift = useCallback(
(newRow = generate(), values: any) => {
setRows((currentRows) => [newRow, ...currentRows]);
if (values) {
change(newRow, values);
}
},
[generate, change],
);
const update = useCallback((newRows: RowType[]) => {
setRows(newRows);
}, []);
const copy = useCallback(
(row: string, newRow = generate(), position = 0) => {
// TODO: @makhnatkin check insert position logic
setRows((currentRows) => {
const copiedIndex = position || currentRows.findIndex((item) => item === row);
const updatedRows = [...currentRows];
updatedRows.splice(copiedIndex, 0, newRow);
return updatedRows;
});
change(newRow, getState().values[row]);
},
[change, generate, getState],
);
const drag = useCallback((dragRow: string, hoverRow: string) => {
setRows((currentRows) => {
const updatedRows = [...currentRows];
const dragIndex = updatedRows.findIndex((row) => row === dragRow);
const hoverIndex = updatedRows.findIndex((row) => row === hoverRow);
if (dragIndex !== -1 && hoverIndex !== -1) {
[updatedRows[dragIndex], updatedRows[hoverIndex]] = [
updatedRows[hoverIndex],
updatedRows[dragIndex],
];
}
return updatedRows;
});
}, []);
const getRows = useCallback(() => {
return rowsRef.current;
}, []);
const getDirty = useCallback(() => {
return dirtyRef.current;
}, []);
const getRowIndex = useCallback((row: string) => rowsRef.current.findIndex((item) => item === row), []);
return {
rows,
getRows,
getRowIndex,
update,
push,
remove,
unshift,
copy,
drag,
concat,
insert,
move,
pop,
removeBatch,
shift,
swap,
generate,
getDirty,
};
}