Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Apply dynamic width to foreign row selector grid

Joshen Lim6ee5f9ccf64d
diff --git a/studio/components/grid/utils/gridColumns.tsx b/studio/components/grid/utils/gridColumns.tsx
index 29e9e958d5b..febc8748dec 100644
--- a/studio/components/grid/utils/gridColumns.tsx
+++ b/studio/components/grid/utils/gridColumns.tsx
@@ -34,7 +34,7 @@ import {
ForeignKeyFormatter,
} from 'components/grid/components/formatter'
-const ESTIMATED_CHARACTER_PIXEL_WIDTH = 9
+export const ESTIMATED_CHARACTER_PIXEL_WIDTH = 9
export function getGridColumns(
table: SupaTable,
@@ -180,7 +180,7 @@ function getColumnType(columnDef: SupaColumn): ColumnType {
} else return 'unknown'
}
-function getColumnDefaultWidth(columnDef: SupaColumn): number {
+export function getColumnDefaultWidth(columnDef: SupaColumn): number {
if (isNumericalColumn(columnDef.dataType)) {
return 120
} else if (
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
index 3b97ac2add6..472703ee19f 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
@@ -82,17 +82,17 @@ const ForeignRowSelector = ({
return (
<SidePanel
+ key="ForeignRowSelector"
visible={visible}
size="large"
header={
<div>
- Selecting foreign key from{' '}
- <code className="text-sm">
+ Selecting a record to reference from{' '}
+ <code className="font-mono text-sm">
{schemaName}.{tableName}
</code>
</div>
}
- hideFooter={false}
onCancel={closePanel}
customFooter={<ActionBar hideApply backButtonLabel="Close" closePanel={closePanel} />}
>
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/SelectorGrid.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/SelectorGrid.tsx
index 87e7d50106b..2e9e7e5f3d0 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/SelectorGrid.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/SelectorGrid.tsx
@@ -2,6 +2,11 @@ import DataGrid, { Column } from '@supabase/react-data-grid'
import * as Tooltip from '@radix-ui/react-tooltip'
import { SupaRow, SupaTable } from 'components/grid'
import { IconKey } from 'ui'
+import {
+ ESTIMATED_CHARACTER_PIXEL_WIDTH,
+ getColumnDefaultWidth,
+} from 'components/grid/utils/gridColumns'
+import { COLUMN_MIN_WIDTH } from 'components/grid/constants'
export interface SelectorGridProps {
table: SupaTable
@@ -42,14 +47,23 @@ const formatter = (column: string, row: any) => {
}
const SelectorGrid = ({ table, rows, onRowSelect }: SelectorGridProps) => {
- const columns: Column<SupaRow>[] = table.columns.map((column) => ({
- key: column.name,
- name: column.name,
- formatter: ({ row }: any) => formatter(column.name, row),
- headerRenderer: () => columnRender(column.name, column.isPrimaryKey),
- resizable: true,
- minWidth: 120,
- }))
+ const columns: Column<SupaRow>[] = table.columns.map((column) => {
+ const columnDefaultWidth = getColumnDefaultWidth(column)
+ const columnWidthBasedOnName =
+ (column.name.length + column.format.length) * ESTIMATED_CHARACTER_PIXEL_WIDTH
+ const columnWidth =
+ columnDefaultWidth < columnWidthBasedOnName ? columnWidthBasedOnName : columnDefaultWidth
+
+ return {
+ key: column.name,
+ name: column.name,
+ formatter: ({ row }: any) => formatter(column.name, row),
+ headerRenderer: () => columnRender(column.name, column.isPrimaryKey),
+ resizable: true,
+ width: columnWidth,
+ minWidth: COLUMN_MIN_WIDTH,
+ }
+ })
return (
<DataGrid
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/HeaderTitle.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/HeaderTitle.tsx
index b9b2823c7c9..38d4e20d5f3 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/HeaderTitle.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/HeaderTitle.tsx
@@ -11,7 +11,7 @@ const HeaderTitle: FC<Props> = ({ isNewRecord, tableName }) => {
return (
<>
{header}
- {tableName && <span className="text-code">{tableName}</span>}
+ {tableName && <span className="text-code font-mono">{tableName}</span>}
</>
)
}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
index bfab3d84d8d..d16fe723025 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
@@ -128,9 +128,7 @@ const RowEditor = ({
onCancel={closePanel}
onInteractOutside={(event) => {
const isToast = (event.target as Element)?.closest('#toast')
- if (isToast) {
- event.preventDefault()
- }
+ if (isToast) event.preventDefault()
}}
>
<form onSubmit={(e) => onSaveChanges(e)} className="h-full">
@@ -190,17 +188,6 @@ const RowEditor = ({
setSelectedValueForJsonEdit(undefined)
}}
/>
-
- <ForeignRowSelector
- key={`foreign-row-selector-${referenceRow?.name}`}
- visible={isSelectingForeignKey}
- referenceRow={referenceRow}
- onSelect={onSelectForeignRowValue}
- closePanel={() => {
- setIsSelectingForeignKey(false)
- setReferenceRow(undefined)
- }}
- />
</div>
<div className="flex-shrink">
<ActionBar
@@ -212,6 +199,17 @@ const RowEditor = ({
</div>
</div>
</form>
+
+ <ForeignRowSelector
+ key={`foreign-row-selector-${referenceRow?.name}`}
+ visible={isSelectingForeignKey}
+ referenceRow={referenceRow}
+ onSelect={onSelectForeignRowValue}
+ closePanel={() => {
+ setIsSelectingForeignKey(false)
+ setReferenceRow(undefined)
+ }}
+ />
</SidePanel>
)
}