@@ -12,6 +12,7 @@ import { useExportToCSV, useLongPress } from "./hooks";
12
12
import { Icons } from "./icons" ;
13
13
import { SearchInput } from "./search" ;
14
14
import { Loading } from "./loading" ;
15
+ import { clone } from "lodash" ;
15
16
16
17
type IPaginationProps = {
17
18
pageCount : number ;
@@ -169,6 +170,10 @@ const TData: FC<ITDataProps> = ({ cell, onCellUpdate, disableEdit }) => {
169
170
}
170
171
} , [ editedData ] ) ;
171
172
173
+ useEffect ( ( ) => {
174
+ setEditedData ( cell . value ) ;
175
+ } , [ cell . value ] ) ;
176
+
172
177
return < div ref = { cellRef } { ...cell . getCellProps ( ) }
173
178
className = { classNames ( "relative group/data cursor-pointer transition-all text-xs table-cell border-t border-l last:border-r group-last/row:border-b group-last/row:first:rounded-bl-lg group-last/row:last:rounded-br-lg border-gray-200 dark:border-white/5 p-0" , {
174
179
"bg-gray-200 dark:bg-white/10 blur-[2px]" : editable || preview ,
@@ -281,9 +286,6 @@ const TableRow: FC<ITableRow> = ({ row, style, onRowUpdate, disableEdit }) => {
281
286
return Promise . reject ( ) ;
282
287
}
283
288
const updatedRow = row . cells . reduce ( ( all , one ) => {
284
- if ( one . column . id === "#" ) {
285
- return all ;
286
- }
287
289
all [ one . column . id ] = one . value ;
288
290
return all ;
289
291
} , { } as Record < string , string > ) ;
@@ -324,6 +326,7 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
324
326
const [ searchIndex , setSearchIndex ] = useState ( 0 ) ;
325
327
const [ height , setHeight ] = useState ( 0 ) ;
326
328
const [ width , setWidth ] = useState ( 0 ) ;
329
+ const [ data , setData ] = useState < Record < string , string > [ ] > ( [ ] ) ;
327
330
328
331
const columns = useMemo ( ( ) => {
329
332
let colWidth = 150 ;
@@ -345,13 +348,13 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
345
348
return cols ;
346
349
} , [ actualColumns , width ] ) ;
347
350
348
- const data = useMemo ( ( ) => {
349
- return actualRows . map ( ( row , rowIndex ) => {
351
+ useEffect ( ( ) => {
352
+ setData ( actualRows . map ( ( row , rowIndex ) => {
350
353
return row . reduce ( ( all , one , colIndex ) => {
351
354
all [ actualColumns [ colIndex ] ] = one ;
352
355
return all ;
353
356
} , { "#" : ( rowIndex + 1 ) . toString ( ) } as Record < string , string > ) ;
354
- } ) ;
357
+ } ) ) ;
355
358
} , [ actualColumns , actualRows ] ) ;
356
359
357
360
const sortedRows = useMemo ( ( ) => {
@@ -463,11 +466,24 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
463
466
setDirection ( "dsc" ) ;
464
467
} , [ sortedColumn , direction ] ) ;
465
468
469
+ const handleRowUpdate = useCallback ( ( index : number , row : Record < string , string > ) => {
470
+ if ( onRowUpdate == null ) {
471
+ return Promise . resolve ( ) ;
472
+ }
473
+ setData ( value => {
474
+ const newValue = clone ( value ) ;
475
+ newValue [ index ] = clone ( row ) ;
476
+ return newValue ;
477
+ } ) ;
478
+ delete row [ "#" ] ;
479
+ return onRowUpdate ( row ) ;
480
+ } , [ onRowUpdate ] ) ;
481
+
466
482
const handleRenderRow = useCallback ( ( { index, style } : ListChildComponentProps ) => {
467
483
const row = rows [ index ] ;
468
484
prepareRow ( row ) ;
469
- return < TableRow key = { `row-${ row . values [ actualColumns [ 0 ] ] } ` } row = { row } style = { style } onRowUpdate = { onRowUpdate } disableEdit = { disableEdit } /> ;
470
- } , [ rows , prepareRow , actualColumns , onRowUpdate , disableEdit ] ) ;
485
+ return < TableRow key = { `row-${ row . values [ actualColumns [ 0 ] ] } ` } row = { row } style = { style } onRowUpdate = { ( row ) => handleRowUpdate ( index , row ) } disableEdit = { disableEdit } /> ;
486
+ } , [ rows , prepareRow , actualColumns , handleRowUpdate , disableEdit ] ) ;
471
487
472
488
useEffect ( ( ) => {
473
489
if ( containerRef . current == null || operationsRef . current == null ) {
0 commit comments