From eec8a4d28946b5b6fcf751f76631cebcb62ec022 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 3 May 2024 15:55:02 +0100 Subject: [PATCH] Revert to use schema as source of truth for width, and use new technique to handle width overrides while still allowing customisation by users --- packages/client/manifest.json | 2 +- .../src/components/app/GridBlock.svelte | 21 +++++++++++++++---- .../src/components/grid/stores/columns.js | 2 +- .../src/components/grid/stores/resize.js | 4 +++- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/client/manifest.json b/packages/client/manifest.json index 3427220952..dff774462f 100644 --- a/packages/client/manifest.json +++ b/packages/client/manifest.json @@ -2749,7 +2749,7 @@ }, { "type": "number", - "label": "Width", + "label": "Initial width", "key": "width", "placeholder": "Auto" } diff --git a/packages/client/src/components/app/GridBlock.svelte b/packages/client/src/components/app/GridBlock.svelte index 27bb351f1a..5b3969de4f 100644 --- a/packages/client/src/components/app/GridBlock.svelte +++ b/packages/client/src/components/app/GridBlock.svelte @@ -36,13 +36,14 @@ } = getContext("sdk") let grid + let resizedColumns = {} $: columnWhitelist = parsedColumns ?.filter(col => col.active) ?.map(col => col.field) - $: schemaOverrides = getSchemaOverrides(parsedColumns) $: enrichedButtons = enrichButtons(buttons) $: parsedColumns = getParsedColumns(columns) + $: schemaOverrides = getSchemaOverrides(parsedColumns, resizedColumns) $: actions = [ { type: ActionTypes.RefreshDatasource, @@ -72,7 +73,6 @@ if (columns?.length && columns[0]?.active !== undefined) { return columns } - return columns?.map(column => ({ label: column.displayName || column.name, field: column.name, @@ -80,12 +80,17 @@ })) } - const getSchemaOverrides = columns => { + const getSchemaOverrides = (columns, resizedColumns) => { let overrides = {} columns?.forEach(column => { overrides[column.field] = { displayName: column.label, - width: column.width, + } + + // Only use the specified width until we resize the column, at which point + // we no longer want to override it + if (!resizedColumns[column.field]) { + overrides[column.field].width = column.width } }) return overrides @@ -119,6 +124,13 @@ }, } } + + const onColumnResize = e => { + // Mark that we've resized this column so we can remove this width from + // schema overrides if present + const { column } = e.detail + resizedColumns = { ...resizedColumns, [column]: true } + }
@@ -148,6 +160,7 @@ notifyError={notificationStore.actions.error} buttons={enrichedButtons} on:rowclick={e => onRowClick?.({ row: e.detail })} + on:columnresize={onColumnResize} /> diff --git a/packages/frontend-core/src/components/grid/stores/columns.js b/packages/frontend-core/src/components/grid/stores/columns.js index f463348a1f..8ceaae105f 100644 --- a/packages/frontend-core/src/components/grid/stores/columns.js +++ b/packages/frontend-core/src/components/grid/stores/columns.js @@ -194,7 +194,7 @@ export const initialise = context => { name: field, label: fieldSchema.displayName || field, schema: fieldSchema, - width: oldColumn?.width || fieldSchema.width || DefaultColumnWidth, + width: fieldSchema.width || oldColumn?.width || DefaultColumnWidth, visible: fieldSchema.visible ?? true, order: fieldSchema.order ?? oldColumn?.order, primaryDisplay: field === primaryDisplay, diff --git a/packages/frontend-core/src/components/grid/stores/resize.js b/packages/frontend-core/src/components/grid/stores/resize.js index 157465e838..38ce16740e 100644 --- a/packages/frontend-core/src/components/grid/stores/resize.js +++ b/packages/frontend-core/src/components/grid/stores/resize.js @@ -21,7 +21,7 @@ export const createStores = () => { } export const createActions = context => { - const { resize, columns, stickyColumn, ui } = context + const { resize, columns, stickyColumn, ui, dispatch } = context // Starts resizing a certain column const startResizing = (column, e) => { @@ -102,6 +102,8 @@ export const createActions = context => { // Persist width if it changed if ($resize.width !== $resize.initialWidth) { await columns.actions.saveChanges() + const { column, width } = $resize + dispatch("columnresize", { column, width }) } }