@@ -707,8 +707,14 @@ export function Table({
707707 if ( target && dragged !== target ) {
708708 const currentOrder = columnOrderRef . current ?? columnsRef . current . map ( ( c ) => c . name )
709709 const newOrder = currentOrder . filter ( ( n ) => n !== dragged )
710- let insertIndex = newOrder . indexOf ( target )
711- if ( side === 'right' ) insertIndex += 1
710+ const targetIndex = newOrder . indexOf ( target )
711+ if ( targetIndex === - 1 ) {
712+ setDragColumnName ( null )
713+ setDropTargetColumnName ( null )
714+ setDropSide ( 'left' )
715+ return
716+ }
717+ const insertIndex = side === 'right' ? targetIndex + 1 : targetIndex
712718 newOrder . splice ( insertIndex , 0 , dragged )
713719 setColumnOrder ( newOrder )
714720 updateMetadataRef . current ( {
@@ -1214,19 +1220,23 @@ export function Table({
12141220 }
12151221
12161222 // Create columns sequentially so each invalidation completes before the next
1223+ const createdColNames : string [ ] = [ ]
12171224 try {
12181225 for ( const name of newColNames ) {
12191226 await addColumnAsyncRef . current ( { name, type : 'string' } )
1227+ createdColNames . push ( name )
12201228 }
12211229 } catch {
1222- // If column creation fails, paste into whatever columns exist
1230+ // If column creation fails partway , paste into whatever columns were created
12231231 }
12241232
12251233 // Build updated column list locally — React Query cache may not have refreshed yet
1226- currentCols = [
1227- ...currentCols ,
1228- ...newColNames . map ( ( name ) => ( { name, type : 'string' as const } ) ) ,
1229- ]
1234+ if ( createdColNames . length > 0 ) {
1235+ currentCols = [
1236+ ...currentCols ,
1237+ ...createdColNames . map ( ( name ) => ( { name, type : 'string' as const } ) ) ,
1238+ ]
1239+ }
12301240 }
12311241
12321242 const undoCells : Array < { rowId : string ; data : Record < string , unknown > } > = [ ]
@@ -1685,6 +1695,12 @@ export function Table({
16851695 [ dragColumnName , displayColumns ]
16861696 )
16871697
1698+ const handleColumnSelect = useCallback ( ( colIndex : number ) => {
1699+ setSelectionAnchor ( { rowIndex : 0 , colIndex } )
1700+ setSelectionFocus ( { rowIndex : 0 , colIndex } )
1701+ setIsColumnSelection ( true )
1702+ } , [ ] )
1703+
16881704 const handleSortAsc = useCallback (
16891705 ( columnName : string ) => handleSortChange ( columnName , 'asc' ) ,
16901706 [ handleSortChange ]
@@ -1830,10 +1846,11 @@ export function Table({
18301846 checked = { isAllRowsSelected }
18311847 onCheckedChange = { handleSelectAllToggle }
18321848 />
1833- { displayColumns . map ( ( column ) => (
1849+ { displayColumns . map ( ( column , colIndex ) => (
18341850 < ColumnHeaderMenu
18351851 key = { column . name }
18361852 column = { column }
1853+ colIndex = { colIndex }
18371854 readOnly = { ! userPermissions . canEdit }
18381855 isRenaming = { columnRename . editingId === column . name }
18391856 renameValue = {
@@ -1862,6 +1879,7 @@ export function Table({
18621879 onSortAsc = { handleSortAsc }
18631880 onSortDesc = { handleSortDesc }
18641881 onFilterColumn = { handleFilterByColumn }
1882+ onColumnSelect = { handleColumnSelect }
18651883 />
18661884 ) ) }
18671885 { userPermissions . canEdit && (
@@ -2766,6 +2784,7 @@ const COLUMN_TYPE_OPTIONS: { type: string; label: string; icon: React.ElementTyp
27662784
27672785const ColumnHeaderMenu = React . memo ( function ColumnHeaderMenu ( {
27682786 column,
2787+ colIndex,
27692788 readOnly,
27702789 isRenaming,
27712790 renameValue,
@@ -2790,8 +2809,10 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
27902809 onSortAsc,
27912810 onSortDesc,
27922811 onFilterColumn,
2812+ onColumnSelect,
27932813} : {
27942814 column : ColumnDefinition
2815+ colIndex : number
27952816 readOnly ?: boolean
27962817 isRenaming : boolean
27972818 renameValue : string
@@ -2816,6 +2837,7 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
28162837 onSortAsc ?: ( columnName : string ) => void
28172838 onSortDesc ?: ( columnName : string ) => void
28182839 onFilterColumn ?: ( columnName : string ) => void
2840+ onColumnSelect ?: ( colIndex : number ) => void
28192841} ) {
28202842 const renameInputRef = useRef < HTMLInputElement > ( null )
28212843
@@ -2940,6 +2962,7 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
29402962 < button
29412963 type = 'button'
29422964 className = 'flex min-w-0 flex-1 cursor-pointer items-center px-2 py-[7px] outline-none'
2965+ onClick = { ( ) => onColumnSelect ?.( colIndex ) }
29432966 >
29442967 < ColumnTypeIcon type = { column . type } />
29452968 < span className = 'ml-1.5 min-w-0 overflow-clip text-ellipsis whitespace-nowrap font-medium text-[var(--text-primary)] text-small' >
0 commit comments