Merge pull request #15228 from Budibase/typing/stores-grid-columns

Typing grid columns store
This commit is contained in:
Adria Navarro 2024-12-23 08:55:58 +01:00 committed by GitHub
commit c0072f0f96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 78 additions and 18 deletions

View File

@ -1,6 +1,7 @@
{ {
"extends": "./tsconfig.build.json", "extends": "./tsconfig.build.json",
"compilerOptions": { "compilerOptions": {
"outDir": "./dist",
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"assets/*": ["./assets/*"], "assets/*": ["./assets/*"],

View File

@ -1,8 +1,25 @@
import { derived, get, writable } from "svelte/store" import { derived, get, Readable, Writable, writable } from "svelte/store"
import { DefaultColumnWidth, GutterWidth } from "../lib/constants" import { DefaultColumnWidth, GutterWidth } from "../lib/constants"
import { UIColumn } from "@budibase/types"
import { Store as StoreContext } from "."
export const createStores = () => { interface ColumnStore {
const columns = writable([]) columns: Writable<UIColumn[]>
}
interface DerivedColumnStore {
tableColumns: Readable<UIColumn[]>
displayColumn: Readable<UIColumn | undefined>
columnLookupMap: Readable<Record<string, UIColumn>>
visibleColumns: Readable<UIColumn[]>
scrollableColumns: Readable<UIColumn[]>
hasNonAutoColumn: Readable<boolean>
}
export type Store = ColumnStore & DerivedColumnStore
export const createStores = (): ColumnStore => {
const columns = writable<UIColumn[]>([])
// Enrich columns with metadata about their display position // Enrich columns with metadata about their display position
const enrichedColumns = derived(columns, $columns => { const enrichedColumns = derived(columns, $columns => {
@ -16,7 +33,7 @@ export const createStores = () => {
} }
if (col.visible) { if (col.visible) {
idx++ idx++
offset += col.width offset += col.width ?? 0
} }
return enriched return enriched
}) })
@ -30,12 +47,12 @@ export const createStores = () => {
} }
} }
export const deriveStores = context => { export const deriveStores = (context: StoreContext): DerivedColumnStore => {
const { columns } = context const { columns } = context
// Derive a lookup map for all columns by name // Derive a lookup map for all columns by name
const columnLookupMap = derived(columns, $columns => { const columnLookupMap = derived(columns, $columns => {
let map = {} let map: Record<string, UIColumn> = {}
$columns.forEach(column => { $columns.forEach(column => {
map[column.name] = column map[column.name] = column
}) })
@ -78,11 +95,11 @@ export const deriveStores = context => {
} }
} }
export const createActions = context => { export const createActions = (context: StoreContext) => {
const { columns, datasource } = context const { columns, datasource } = context
// Updates the width of all columns // Updates the width of all columns
const changeAllColumnWidths = async width => { const changeAllColumnWidths = async (width: number) => {
const $columns = get(columns) const $columns = get(columns)
$columns.forEach(column => { $columns.forEach(column => {
const { related } = column const { related } = column
@ -101,7 +118,7 @@ export const createActions = context => {
} }
// Checks if a column is readonly // Checks if a column is readonly
const isReadonly = column => { const isReadonly = (column: UIColumn) => {
if (!column?.schema) { if (!column?.schema) {
return false return false
} }
@ -125,11 +142,11 @@ export const createActions = context => {
} }
} }
export const initialise = context => { export const initialise = (context: StoreContext) => {
const { definition, columns, displayColumn, enrichedSchema } = context const { definition, columns, displayColumn, enrichedSchema } = context
// Merge new schema fields with existing schema in order to preserve widths // Merge new schema fields with existing schema in order to preserve widths
const processColumns = $enrichedSchema => { const processColumns = ($enrichedSchema: any) => {
if (!$enrichedSchema) { if (!$enrichedSchema) {
columns.set([]) columns.set([])
return return
@ -139,7 +156,7 @@ export const initialise = context => {
const $displayColumn = get(displayColumn) const $displayColumn = get(displayColumn)
// Find primary display // Find primary display
let primaryDisplay let primaryDisplay: string
const candidatePD = $definition.primaryDisplay || $displayColumn?.name const candidatePD = $definition.primaryDisplay || $displayColumn?.name
if (candidatePD && $enrichedSchema[candidatePD]) { if (candidatePD && $enrichedSchema[candidatePD]) {
primaryDisplay = candidatePD primaryDisplay = candidatePD
@ -151,7 +168,8 @@ export const initialise = context => {
.map(field => { .map(field => {
const fieldSchema = $enrichedSchema[field] const fieldSchema = $enrichedSchema[field]
const oldColumn = $columns?.find(col => col.name === field) const oldColumn = $columns?.find(col => col.name === field)
const column = { const column: UIColumn = {
type: fieldSchema.type,
name: field, name: field,
label: fieldSchema.displayName || field, label: fieldSchema.displayName || field,
schema: fieldSchema, schema: fieldSchema,

View File

@ -1,3 +1,5 @@
import { Writable } from "svelte/store"
import * as Bounds from "./bounds" import * as Bounds from "./bounds"
import * as Columns from "./columns" import * as Columns from "./columns"
import * as Menu from "./menu" import * as Menu from "./menu"
@ -48,25 +50,43 @@ const DependencyOrderedStores = [
Cache, Cache,
] ]
export const attachStores = context => { export interface BaseStore {}
export type Store = BaseStore &
Columns.Store & {
// TODO while typing the rest of stores
datasource: any
definition: Writable<any>
enrichedSchema: any
}
export const attachStores = (context: Store): Store => {
// Atomic store creation // Atomic store creation
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
context = { ...context, ...store.createStores?.(context) } if ("createStores" in store) {
context = { ...context, ...store.createStores?.(context) }
}
} }
// Derived store creation // Derived store creation
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
context = { ...context, ...store.deriveStores?.(context) } if ("deriveStores" in store) {
context = { ...context, ...store.deriveStores?.(context) }
}
} }
// Action creation // Action creation
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
context = { ...context, ...store.createActions?.(context) } if ("createActions" in store) {
context = { ...context, ...store.createActions?.(context) }
}
} }
// Initialise any store logic // Initialise any store logic
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
store.initialise?.(context) if ("initialise" in store) {
store.initialise?.(context)
}
} }
return context return context

View File

@ -0,0 +1,19 @@
import { CalculationType, FieldSchema, FieldType } from "@budibase/types"
export type UIColumn = FieldSchema & {
label: string
readonly: boolean
conditions: any
related?: {
field: string
subField: string
}
primaryDisplay?: boolean
schema?: {
disabled: boolean
type: FieldType
readonly: boolean
autocolumn: boolean
}
calculationType: CalculationType
}

View File

@ -0,0 +1 @@
export * from "./columns"

View File

@ -1 +1,2 @@
export * from "./integration" export * from "./integration"
export * from "./grid"