@@ -737,6 +737,31 @@ export function Table({
737737 return
738738 }
739739
740+ if ( ( e . key === 'Delete' || e . key === 'Backspace' ) && checkedRowsRef . current . size > 0 ) {
741+ if ( editingCellRef . current ) return
742+ e . preventDefault ( )
743+ const checked = checkedRowsRef . current
744+ const pMap = positionMapRef . current
745+ const currentCols = columnsRef . current
746+ const undoCells : Array < { rowId : string ; data : Record < string , unknown > } > = [ ]
747+ for ( const pos of checked ) {
748+ const row = pMap . get ( pos )
749+ if ( ! row ) continue
750+ const updates : Record < string , unknown > = { }
751+ const previousData : Record < string , unknown > = { }
752+ for ( const col of currentCols ) {
753+ previousData [ col . name ] = row . data [ col . name ] ?? null
754+ updates [ col . name ] = null
755+ }
756+ undoCells . push ( { rowId : row . id , data : previousData } )
757+ mutateRef . current ( { rowId : row . id , data : updates } )
758+ }
759+ if ( undoCells . length > 0 ) {
760+ pushUndoRef . current ( { type : 'clear-cells' , cells : undoCells } )
761+ }
762+ return
763+ }
764+
740765 const anchor = selectionAnchorRef . current
741766 if ( ! anchor || editingCellRef . current ) return
742767
@@ -837,41 +862,24 @@ export function Table({
837862
838863 if ( e . key === 'Delete' || e . key === 'Backspace' ) {
839864 e . preventDefault ( )
840- const checked = checkedRowsRef . current
865+ const sel = computeNormalizedSelection ( anchor , selectionFocusRef . current )
866+ if ( ! sel ) return
841867 const pMap = positionMapRef . current
842868 const undoCells : Array < { rowId : string ; data : Record < string , unknown > } > = [ ]
843-
844- if ( checked . size > 0 ) {
845- for ( const pos of checked ) {
846- const row = pMap . get ( pos )
847- if ( ! row ) continue
848- const updates : Record < string , unknown > = { }
849- const previousData : Record < string , unknown > = { }
850- for ( const col of cols ) {
851- previousData [ col . name ] = row . data [ col . name ] ?? null
852- updates [ col . name ] = null
853- }
854- undoCells . push ( { rowId : row . id , data : previousData } )
855- mutateRef . current ( { rowId : row . id , data : updates } )
856- }
857- } else {
858- const sel = computeNormalizedSelection ( anchor , selectionFocusRef . current )
859- if ( ! sel ) return
860- for ( let r = sel . startRow ; r <= sel . endRow ; r ++ ) {
861- const row = pMap . get ( r )
862- if ( ! row ) continue
863- const updates : Record < string , unknown > = { }
864- const previousData : Record < string , unknown > = { }
865- for ( let c = sel . startCol ; c <= sel . endCol ; c ++ ) {
866- if ( c < cols . length ) {
867- const colName = cols [ c ] . name
868- previousData [ colName ] = row . data [ colName ] ?? null
869- updates [ colName ] = null
870- }
869+ for ( let r = sel . startRow ; r <= sel . endRow ; r ++ ) {
870+ const row = pMap . get ( r )
871+ if ( ! row ) continue
872+ const updates : Record < string , unknown > = { }
873+ const previousData : Record < string , unknown > = { }
874+ for ( let c = sel . startCol ; c <= sel . endCol ; c ++ ) {
875+ if ( c < cols . length ) {
876+ const colName = cols [ c ] . name
877+ previousData [ colName ] = row . data [ colName ] ?? null
878+ updates [ colName ] = null
871879 }
872- undoCells . push ( { rowId : row . id , data : previousData } )
873- mutateRef . current ( { rowId : row . id , data : updates } )
874880 }
881+ undoCells . push ( { rowId : row . id , data : previousData } )
882+ mutateRef . current ( { rowId : row . id , data : updates } )
875883 }
876884 if ( undoCells . length > 0 ) {
877885 pushUndoRef . current ( { type : 'clear-cells' , cells : undoCells } )
0 commit comments