Skip to content

Commit

Permalink
fixed missing generics, added default constraints for all generics
Browse files Browse the repository at this point in the history
  • Loading branch information
mreid21 committed Nov 4, 2024
1 parent 3d8b191 commit b3cc21b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
10 changes: 4 additions & 6 deletions src/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ export interface ITreeViewProps<M extends IFlatMetadata = IFlatMetadata> {
focusedId?: NodeId;
}

function TreeViewInner<M extends IFlatMetadata>(
const TreeView = React.forwardRef(
function TreeView<M extends IFlatMetadata = IFlatMetadata>(
{
data,
selectedIds,
Expand Down Expand Up @@ -670,12 +671,9 @@ function TreeViewInner<M extends IFlatMetadata>(
))}
</ul>
)
}
})


const TreeView = React.forwardRef(TreeViewInner) as <T extends IFlatMetadata>(
props: ITreeViewProps<T> & React.RefAttributes<T>
) => ReturnType<typeof TreeViewInner>;



Expand Down Expand Up @@ -981,7 +979,7 @@ const handleKeyDown = ({
}
};

//@ts-expect-error type assertion for ts

TreeView.propTypes = {
/** Tree data*/
data: PropTypes.array.isRequired,
Expand Down
10 changes: 5 additions & 5 deletions src/TreeView/node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "./utils";
import { baseClassNames, clickActions, treeTypes } from "./constants";

export interface INodeProps<M extends IFlatMetadata> {
export interface INodeProps<M extends IFlatMetadata = IFlatMetadata> {
element: INode<M>;
dispatch: React.Dispatch<TreeViewAction>;
data: INode<M>[];
Expand Down Expand Up @@ -54,7 +54,7 @@ export interface INodeProps<M extends IFlatMetadata> {
propagateSelectUpwards: boolean;
}

export interface INodeGroupProps<M extends IFlatMetadata>
export interface INodeGroupProps<M extends IFlatMetadata = IFlatMetadata>
extends Omit<INodeProps<M>, "setsize" | "posinset"> {
getClasses: (className: string) => string;
/** don't send this. The NodeGroup render function, determines it for you */
Expand All @@ -66,15 +66,15 @@ export interface INodeGroupProps<M extends IFlatMetadata>
/**
* It's convenient to pass props down to the child, but we don't want to pass everything since it would create incorrect values for setsize and posinset
*/
const removeIrrelevantGroupProps = <M extends IFlatMetadata,>(
const removeIrrelevantGroupProps = <M extends IFlatMetadata = IFlatMetadata,>(
nodeProps: INodeProps<M>
): Omit<INodeGroupProps<M>, "getClasses"> => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { setsize, posinset, ...rest } = nodeProps;
return rest;
};

export const Node = <M extends IFlatMetadata>(props: INodeProps<M>) => {
export const Node = <M extends IFlatMetadata = IFlatMetadata>(props: INodeProps<M>) => {
const {
element,
dispatch,
Expand Down Expand Up @@ -330,7 +330,7 @@ export const NodeGroup = <M extends IFlatMetadata = IFlatMetadata>({
expandedIds={expandedIds}
baseClassNames={baseClassNames}
key={`${x}-${typeof x}`}
element={getTreeNode(data, x) as INode<M>}
element={getTreeNode(data, x)}
setsize={element.children.length}
posinset={index + 1}
level={level + 1}
Expand Down
2 changes: 1 addition & 1 deletion src/TreeView/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type EventCallback = <T, E>(
metadata?: M;
}

export interface INodeRendererProps<M extends IFlatMetadata> {
export interface INodeRendererProps<M extends IFlatMetadata = IFlatMetadata> {
/** The object that represents the rendered node */
element: INode<M>;
/** A function which gives back the props to pass to the node */
Expand Down
10 changes: 5 additions & 5 deletions src/TreeView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ export type IFlatMetadata = Record<
string | number | boolean | undefined | null
>;

interface ITreeNode<M extends IFlatMetadata> {
interface ITreeNode<M extends IFlatMetadata = IFlatMetadata> {
id?: NodeId;
name: string;
isBranch?: boolean;
children?: ITreeNode<M>[];
metadata?: M;
}

export const flattenTree = <M extends IFlatMetadata>(
export const flattenTree = <M extends IFlatMetadata = IFlatMetadata>(
tree: ITreeNode<M>
): INode<M>[] => {
let internalCount = 0;
Expand Down Expand Up @@ -515,8 +515,8 @@ export const getOnSelectTreeAction = (
return treeTypes.toggleSelect;
};

export const getTreeParent = (data: INode[]): INode => {
const parentNode: INode | undefined = data.find(
export const getTreeParent = <M extends IFlatMetadata = IFlatMetadata>(data: INode<M>[]): INode<M> => {
const parentNode: INode<M> | undefined = data.find(
(node) => node.parent === null
);

Expand All @@ -527,7 +527,7 @@ export const getTreeParent = (data: INode[]): INode => {
return parentNode;
};

export const getTreeNode = (data: INode[], id: NodeId): INode => {
export const getTreeNode = <M extends IFlatMetadata = IFlatMetadata>(data: INode<M>[], id: NodeId): INode<M> => {
const treeNode = data.find((node) => node.id === id);

if (treeNode == null) {
Expand Down

0 comments on commit b3cc21b

Please sign in to comment.