Skip to content

Commit 0aef36b

Browse files
waleedlatif1claude
andcommitted
fix(tables): preserve selection on right-click, auto-resize on double-click, fix escape during drag
- Right-clicking within an existing selection now preserves it instead of resetting to a single cell, so context menu operations apply to the full range - Double-clicking a column border auto-resizes the column to fit its content - Escape during column drag now immediately clears refs before state update, preventing the dragend handler from executing the reorder Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent abafacb commit 0aef36b

1 file changed

Lines changed: 61 additions & 2 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table/table.tsx

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,24 @@ export function Table({
604604
const rowIndex = Number.parseInt(td.getAttribute('data-row') || '-1', 10)
605605
const colIndex = Number.parseInt(td.getAttribute('data-col') || '-1', 10)
606606
if (rowIndex >= 0 && colIndex >= 0) {
607-
setSelectionAnchor({ rowIndex, colIndex })
608-
setSelectionFocus(null)
609607
columnName =
610608
colIndex < columnsRef.current.length ? columnsRef.current[colIndex].name : null
609+
610+
const sel = computeNormalizedSelection(
611+
selectionAnchorRef.current,
612+
selectionFocusRef.current
613+
)
614+
const isWithinSelection =
615+
sel !== null &&
616+
rowIndex >= sel.startRow &&
617+
rowIndex <= sel.endRow &&
618+
colIndex >= sel.startCol &&
619+
colIndex <= sel.endCol
620+
621+
if (!isWithinSelection) {
622+
setSelectionAnchor({ rowIndex, colIndex })
623+
setSelectionFocus(null)
624+
}
611625
}
612626
}
613627
baseHandleRowContextMenu(e, row, columnName)
@@ -731,6 +745,40 @@ export function Table({
731745
updateMetadataRef.current({ columnWidths: columnWidthsRef.current })
732746
}, [])
733747

748+
const handleColumnAutoResize = useCallback((columnName: string) => {
749+
const cols = columnsRef.current
750+
const colIndex = cols.findIndex((c) => c.name === columnName)
751+
if (colIndex === -1) return
752+
753+
const currentRows = rowsRef.current
754+
let maxWidth = COL_WIDTH_MIN
755+
756+
const measure = document.createElement('span')
757+
measure.style.cssText =
758+
'position:absolute;visibility:hidden;white-space:nowrap;font:inherit;padding:0 8px'
759+
document.body.appendChild(measure)
760+
761+
measure.className = 'font-medium text-small'
762+
measure.textContent = columnName
763+
maxWidth = Math.max(maxWidth, measure.offsetWidth + 48)
764+
765+
measure.className = 'text-small'
766+
for (const row of currentRows) {
767+
const val = row.data[columnName]
768+
if (val == null) continue
769+
measure.textContent = String(val)
770+
maxWidth = Math.max(maxWidth, measure.offsetWidth + 20)
771+
}
772+
773+
document.body.removeChild(measure)
774+
775+
const newWidth = Math.min(Math.ceil(maxWidth), 600)
776+
setColumnWidths((prev) => ({ ...prev, [columnName]: newWidth }))
777+
const updated = { ...columnWidthsRef.current, [columnName]: newWidth }
778+
columnWidthsRef.current = updated
779+
updateMetadataRef.current({ columnWidths: updated })
780+
}, [])
781+
734782
const handleColumnDragStart = useCallback((columnName: string) => {
735783
setDragColumnName(columnName)
736784
}, [])
@@ -928,6 +976,9 @@ export function Table({
928976
if (e.key === 'Escape') {
929977
e.preventDefault()
930978
if (dragColumnNameRef.current) {
979+
dragColumnNameRef.current = null
980+
dropTargetColumnNameRef.current = null
981+
dropSideRef.current = 'left'
931982
setDragColumnName(null)
932983
setDropTargetColumnName(null)
933984
setDropSide('left')
@@ -1957,6 +2008,7 @@ export function Table({
19572008
onResizeStart={handleColumnResizeStart}
19582009
onResize={handleColumnResize}
19592010
onResizeEnd={handleColumnResizeEnd}
2011+
onAutoResize={handleColumnAutoResize}
19602012
onDragStart={handleColumnDragStart}
19612013
onDragOver={handleColumnDragOver}
19622014
onDragEnd={handleColumnDragEnd}
@@ -2880,6 +2932,7 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
28802932
onResizeStart,
28812933
onResize,
28822934
onResizeEnd,
2935+
onAutoResize,
28832936
onDragStart,
28842937
onDragOver,
28852938
onDragEnd,
@@ -2904,6 +2957,7 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
29042957
onResizeStart: (columnName: string) => void
29052958
onResize: (columnName: string, width: number) => void
29062959
onResizeEnd: () => void
2960+
onAutoResize: (columnName: string) => void
29072961
onDragStart?: (columnName: string) => void
29082962
onDragOver?: (columnName: string, side: 'left' | 'right') => void
29092963
onDragEnd?: () => void
@@ -3150,6 +3204,11 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
31503204
draggable={false}
31513205
onDragStart={(e) => e.stopPropagation()}
31523206
onPointerDown={handleResizePointerDown}
3207+
onDoubleClick={(e) => {
3208+
e.preventDefault()
3209+
e.stopPropagation()
3210+
onAutoResize(column.name)
3211+
}}
31533212
/>
31543213
</th>
31553214
)

0 commit comments

Comments
 (0)