Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
feat: added types to resolve issue with useTable hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nc1337 committed Apr 15, 2023
1 parent 26f2510 commit 8383464
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 10 deletions.
7 changes: 4 additions & 3 deletions frontend/src/components/dbfragments/jsontable/jsontable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ const JsonTable = ({ queryData, dbConnection, mName, isInteractive, showHeader,
rows,
prepareRow,
state,
} = useTable<any>({
} = useTable({
columns,
data,
defaultColumn,
initialState: { selectedRowIds : {}},
...{ editingCellIndex, startEditing, onSaveCell }
}, useRowSelect, hooks => {
if (isInteractive && isEditing)
Expand All @@ -127,7 +128,7 @@ const JsonTable = ({ queryData, dbConnection, mName, isInteractive, showHeader,

const newState: any = state // temporary typescript hack
const selectedRows: number[] = Object.keys(newState.selectedRowIds).map(x => parseInt(x))
const selectedUnderscoreIDs = rows.filter((_, i) => selectedRows.includes(i)).map(x => x.original['_id']).filter(x => x)
const selectedUnderscoreIDs = rows.filter((_, i) => selectedRows.includes(i)).map(x => (x.original as any)['_id']).filter(x => x)

const deleteRows = async () => {
if (selectedUnderscoreIDs.length > 0) {
Expand Down Expand Up @@ -275,4 +276,4 @@ const CellSelectionComponent = ({ row }: any) => (
</div>
)

export default JsonTable
export default JsonTable
13 changes: 6 additions & 7 deletions frontend/src/components/dbfragments/table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isInteractive, showHea
rows,
prepareRow,
state,
} = useTable<any>({
} = useTable({
columns,
data,
defaultColumn,
initialState: { selectedRowIds: {}},
...{ editCell, resetEditCell, onSaveCell }
},
useRowSelect,
Expand All @@ -121,12 +122,10 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isInteractive, showHea
}
)

const newState: any = state // temporary typescript hack
const selectedRowIds: any = newState.selectedRowIds
const selectedRows: number[] = Object.keys(selectedRowIds).map(x => parseInt(x))
const selectedRows: number[] = Object.keys(state.selectedRowIds).map(x => parseInt(x))
const selectedIDs = dbConnection.type === DBConnType.POSTGRES ?
rows.filter((_, i) => selectedRows.includes(i)).map(x => x.original['0']).filter(x => x)
: rows.filter((_, i) => selectedRows.includes(i)).map(x => queryData.pkeys!.map((pkey) => ({ [pkey]: x.original[queryData.columns.findIndex(x => x === pkey)] }))).map(x => x.reduce(((r, c) => Object.assign(r, c)), {})).map(x => JSON.stringify(x))
rows.filter((_, i) => selectedRows.includes(i)).map(x => (x.original as any)['0']).filter(x => x)
: rows.filter((_, i) => selectedRows.includes(i)).map(x => queryData.pkeys!.map((pkey) => ({ [pkey]: (x.original as any)[queryData.columns.findIndex(x => x === pkey)] }))).map(x => x.reduce(((r, c) => Object.assign(r, c)), {})).map(x => JSON.stringify(x))

const deleteRows = async () => {
if (dbConnection.type === DBConnType.MYSQL && queryData.pkeys?.length === 0) {
Expand Down Expand Up @@ -353,4 +352,4 @@ const CellSelectionComponent = ({ row }: any) => (
)


export default Table
export default Table
130 changes: 130 additions & 0 deletions frontend/src/types/react-table-config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import {
UseColumnOrderInstanceProps,
UseColumnOrderState,
UseExpandedHooks,
UseExpandedInstanceProps,
UseExpandedOptions,
UseExpandedRowProps,
UseExpandedState,
UseFiltersColumnOptions,
UseFiltersColumnProps,
UseFiltersInstanceProps,
UseFiltersOptions,
UseFiltersState,
UseGlobalFiltersColumnOptions,
UseGlobalFiltersInstanceProps,
UseGlobalFiltersOptions,
UseGlobalFiltersState,
UseGroupByCellProps,
UseGroupByColumnOptions,
UseGroupByColumnProps,
UseGroupByHooks,
UseGroupByInstanceProps,
UseGroupByOptions,
UseGroupByRowProps,
UseGroupByState,
UsePaginationInstanceProps,
UsePaginationOptions,
UsePaginationState,
UseResizeColumnsColumnOptions,
UseResizeColumnsColumnProps,
UseResizeColumnsOptions,
UseResizeColumnsState,
UseRowSelectHooks,
UseRowSelectInstanceProps,
UseRowSelectOptions,
UseRowSelectRowProps,
UseRowSelectState,
UseRowStateCellProps,
UseRowStateInstanceProps,
UseRowStateOptions,
UseRowStateRowProps,
UseRowStateState,
UseSortByColumnOptions,
UseTableRowProps,
UseSortByColumnProps,
UseSortByHooks,
UseSortByInstanceProps,
UseSortByOptions,
UseSortByState,
} from "react-table";

declare module "react-table" {
// take this file as-is, or comment out the sections that don't apply to your plugin configuration

export interface TableOptions<D extends Record<string, unknown>>
extends UseExpandedOptions<D>,
UseFiltersOptions<D>,
UseGlobalFiltersOptions<D>,
UseGroupByOptions<D>,
UsePaginationOptions<D>,
UseResizeColumnsOptions<D>,
UseRowSelectOptions<D>,
UseRowStateOptions<D>,
UseSortByOptions<D>,
// note that having Record here allows you to add anything to the options, this matches the spirit of the
// underlying js library, but might be cleaner if it's replaced by a more specific type that matches your
// feature set, this is a safe default.
Record<string, any> {}

export interface Hooks<
D extends Record<string, unknown> = Record<string, unknown>
> extends UseExpandedHooks<D>,
UseGroupByHooks<D>,
UseRowSelectHooks<D>,
UseSortByHooks<D> {}

export interface TableInstance<
D extends Record<string, unknown> = Record<string, unknown>
> extends UseColumnOrderInstanceProps<D>,
UseExpandedInstanceProps<D>,
UseFiltersInstanceProps<D>,
UseGlobalFiltersInstanceProps<D>,
UseGroupByInstanceProps<D>,
UsePaginationInstanceProps<D>,
UseRowSelectInstanceProps<D>,
UseRowStateInstanceProps<D>,
UseSortByInstanceProps<D> {}

export interface TableState<
D extends Record<string, unknown> = Record<string, unknown>
> extends UseColumnOrderState<D>,
UseExpandedState<D>,
UseFiltersState<D>,
UseGlobalFiltersState<D>,
UseGroupByState<D>,
UsePaginationState<D>,
UseResizeColumnsState<D>,
UseRowSelectState<D>,
UseRowStateState<D>,
UseSortByState<D> {}

export interface ColumnInterface<
D extends Record<string, unknown> = Record<string, unknown>
> extends UseFiltersColumnOptions<D>,
UseGlobalFiltersColumnOptions<D>,
UseGroupByColumnOptions<D>,
UseResizeColumnsColumnOptions<D>,
UseSortByColumnOptions<D> {}

export interface ColumnInstance<
D extends Record<string, unknown> = Record<string, unknown>
> extends UseFiltersColumnProps<D>,
UseGroupByColumnProps<D>,
UseResizeColumnsColumnProps<D>,
UseSortByColumnProps<D> {}

export interface Cell<
D extends Record<string, unknown> = Record<string, unknown>,
V = any
> extends UseGroupByCellProps<D>,
UseRowStateCellProps<D> {}

export interface Row<
D extends Record<string, unknown> = Record<string, unknown>
> extends UseExpandedRowProps<D>,
UseGroupByRowProps<D>,
UseRowSelectRowProps<D>,
UseRowStateRowProps<D>,
UseTableRowProps<D> {}
}

0 comments on commit 8383464

Please sign in to comment.