From b0e58587680343ea93b15f66307d69463dc9523f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 27 Dec 2024 08:57:00 +0100 Subject: [PATCH] Type grid utils --- .../src/components/grid/lib/{utils.js => utils.ts} | 8 ++++---- .../frontend-core/src/components/grid/stores/menu.ts | 2 +- .../frontend-core/src/components/grid/stores/ui.ts | 12 ++++++++---- .../src/components/grid/stores/validation.ts | 8 +++++--- 4 files changed, 18 insertions(+), 12 deletions(-) rename packages/frontend-core/src/components/grid/lib/{utils.js => utils.ts} (71%) diff --git a/packages/frontend-core/src/components/grid/lib/utils.js b/packages/frontend-core/src/components/grid/lib/utils.ts similarity index 71% rename from packages/frontend-core/src/components/grid/lib/utils.js rename to packages/frontend-core/src/components/grid/lib/utils.ts index f1f33d9950..14aee663b7 100644 --- a/packages/frontend-core/src/components/grid/lib/utils.js +++ b/packages/frontend-core/src/components/grid/lib/utils.ts @@ -1,7 +1,7 @@ import { GeneratedIDPrefix, CellIDSeparator } from "./constants" import { Helpers } from "@budibase/bbui" -export const parseCellID = cellId => { +export const parseCellID = (cellId: string | null) => { if (!cellId) { return { rowId: undefined, field: undefined } } @@ -10,11 +10,11 @@ export const parseCellID = cellId => { return { rowId: parts.join(CellIDSeparator), field } } -export const getCellID = (rowId, fieldName) => { +export const getCellID = (rowId: string, fieldName: string) => { return `${rowId}${CellIDSeparator}${fieldName}` } -export const parseEventLocation = e => { +export const parseEventLocation = (e: MouseEvent & TouchEvent) => { return { x: e.clientX ?? e.touches?.[0]?.clientX, y: e.clientY ?? e.touches?.[0]?.clientY, @@ -25,6 +25,6 @@ export const generateRowID = () => { return `${GeneratedIDPrefix}${Helpers.uuid()}` } -export const isGeneratedRowID = id => { +export const isGeneratedRowID = (id: string) => { return id?.startsWith(GeneratedIDPrefix) } diff --git a/packages/frontend-core/src/components/grid/stores/menu.ts b/packages/frontend-core/src/components/grid/stores/menu.ts index 77ce2c6667..ae29ae1de3 100644 --- a/packages/frontend-core/src/components/grid/stores/menu.ts +++ b/packages/frontend-core/src/components/grid/stores/menu.ts @@ -69,7 +69,7 @@ export const createActions = (context: StoreContext): MenuActions => { let multiRowMode = false if (get(selectedRowCount) > 1) { const { rowId } = parseCellID(cellId) - if (get(selectedRows)[rowId]) { + if (rowId !== undefined && get(selectedRows)[rowId]) { multiRowMode = true } } diff --git a/packages/frontend-core/src/components/grid/stores/ui.ts b/packages/frontend-core/src/components/grid/stores/ui.ts index 78a0b9c2b0..3378d34c6a 100644 --- a/packages/frontend-core/src/components/grid/stores/ui.ts +++ b/packages/frontend-core/src/components/grid/stores/ui.ts @@ -101,6 +101,10 @@ export const deriveStores = (context: StoreContext) => { const focusedRow = derived( [focusedRowId, rowLookupMap], ([$focusedRowId, $rowLookupMap]) => { + if ($focusedRowId === undefined) { + return + } + if ($focusedRowId === NewRowID) { return { _id: NewRowID } } @@ -152,8 +156,8 @@ export const deriveStores = (context: StoreContext) => { } // Row indices - const sourceRowIndex = $rowLookupMap[sourceInfo.rowId]?.__idx - const targetRowIndex = $rowLookupMap[targetInfo.rowId]?.__idx + const sourceRowIndex = $rowLookupMap[sourceInfo.rowId!]?.__idx + const targetRowIndex = $rowLookupMap[targetInfo.rowId!]?.__idx if (sourceRowIndex == null || targetRowIndex == null) { return [] } @@ -164,8 +168,8 @@ export const deriveStores = (context: StoreContext) => { upperRowIndex = Math.min(upperRowIndex, lowerRowIndex + 49) // Column indices - const sourceColIndex = $columnLookupMap[sourceInfo.field].__idx || 0 - const targetColIndex = $columnLookupMap[targetInfo.field].__idx || 0 + const sourceColIndex = $columnLookupMap[sourceInfo.field!].__idx || 0 + const targetColIndex = $columnLookupMap[targetInfo.field!].__idx || 0 const lowerColIndex = Math.min(sourceColIndex, targetColIndex) const upperColIndex = Math.max(sourceColIndex, targetColIndex) diff --git a/packages/frontend-core/src/components/grid/stores/validation.ts b/packages/frontend-core/src/components/grid/stores/validation.ts index 32bb1cf978..f118020b16 100644 --- a/packages/frontend-core/src/components/grid/stores/validation.ts +++ b/packages/frontend-core/src/components/grid/stores/validation.ts @@ -33,10 +33,12 @@ export const deriveStores = (context: StoreContext): DerivedValidationStore => { // Extract row ID from all errored cell IDs if (error) { const { rowId } = parseCellID(key) - if (!map[rowId]) { - map[rowId] = [] + if (rowId !== undefined) { + if (!map[rowId]) { + map[rowId] = [] + } + map[rowId].push(key) } - map[rowId].push(key) } }) return map