Skip to content

Commit

Permalink
fixed missing generics on node
Browse files Browse the repository at this point in the history
  • Loading branch information
mreid21 committed Oct 22, 2024
1 parent e2f0e0d commit 3d8b191
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export interface ITreeViewOnLoadDataProps {
treeState: ITreeViewState;
}

export interface ITreeViewProps<M extends IFlatMetadata> {
export interface ITreeViewProps<M extends IFlatMetadata = IFlatMetadata> {
/** Tree data*/
data: INode<M>[];
/** Function called when a node changes its selected state */
Expand Down Expand Up @@ -648,7 +648,7 @@ function TreeViewInner<M extends IFlatMetadata>(
<Node
key={`${x}-${typeof x}`}
data={data}
element={getTreeNode(data, x)}
element={getTreeNode(data, x) as INode<M>}
setsize={getTreeParent(data).children.length}
posinset={index + 1}
level={1}
Expand Down Expand Up @@ -981,7 +981,7 @@ const handleKeyDown = ({
}
};

//@ts-expect-error
//@ts-expect-error type assertion for ts
TreeView.propTypes = {
/** Tree data*/
data: PropTypes.array.isRequired,
Expand Down
27 changes: 14 additions & 13 deletions src/TreeView/node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import {
noop,
propagatedIds,
getOnSelectTreeAction,
IFlatMetadata,
} from "./utils";
import { baseClassNames, clickActions, treeTypes } from "./constants";

export interface INodeProps {
element: INode;
export interface INodeProps<M extends IFlatMetadata> {
element: INode<M>;
dispatch: React.Dispatch<TreeViewAction>;
data: INode[];
data: INode<M>[];
nodeAction: NodeAction;
selectedIds: Set<NodeId>;
tabbableId: NodeId;
Expand All @@ -40,7 +41,7 @@ export interface INodeProps {
nodeRefs: INodeRefs;
leafRefs: INodeRefs;
baseClassNames: typeof baseClassNames;
nodeRenderer: (props: INodeRendererProps) => React.ReactNode;
nodeRenderer: (props: INodeRendererProps<M>) => React.ReactNode;
setsize: number;
posinset: number;
level: number;
Expand All @@ -53,8 +54,8 @@ export interface INodeProps {
propagateSelectUpwards: boolean;
}

export interface INodeGroupProps
extends Omit<INodeProps, "setsize" | "posinset"> {
export interface INodeGroupProps<M extends IFlatMetadata>
extends Omit<INodeProps<M>, "setsize" | "posinset"> {
getClasses: (className: string) => string;
/** don't send this. The NodeGroup render function, determines it for you */
setsize?: undefined;
Expand All @@ -65,15 +66,15 @@ export interface INodeGroupProps
/**
* 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 = (
nodeProps: INodeProps
): Omit<INodeGroupProps, "getClasses"> => {
const removeIrrelevantGroupProps = <M extends 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 = (props: INodeProps) => {
export const Node = <M extends IFlatMetadata>(props: INodeProps<M>) => {
const {
element,
dispatch,
Expand Down Expand Up @@ -311,15 +312,15 @@ export const Node = (props: INodeProps) => {
);
};

export const NodeGroup = ({
export const NodeGroup = <M extends IFlatMetadata = IFlatMetadata>({
data,
element,
expandedIds,
getClasses,
baseClassNames,
level,
...rest
}: INodeGroupProps) => (
}: INodeGroupProps<M>) => (
<ul role="group" className={getClasses(baseClassNames.nodeGroup)}>
{expandedIds.has(element.id) &&
element.children.length > 0 &&
Expand All @@ -329,7 +330,7 @@ export const NodeGroup = ({
expandedIds={expandedIds}
baseClassNames={baseClassNames}
key={`${x}-${typeof x}`}
element={getTreeNode(data, x)}
element={getTreeNode(data, x) as INode<M>}
setsize={element.children.length}
posinset={index + 1}
level={level + 1}
Expand Down

0 comments on commit 3d8b191

Please sign in to comment.