-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: extract options logic into upper component * feat: icon supports placement, offsetX, offsetY * feat: mindmap * fix: fix ci issues * feat: custom styled mindmap * refactor: change collapsible option to transform * fix: fix ci issues * refactor: rename infer-react-style to translate-react-node-origin * refactor: modify field names * fix: fix ci issues
- Loading branch information
Showing
36 changed files
with
817 additions
and
338 deletions.
There are no files selected for viewing
64 changes: 55 additions & 9 deletions
64
packages/graphs/src/components/hierarchical-graph/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,60 @@ | ||
import type { Graph } from '@antv/g6'; | ||
import React, { forwardRef, ForwardRefExoticComponent, PropsWithChildren, PropsWithoutRef, RefAttributes } from 'react'; | ||
import type { Graph, NodeData } from '@antv/g6'; | ||
import { idOf } from '@antv/g6'; | ||
import React, { | ||
forwardRef, | ||
ForwardRefExoticComponent, | ||
PropsWithChildren, | ||
PropsWithoutRef, | ||
RefAttributes, | ||
useMemo, | ||
} from 'react'; | ||
import { BaseGraph } from '../../core/base-graph'; | ||
import { COMMON_OPTIONS } from '../../core/constants'; | ||
import { PlainNode } from '../../core/nodes'; | ||
import { mergeOptions } from '../../core/utils/options'; | ||
import type { GraphOptions } from '../../types'; | ||
|
||
const HierarchicalGraph: ForwardRefExoticComponent< | ||
const DEFAULT_OPTIONS: GraphOptions = { | ||
node: { | ||
type: 'react', | ||
style: { | ||
component: (data: NodeData) => <PlainNode text={idOf(data)} isActive={data.states?.includes('active')} />, | ||
size: [80, 40], | ||
ports: [{ placement: 'top' }, { placement: 'bottom' }], | ||
}, | ||
state: { | ||
active: { | ||
halo: false, | ||
}, | ||
selected: { | ||
halo: false, | ||
}, | ||
}, | ||
}, | ||
edge: { | ||
type: 'polyline', | ||
style: { | ||
router: { | ||
type: 'orth', | ||
}, | ||
}, | ||
}, | ||
layout: { | ||
type: 'antv-dagre', | ||
rankdir: 'TB', | ||
}, | ||
transforms: ['translate-react-node-origin'], | ||
animation: false, | ||
}; | ||
|
||
export const HierarchicalGraph: ForwardRefExoticComponent< | ||
PropsWithoutRef<PropsWithChildren<GraphOptions>> & RefAttributes<Graph> | ||
> = forwardRef<Graph, PropsWithChildren<GraphOptions>>(({ children, ...props }, ref) => ( | ||
<BaseGraph type="hierarchical-graph" {...props} ref={ref}> | ||
{children} | ||
</BaseGraph> | ||
)); | ||
> = forwardRef<Graph, PropsWithChildren<GraphOptions>>(({ children, ...props }, ref) => { | ||
const options = useMemo(() => mergeOptions(COMMON_OPTIONS, DEFAULT_OPTIONS, props), [props]); | ||
|
||
export default HierarchicalGraph; | ||
return ( | ||
<BaseGraph {...options} ref={ref}> | ||
{children} | ||
</BaseGraph> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
import HierarchicalGraph from './hierarchical-graph'; | ||
|
||
export { HierarchicalGraph }; | ||
export { HierarchicalGraph } from './hierarchical-graph'; | ||
export { MindMap } from './mind-map'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import type { Graph, NodeData } from '@antv/g6'; | ||
import { idOf } from '@antv/g6'; | ||
import React, { | ||
forwardRef, | ||
ForwardRefExoticComponent, | ||
PropsWithChildren, | ||
PropsWithoutRef, | ||
RefAttributes, | ||
useMemo, | ||
} from 'react'; | ||
import { BaseGraph } from '../../core/base-graph'; | ||
import { COMMON_OPTIONS } from '../../core/constants'; | ||
import { measureMindMapNodeSize, MindMapNode } from '../../core/nodes'; | ||
import { getNodeSide } from '../../core/utils/node'; | ||
import { mergeOptions } from '../../core/utils/options'; | ||
import type { GraphOptions } from '../../types'; | ||
|
||
const DEFAULT_OPTIONS: GraphOptions = { | ||
node: { | ||
type: 'react', | ||
style: { | ||
component: (data: NodeData) => ( | ||
<MindMapNode text={idOf(data)} depth={data.data!.depth as number} color={data.style!.color as string} /> | ||
), | ||
size: (data: NodeData) => measureMindMapNodeSize(data), | ||
dx: function (data: NodeData) { | ||
const parentData = (this as unknown as Graph).getParentData(idOf(data), 'tree'); | ||
const side = getNodeSide(data, parentData); | ||
const size = measureMindMapNodeSize(data); | ||
return side === 'left' ? -size[0] : side === 'center' ? -size[0] / 2 : 0; | ||
}, | ||
ports: [{ placement: 'left' }, { placement: 'right' }], | ||
}, | ||
state: { | ||
active: { | ||
halo: false, | ||
}, | ||
selected: { | ||
halo: false, | ||
}, | ||
}, | ||
}, | ||
edge: { | ||
type: 'cubic-horizontal', | ||
style: { | ||
stroke: function (data) { | ||
return (this.getNodeData(data.source).style!.color as string) || '#99ADD1'; | ||
}, | ||
lineWidth: 2, | ||
}, | ||
}, | ||
layout: { | ||
type: 'mindmap', | ||
direction: 'H', | ||
getWidth: () => 120, | ||
getHeight: (data) => measureMindMapNodeSize(data)[1], | ||
getVGap: () => 28, | ||
getHGap: () => 64, | ||
animation: false, | ||
}, | ||
transforms: (prev) => [ | ||
...prev, | ||
'assign-color-by-branch', | ||
{ | ||
type: 'collapse-expand-react-node', | ||
key: 'collapse-expand-react-node', | ||
trigger: 'node', | ||
iconPlacement: function (data: NodeData) { | ||
const parentData = (this as unknown as Graph).getParentData(idOf(data), 'tree'); | ||
const side = getNodeSide(data, parentData); | ||
return side === 'left' ? 'left' : 'right'; | ||
}, | ||
}, | ||
], | ||
animation: { | ||
duration: 500, | ||
}, | ||
}; | ||
|
||
export const MindMap: ForwardRefExoticComponent< | ||
PropsWithoutRef<PropsWithChildren<GraphOptions>> & RefAttributes<Graph> | ||
> = forwardRef<Graph, PropsWithChildren<GraphOptions>>(({ children, ...props }, ref) => { | ||
const options = useMemo(() => mergeOptions(COMMON_OPTIONS, DEFAULT_OPTIONS, props), [props]); | ||
|
||
return ( | ||
<BaseGraph {...options} ref={ref}> | ||
{children} | ||
</BaseGraph> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { COMMON_OPTIONS } from './options'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { GraphOptions } from '../../types'; | ||
|
||
export const COMMON_OPTIONS: GraphOptions = { | ||
behaviors: ['drag-canvas', 'zoom-canvas'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.