Skip to content

Commit

Permalink
fix(TypeScript): tablieList => item is always ExtendedNode<TableNode>…
Browse files Browse the repository at this point in the history
… instead of TableNode
  • Loading branch information
rwieruch committed Feb 23, 2023
1 parent bf3b1d9 commit 7f6bd3e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 25 deletions.
7 changes: 5 additions & 2 deletions .storybook/stories/Types/data.story.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export type TableNode = {
[prop: string]: any,
};

export type ExtendedTableNode = TableNode & {
ancestors: TableNode[],
export type ExtendedNode<T extends TableNode> = T & {
treeXLevel?: number;
treeYLevel?: number;
parentNode?: ExtendedNode<T> | Nullish;
ancestors?: ExtendedNode<T>[];
};

export type Data = {
Expand Down
4 changes: 2 additions & 2 deletions src/common/util/modifiers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TableNode, Features } from '@table-library/react-table-library/types/table';
import { TableNode, Features, ExtendedNode } from '@table-library/react-table-library/types/table';

export const applyModifiers =
({ sort, pagination, tree }: Features) =>
(nodes: TableNode[]) => {
let modifiedNodes = [...nodes];
let modifiedNodes: ExtendedNode<TableNode>[] = [...nodes];

modifiedNodes = sort ? sort.modifier(modifiedNodes) : modifiedNodes;

Expand Down
26 changes: 11 additions & 15 deletions src/common/util/tree/fromTreeToList.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { Nullish } from '@table-library/react-table-library/types/common';
import { Data, TableNode } from '@table-library/react-table-library/types/table';
import { Data, TableNode, ExtendedNode } from '@table-library/react-table-library/types/table';

import { hasLeaves } from './hasLeaves';

type ExtendedNode = {
treeXLevel: number;
treeYLevel: number;
parentNode: TableNode;
ancestors: TableNode[];
};

export const fromTreeToList = <T extends TableNode>(nodes: T[] | Nullish): T[] =>
(nodes || []).reduce((acc: T[], value: T) => {
acc = acc.concat(value); // eslint-disable-line no-param-reassign
Expand All @@ -27,9 +20,9 @@ export const fromTreeToListExtended = (
treeIds: string[],
treeXLevel = 0,
treeYLevel = 0,
parentNode: (TableNode & ExtendedNode) | Nullish,
): (TableNode & ExtendedNode)[] =>
(nodes || []).reduce((acc: (TableNode & ExtendedNode)[], value: TableNode) => {
parentNode: ExtendedNode<TableNode> | Nullish,
): ExtendedNode<TableNode>[] =>
(nodes || []).reduce((acc: ExtendedNode<TableNode>[], value: TableNode) => {
let listNode;

if (value.nodes) {
Expand All @@ -41,14 +34,17 @@ export const fromTreeToListExtended = (
const extendedNode = {
treeXLevel,
treeYLevel,
parentNode: parentNode || data,
ancestors: parentNode ? [parentNode, ...parentNode.ancestors] : [parentNode || data],
} as ExtendedNode;
// TODO: data needs to be explicitly typed here for this edge case of root
parentNode: (parentNode || data) as ExtendedNode<TableNode>,
ancestors: (parentNode
? [parentNode, ...(parentNode?.ancestors ?? [])]
: [parentNode || data]) as ExtendedNode<TableNode>[],
};

listNode = {
...listNode,
...extendedNode,
} as TableNode & ExtendedNode;
} as ExtendedNode<TableNode>;

acc = acc.concat(listNode); // eslint-disable-line no-param-reassign

Expand Down
4 changes: 2 additions & 2 deletions src/pagination/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSyncControlledState } from '@table-library/react-table-library/commo
import { useSyncRefState } from '@table-library/react-table-library/common/util/useSyncRefState';

import { Action, State, StateAndChange } from '@table-library/react-table-library/types/common';
import { Data, TableNode } from '@table-library/react-table-library/types/table';
import { Data, ExtendedNode, TableNode } from '@table-library/react-table-library/types/table';
import {
Pages,
Pagination,
Expand Down Expand Up @@ -139,7 +139,7 @@ const usePagination = (
getPageBoundaries,
};

const modifier = (nodes: TableNode[]): TableNode[] => {
const modifier = (nodes: TableNode[]): ExtendedNode<TableNode>[] => {
if (mergedOptions.isServer) {
return nodes;
}
Expand Down
4 changes: 2 additions & 2 deletions src/sort/useSort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import IconChevronSingleUp from '@table-library/react-table-library/common/icons
import IconChevronSingleUpDown from '@table-library/react-table-library/common/icons/IconChevronSingleUpDown';

import { Action, State, StateAndChange } from '@table-library/react-table-library/types/common';
import { Data, TableNode } from '@table-library/react-table-library/types/table';
import { Data, ExtendedNode, TableNode } from '@table-library/react-table-library/types/table';
import {
Sort,
SortOptions,
Expand Down Expand Up @@ -163,7 +163,7 @@ const useSort = (

const stateAndGetters = { ...state, sortFn };

const modifier = (nodes: TableNode[]): TableNode[] => {
const modifier = (nodes: TableNode[]): ExtendedNode<TableNode>[] => {
if (mergedOptions.isServer) {
return nodes;
}
Expand Down
3 changes: 2 additions & 1 deletion src/tree/useTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Features,
FeatureProps,
RowProps,
ExtendedNode,
} from '@table-library/react-table-library/types/table';
import {
Tree,
Expand Down Expand Up @@ -120,7 +121,7 @@ const useTree = (
},
};

const modifier = (nodes: TableNode[]): TableNode[] => {
const modifier = (nodes: TableNode[]): ExtendedNode<TableNode>[] => {
if (mergedOptions.isServer) {
return nodes;
}
Expand Down
4 changes: 3 additions & 1 deletion src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExtendedNode, TableNode } from './table';

export type Nullish = null | undefined;

export type Action = {
Expand All @@ -21,7 +23,7 @@ export type StateAndChange = {
onChange?: MiddlewareFunction;
};

export type Modifier = (nodes: any[]) => any[];
export type Modifier = (nodes: TableNode[]) => TableNode[] | ExtendedNode<TableNode>[];

type IdReducerFunctionsOptions = {
isCarryForward?: boolean;
Expand Down
7 changes: 7 additions & 0 deletions src/types/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ export type TableNode = {
[prop: string]: any;
};

export type ExtendedNode<T extends TableNode> = T & {
treeXLevel?: number;
treeYLevel?: number;
parentNode?: ExtendedNode<T> | Nullish;
ancestors?: ExtendedNode<T>[];
};

export type Data = {
pageInfo?: any;
nodes: TableNode[];
Expand Down

0 comments on commit 7f6bd3e

Please sign in to comment.