Skip to content

Commit c41053c

Browse files
committed
REA-265: Document edge tooltip support
1 parent 947c1ae commit c41053c

File tree

7 files changed

+93
-51
lines changed

7 files changed

+93
-51
lines changed

docs/features/Tooltips.mdx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,26 @@ You can also specify custom React components to render the tooltips according to
2828
<Playground
2929
code={`
3030
31-
function MyTooltipComponent({ data: { name } }: RenderTooltipProps<CustomOrgChartItem>) {
32-
return <div style={{ backgroundColor: "tomato", padding: 5, borderRadius: 10, color: "white" }}>{name}</div>
31+
function MyTooltipComponent({
32+
data
33+
}: RenderTooltipProps<CustomOrgChartItem | OrgChartConnection<CustomOrgChartItem>>) {
34+
let text = ''
35+
if ('name' in data) {
36+
// orgchart item tooltip
37+
text = data.name!
38+
} else if ('source' in data) {
39+
// orgchart connection tooltip
40+
text = \`${data.source.name} → ${data.target.name}\`
41+
}
42+
43+
return (
44+
<div style={{ backgroundColor: 'tomato', padding: 5, borderRadius: 10, color: 'white' }}>
45+
{text}
46+
</div>
47+
)
3348
}
3449
50+
3551
function App () {
3652
return (
3753
<OrgChart

examples/src/examples/Islands/index.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ContextMenuItem,
33
CustomOrgChartItem,
44
OrgChart,
5+
OrgChartConnection,
56
RenderItemProps,
67
RenderTooltipProps
78
} from '@yworks/react-yfiles-orgchart'
@@ -14,9 +15,23 @@ export function useColorContext() {
1415
return useContext(ColorContext)
1516
}
1617

17-
function TooltipTemplate({ data: { name } }: RenderTooltipProps<CustomOrgChartItem>) {
18-
const color = useColorContext()
19-
return <div style={{ backgroundColor: color }}>{name}</div>
18+
function MyTooltipComponent({
19+
data
20+
}: RenderTooltipProps<CustomOrgChartItem | OrgChartConnection<CustomOrgChartItem>>) {
21+
let text = ''
22+
if ('name' in data) {
23+
// orgchart item tooltip
24+
text = data.name!
25+
} else if ('source' in data) {
26+
// orgchart connection tooltip
27+
text = `${data.source.name}${data.target.name}`
28+
}
29+
30+
return (
31+
<div style={{ backgroundColor: 'tomato', padding: 5, borderRadius: 10, color: 'white' }}>
32+
{text}
33+
</div>
34+
)
2035
}
2136

2237
function RenderMenu(_: {
@@ -70,7 +85,7 @@ export default function Islands() {
7085
<OrgChart
7186
data={largeOrgchartData}
7287
renderItem={RenderItem}
73-
renderTooltip={TooltipTemplate}
88+
renderTooltip={MyTooltipComponent}
7489
renderContextMenu={RenderMenu}
7590
></OrgChart>
7691
</ColorContext.Provider>

examples/src/examples/MUIDarkMode/index.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ContextMenuItem,
44
CustomOrgChartItem,
55
OrgChart,
6+
OrgChartConnection,
67
OrgChartContextMenuItems,
78
RenderItemProps,
89
RenderTooltipProps
@@ -26,26 +27,30 @@ import {
2627
Tooltip
2728
} from '@mui/material'
2829

29-
function TooltipTemplate({ data }: RenderTooltipProps<CustomOrgChartItem>) {
30-
return (
31-
<Card sx={{ minWidth: 275 }}>
32-
<CardContent>
33-
<Typography sx={{ fontSize: 14 }} color="text.secondary" gutterBottom>
34-
{data.position}
35-
</Typography>
36-
<Typography variant="h5" component="div">
37-
{data.name}
38-
</Typography>
39-
<Typography sx={{ mb: 1.5 }} color="text.secondary">
40-
{data.status}
41-
</Typography>
42-
<Typography variant="body2">{data.email}</Typography>
43-
</CardContent>
44-
<CardActions>
45-
<Button size="small">Learn More</Button>
46-
</CardActions>
47-
</Card>
48-
)
30+
function TooltipTemplate({
31+
data
32+
}: RenderTooltipProps<CustomOrgChartItem | OrgChartConnection<CustomOrgChartItem>>) {
33+
if ('name' in data) {
34+
return (
35+
<Card sx={{ minWidth: 275 }}>
36+
<CardContent>
37+
<Typography sx={{ fontSize: 14 }} color="text.secondary" gutterBottom>
38+
{data.position}
39+
</Typography>
40+
<Typography variant="h5" component="div">
41+
{data.name}
42+
</Typography>
43+
<Typography sx={{ mb: 1.5 }} color="text.secondary">
44+
{data.status}
45+
</Typography>
46+
<Typography variant="body2">{data.email}</Typography>
47+
</CardContent>
48+
<CardActions>
49+
<Button size="small">Learn More</Button>
50+
</CardActions>
51+
</Card>
52+
)
53+
}
4954
}
5055

5156
function RenderItem(props: RenderItemProps<CustomOrgChartItem>) {
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { RenderTooltipProps } from '@yworks/react-yfiles-core'
2-
import { CustomOrgChartItem } from '@yworks/react-yfiles-orgchart'
2+
import { CustomOrgChartItem, OrgChartConnection } from '@yworks/react-yfiles-orgchart'
33

4-
export default function TooltipTemplate({ data }: RenderTooltipProps<CustomOrgChartItem>) {
5-
return (
6-
<div className={'node-background'}>
7-
{
8-
<div className={'detail-node'}>
9-
<div className={'node-right'}>
10-
<div className={'node-name'}>{data.name}</div>
11-
<div className={'node-position'}>{data.position}</div>
12-
<div className={'node-email'}>{data.email}</div>
13-
<div className={'node-phone'}>{data.name}</div>
4+
export default function TooltipTemplate({
5+
data
6+
}: RenderTooltipProps<CustomOrgChartItem | OrgChartConnection<CustomOrgChartItem>>) {
7+
if ('name' in data) {
8+
return (
9+
<div className={'node-background'}>
10+
{
11+
<div className={'detail-node'}>
12+
<div className={'node-right'}>
13+
<div className={'node-name'}>{data.name}</div>
14+
<div className={'node-position'}>{data.position}</div>
15+
<div className={'node-email'}>{data.email}</div>
16+
<div className={'node-phone'}>{data.name}</div>
17+
</div>
1418
</div>
15-
</div>
16-
}
17-
</div>
18-
)
19+
}
20+
</div>
21+
)
22+
}
1923
}

src/OrgChart.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ export type CustomOrgChartData<TCustomProps> = CustomOrgChartItem<TCustomProps>[
127127
/**
128128
* The basic data type for the connections between data items visualized by the {@link OrgChart} component.
129129
*/
130-
export interface OrgChartConnection {
131-
source: OrgChartItem
132-
target: OrgChartItem
130+
export interface OrgChartConnection<TOrgChartItem extends OrgChartItem> {
131+
source: TOrgChartItem
132+
target: TOrgChartItem
133133
}
134134

135135
/**
@@ -235,7 +235,9 @@ export interface OrgChartProps<TOrgChartItem extends OrgChartItem, TNeedle> {
235235
/**
236236
* An optional component that can be used for rendering a custom tooltip.
237237
*/
238-
renderTooltip?: ComponentType<RenderTooltipProps<TOrgChartItem>>
238+
renderTooltip?: ComponentType<
239+
RenderTooltipProps<TOrgChartItem | OrgChartConnection<TOrgChartItem>>
240+
>
239241
/**
240242
* An optional function specifying the context menu items for a data item.
241243
*/

src/OrgChartModel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ export interface OrgChartModel {
8080
/**
8181
* Pans the viewport to the center of the given items.
8282
*/
83-
zoomTo(items: (OrgChartItem | OrgChartConnection)[]): void
83+
zoomTo(items: (OrgChartItem | OrgChartConnection<OrgChartItem>)[]): void
8484
/**
8585
* Pans the viewport to center the given item.
8686
*/
87-
zoomToItem(item: OrgChartItem | OrgChartConnection): void
87+
zoomToItem(item: OrgChartItem | OrgChartConnection<OrgChartItem>): void
8888
/**
8989
* Increases the zoom level.
9090
*/
@@ -169,7 +169,7 @@ export function createOrgChartModel(
169169
onRenderedCallback = null
170170
}
171171

172-
function zoomTo(items: (OrgChartItem | OrgChartConnection)[]) {
172+
function zoomTo(items: (OrgChartItem | OrgChartConnection<OrgChartItem>)[]) {
173173
if (items.length === 0) {
174174
return
175175
}
@@ -277,7 +277,7 @@ export function createOrgChartModel(
277277
return collapsibleTree.applyInitialLayout(incremental ?? false, incrementalNodes)
278278
},
279279

280-
zoomToItem(item: OrgChartItem | OrgChartConnection) {
280+
zoomToItem(item: OrgChartItem | OrgChartConnection<OrgChartItem>) {
281281
zoomTo([item])
282282
},
283283

src/styles/Templates.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function findProperties(data: CustomOrgChartItem) {
194194
*/
195195
export function RenderOrgChartTooltip<TOrgChartItem extends OrgChartItem>({
196196
data
197-
}: RenderTooltipProps<TOrgChartItem>) {
197+
}: RenderTooltipProps<TOrgChartItem | OrgChartConnection<TOrgChartItem>>) {
198198
// Currently, no tooltips are shown for edges.
199199
if ('source' in data && 'target' in data) {
200200
return null
@@ -288,7 +288,7 @@ export function OrgChartControlButtons(): ControlButton[] {
288288
* @returns an array of [context menu items]{@link ContextMenuProps.menuItems}.
289289
*/
290290
export function OrgChartContextMenuItems(
291-
item: OrgChartItem | OrgChartConnection | null
291+
item: OrgChartItem | OrgChartConnection<OrgChartItem> | null
292292
): ContextMenuItem<OrgChartItem>[] {
293293
const orgChart = useOrgChartContext()
294294

0 commit comments

Comments
 (0)