Skip to content

Commit

Permalink
Merge pull request #930 from 3YOURMIND/gather-table-stare-state-types
Browse files Browse the repository at this point in the history
refact(KtTable): declare table store's state types
  • Loading branch information
Isokaeder authored Jun 10, 2024
2 parents 0776a0b + 9500368 commit f588fdc
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 22 deletions.
16 changes: 10 additions & 6 deletions packages/kotti-ui/source/kotti-table/KtTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ import {
import { KtTableColumn } from './KtTableColumn'
import { TableStore } from './logic/store'
import { TableLayout } from './table-layout'
import type { CreateElement } from 'vue'
import type { CreateElement, PropType } from 'vue'
import type { KottiTable } from './types'
let tableIdSeed = 1
export const INITIAL_TABLE_STORE_PROPS = [
'rowKey',
'sortMultiple',
'expandMultiple',
'filteredColumns',
'hiddenColumns',
'remoteSort',
'rowKey',
'sortable',
'sortedColumns',
'hiddenColumns',
'filteredColumns',
'sortMultiple',
]
export default {
Expand Down Expand Up @@ -80,7 +81,10 @@ export default {
emptyText: { default: 'No Data', type: String },
id: { default: null, type: String },
// FIXME: Props should either be required or have a default
rowKey: { required: false, type: [String, Function] },
rowKey: {
required: false,
type: [String, Function] as PropType<KottiTable.RowKey>,
},
rows: { default: () => [], type: Array },
useColumnDragToOrder: { default: false, type: Boolean },
Expand Down
7 changes: 4 additions & 3 deletions packages/kotti-ui/source/kotti-table/logic/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { setFilteredColumn } from './filter'
import { setHiddenColumn, getResolvedHiddenColumns } from './hide'
import { resolveColumnsOrder, getOrderedColumns } from './order'
import { setSortedColumn } from './sort'
import type { Store } from './types'

export function getColumnRealIndex(state: any, column: any) {
return state._columnsArray.findIndex(({ id }: any) => id == column.id)
Expand Down Expand Up @@ -101,12 +102,12 @@ function didRestoreDestroyedColumns({
)
}

export const defaultState = {
_destroyedColumns: {},
refreshColumnArray: true,
export const defaultState: Store.StateComponents.Column = {
_columns: {},
_columnsArray: [],
_destroyedColumns: {},
columns: [],
refreshColumnArray: true,
}

export const mutations = {
Expand Down
3 changes: 2 additions & 1 deletion packages/kotti-ui/source/kotti-table/logic/disable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import negate from 'lodash/negate'
import type { Store } from './types'

export const defaultState = {
export const defaultState: Store.StateComponents.Disable = {
enabledRows: [],
isAllRowsDisabled: false,
}
Expand Down
3 changes: 2 additions & 1 deletion packages/kotti-ui/source/kotti-table/logic/expand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import isEqual from 'lodash/isEqual'
import type { Store } from './types'

export const defaultState = {
export const defaultState: Store.StateComponents.Expand = {
expandMultiple: false,
expanded: [],
}
Expand Down
3 changes: 2 additions & 1 deletion packages/kotti-ui/source/kotti-table/logic/filter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { setColumnsArray } from './column'
import type { Store } from './types'

function getFilteredColumnIndex(state: any, column: any) {
return state.filteredColumns.findIndex(
({ prop }: any) => prop === column.prop,
)
}

export const defaultState = {
export const defaultState: Store.StateComponents.Filter = {
filteredColumns: [],
}

Expand Down
3 changes: 2 additions & 1 deletion packages/kotti-ui/source/kotti-table/logic/hide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { setColumnsArray } from './column'
import type { Store } from './types'

function getHiddenColumnIndex(state: any, column: any) {
return state.hiddenColumns.findIndex(({ prop }: any) => prop === column.prop)
Expand All @@ -26,7 +27,7 @@ export function setHiddenColumn(state: any, column: any) {
}
}

export const defaultState = {
export const defaultState: Store.StateComponents.Hide = {
hiddenColumns: [],
}

Expand Down
3 changes: 2 additions & 1 deletion packages/kotti-ui/source/kotti-table/logic/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pick from 'lodash/pick'

import { setColumnsArray, getColumnIndex, getColumnRealIndex } from './column'
import type { Store } from './types'

function byOrder(a: any, b: any) {
return a.order < b.order
Expand Down Expand Up @@ -48,7 +49,7 @@ export function resolveColumnsOrder({
})
}

export const defaultState = {
export const defaultState: Store.StateComponents.Order = {
orderedColumns: [],
}

Expand Down
3 changes: 2 additions & 1 deletion packages/kotti-ui/source/kotti-table/logic/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { updateAllSelected, cleanSelection, clearSelection } from './select'
import { sortData } from './sort'
import type { Store } from './types'

export const defaultState = {
export const defaultState: Store.StateComponents.Row = {
_data: [],
filteredData: null,
focusedRow: null,
Expand Down
5 changes: 3 additions & 2 deletions packages/kotti-ui/source/kotti-table/logic/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import debounce from 'lodash/debounce'
import isEqual from 'lodash/isEqual'
import type { Store } from './types'

function toggleRowSelection(state: any, row: any, selected: any) {
let changed = false
Expand Down Expand Up @@ -64,10 +65,10 @@ export function clearSelection(store: any) {
}
}

export const defaultState = {
selection: [],
export const defaultState: Store.StateComponents.Select = {
isAllSelected: false,
reserveSelection: false,
selection: [],
}

export const mutations = {
Expand Down
5 changes: 3 additions & 2 deletions packages/kotti-ui/source/kotti-table/logic/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IS_ASC, IS_DSC, PUBLIC_SORT_PROPS } from '../constants'
import { KottiTable } from '../types'

import { setColumnsArray } from './column'
import type { Store } from './types'

function getSortedColumnIndex(state: any, column: any) {
return state.sortedColumns.findIndex(({ prop }: any) => prop === column.prop)
Expand Down Expand Up @@ -113,11 +114,11 @@ export function sortData(data: any, state: any) {
return orderBy(data, state.sortedColumns)
}

export const defaultState = {
sortMultiple: false,
export const defaultState: Store.StateComponents.Sort = {
remoteSort: false,
sortable: false,
sortedColumns: [],
sortMultiple: false,
}

export const mutations = {
Expand Down
7 changes: 4 additions & 3 deletions packages/kotti-ui/source/kotti-table/logic/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import * as order from './order'
import * as rows from './row'
import * as select from './select'
import * as sort from './sort'
import type { Store } from './types'

export class TableStore {
getters: any
mutations: any
state: any
state: Store.State
table: any
id: string | undefined

constructor(table: any, initialState: any = {}) {
constructor(table: any, initialState: Partial<Store.State> = {}) {
this.setTable(table)
// we deep clone initial state in order to not refer to the same objects
this.state = cloneDeep({
Expand Down Expand Up @@ -67,7 +68,7 @@ export class TableStore {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
this.id = table.tableId + '_store'
}
setInitialState(initialState: any) {
setInitialState(initialState: Partial<Store.State>) {
this.state = cloneDeep({
...this.state,
...pickBy(initialState, negate(isUndefined)),
Expand Down
79 changes: 79 additions & 0 deletions packages/kotti-ui/source/kotti-table/logic/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { Kotti } from '../../types'

export module Store {
export module StateComponents {
export type Column = {
_columns: any
_columnsArray: any
_destroyedColumns: Record<string, number>
columns: any
refreshColumnArray: boolean
}

export type Disable = {
enabledRows: any
isAllRowsDisabled: boolean
}

export type Expand = {
expanded: any
expandMultiple: boolean
}

export type Filter = {
filteredColumns: any
}

export type Hide = {
hiddenColumns: any
}

export type Order = {
orderedColumns: any
}

export type Row = {
_data: any
filteredData: any
focusedRow: any
rows: any
}

export type Select = {
isAllSelected: boolean
reserveSelection: boolean
selection: any
}

export type Sort = {
remoteSort: boolean
sortable: boolean | string
sortedColumns: any
sortMultiple: boolean
}
}

export type State = StateComponents.Column &
StateComponents.Disable &
StateComponents.Expand &
StateComponents.Filter &
StateComponents.Hide &
StateComponents.Order &
StateComponents.Row &
StateComponents.Select &
StateComponents.Sort & {
/**
* This is passed to state exclusively via `setInitialState` and thus is not defined
* in any of the above objects.
*
* Note: rowKey is right now not a required prop of KtTable, but from what I can see
* the store either depends on its existence or at least has better performance if
* it exists.
*
* TODO: investigate if rowKey should be a required prop.
*/
rowKey?: Kotti.Table.RowKey
}
}
2 changes: 2 additions & 0 deletions packages/kotti-ui/source/kotti-table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export module KottiTable {
}
}

export type RowKey = string | ((row: Row.Props) => string | number)

export type Props = {
columns: Column.Props[] | null
emptyText: string
Expand Down

0 comments on commit f588fdc

Please sign in to comment.