Skip to content

Commit ea35e8f

Browse files
committed
fix(useTree): treeIcon can be configured dynamically with the node too
1 parent 2150911 commit ea35e8f

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

.storybook/stories/Features/tree.story.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
77
import FolderIcon from '@mui/icons-material/Folder';
88
import FolderOpenIcon from '@mui/icons-material/FolderOpen';
99
import InsertDriveFileOutlinedIcon from '@mui/icons-material/InsertDriveFileOutlined';
10+
import CheckIcon from '@mui/icons-material/Check';
11+
import CloseIcon from '@mui/icons-material/Close';
1012

1113
import {
1214
Table,
@@ -353,7 +355,8 @@ storiesOf('Features/Tree', module)
353355
{
354356
treeIcon: {
355357
margin: '4px',
356-
iconDefault: <InsertDriveFileOutlinedIcon fontSize="small" />,
358+
iconDefault: (node) =>
359+
node.isComplete ? <CheckIcon fontSize="small" /> : <CloseIcon fontSize="small" />,
357360
iconRight: <FolderIcon fontSize="small" />,
358361
iconDown: <FolderOpenIcon fontSize="small" />,
359362
},

.storybook/stories/Types/tree.story.mdx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { Meta } from '@storybook/addon-docs';
77
```javascript
88
import * as React from 'react';
99
import {
10-
Nullish,
1110
State,
1211
IdReducerFunctions,
1312
Modifier,
13+
Nullish,
1414
} from '@table-library/react-table-library/types/common';
1515
import { TableNode, GetRowProps } from '@table-library/react-table-library/types/table';
1616

@@ -19,13 +19,15 @@ export enum TreeExpandClickTypes {
1919
ButtonClick,
2020
}
2121

22+
export type CustomIcon = React.ReactElement | ((node: TableNode) => React.ReactElement) | Nullish;
23+
2224
export type TreeOptionsIcon = {
2325
margin?: string;
2426
size?: string;
2527
noIconMargin?: string;
26-
iconDefault?: React.ReactElement | Nullish;
27-
iconRight?: React.ReactElement | Nullish;
28-
iconDown?: React.ReactElement | Nullish;
28+
iconDefault?: CustomIcon;
29+
iconRight?: CustomIcon;
30+
iconDown?: CustomIcon;
2931
};
3032

3133
export type TreeOptions = {
@@ -41,9 +43,9 @@ export type TreeOptionsIconSound = {
4143
margin: string;
4244
size: string;
4345
noIconMargin: string;
44-
iconDefault: React.ReactElement | Nullish;
45-
iconRight: React.ReactElement | Nullish;
46-
iconDown: React.ReactElement | Nullish;
46+
iconDefault: CustomIcon;
47+
iconRight: CustomIcon;
48+
iconDown: CustomIcon;
4749
};
4850

4951
export type TreeOptionsSound = {
@@ -64,7 +66,7 @@ export type ColumnTreeProps = ColumnTreePropsObject | boolean;
6466
export type CellTreeProps = {
6567
item: TableNode;
6668
treeIcon?: TreeOptionsIcon;
67-
children: React.ReactNode;
69+
children?: React.ReactNode;
6870
};
6971

7072
export type Tree = {

src/tree/CellTree.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { Cell } from '@table-library/react-table-library/table/Cell';
88
import { TreeContext } from '@table-library/react-table-library/common/context/Tree';
99
import { isLeaf } from '@table-library/react-table-library/common/util/tree';
1010

11-
import { Nullish, State } from '@table-library/react-table-library/types/common';
11+
import { State } from '@table-library/react-table-library/types/common';
1212
import { TableNode } from '@table-library/react-table-library/types/table';
13-
import { CellTreeProps } from '@table-library/react-table-library/types/tree';
13+
import { CellTreeProps, CustomIcon } from '@table-library/react-table-library/types/tree';
1414

1515
const style = css`
1616
display: flex;
@@ -23,13 +23,28 @@ const style = css`
2323
}
2424
`;
2525

26+
export type Size = {
27+
height: string;
28+
width: string;
29+
};
30+
31+
const resolveIcon = (customIcon: CustomIcon, node: TableNode, size: Size) => {
32+
if (!customIcon) return null;
33+
34+
if (typeof customIcon === 'function') {
35+
return React.cloneElement(customIcon(node), { ...size });
36+
}
37+
38+
return React.cloneElement(customIcon, { ...size });
39+
};
40+
2641
const getTreeIcon = (
2742
item: TableNode,
2843
treeState: State,
2944
treeIconSize: string,
30-
TreeIconDefault: React.ReactElement | Nullish,
31-
TreeIconRight: React.ReactElement | Nullish,
32-
TreeIconDown: React.ReactElement | Nullish,
45+
TreeIconDefault: CustomIcon,
46+
TreeIconRight: CustomIcon,
47+
TreeIconDown: CustomIcon,
3348
) => {
3449
const size = {
3550
height: `${treeIconSize}`,
@@ -39,14 +54,14 @@ const getTreeIcon = (
3954
const isTreeExpanded = treeState.ids.includes(item.id);
4055

4156
if (!isLeaf(item) && isTreeExpanded) {
42-
return TreeIconDown ? React.cloneElement(TreeIconDown, { ...size }) : null;
57+
return resolveIcon(TreeIconDown, item, size);
4358
}
4459

4560
if (!isLeaf(item) && !isTreeExpanded) {
46-
return TreeIconRight ? React.cloneElement(TreeIconRight, { ...size }) : null;
61+
return resolveIcon(TreeIconRight, item, size);
4762
}
4863

49-
return TreeIconDefault ? React.cloneElement(TreeIconDefault, { ...size }) : null;
64+
return resolveIcon(TreeIconDefault, item, size);
5065
};
5166

5267
export const CellTree: React.FC<CellTreeProps> = ({

src/types/tree.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as React from 'react';
22
import {
3-
Nullish,
43
State,
54
IdReducerFunctions,
65
Modifier,
6+
Nullish,
77
} from '@table-library/react-table-library/types/common';
88
import { TableNode, GetRowProps } from '@table-library/react-table-library/types/table';
99

@@ -12,13 +12,15 @@ export enum TreeExpandClickTypes {
1212
ButtonClick,
1313
}
1414

15+
export type CustomIcon = React.ReactElement | ((node: TableNode) => React.ReactElement) | Nullish;
16+
1517
export type TreeOptionsIcon = {
1618
margin?: string;
1719
size?: string;
1820
noIconMargin?: string;
19-
iconDefault?: React.ReactElement | Nullish;
20-
iconRight?: React.ReactElement | Nullish;
21-
iconDown?: React.ReactElement | Nullish;
21+
iconDefault?: CustomIcon;
22+
iconRight?: CustomIcon;
23+
iconDown?: CustomIcon;
2224
};
2325

2426
export type TreeOptions = {
@@ -34,9 +36,9 @@ export type TreeOptionsIconSound = {
3436
margin: string;
3537
size: string;
3638
noIconMargin: string;
37-
iconDefault: React.ReactElement | Nullish;
38-
iconRight: React.ReactElement | Nullish;
39-
iconDown: React.ReactElement | Nullish;
39+
iconDefault: CustomIcon;
40+
iconRight: CustomIcon;
41+
iconDown: CustomIcon;
4042
};
4143

4244
export type TreeOptionsSound = {

0 commit comments

Comments
 (0)