Skip to content

Commit

Permalink
docs(graphin):add behaviors demos
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Jan 22, 2021
1 parent 228241e commit ad46384
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
11 changes: 0 additions & 11 deletions packages/graphin/docs/register/behaviors.md

This file was deleted.

29 changes: 29 additions & 0 deletions packages/graphin/docs/register/behaviors/g6-style.tsx
@@ -0,0 +1,29 @@
import React from 'react';
import Graphin, { IG6GraphEvent, Utils, GraphinData } from '@antv/graphin';
import { INode, NodeConfig } from '@antv/g6';

const data: GraphinData = Utils.mock(8)
.circle()
.graphin();

Graphin.registerBehavior('sampleBehavior', {
getEvents() {
return {
'node:click': 'onClick',
};
},
onClick(evt: IG6GraphEvent) {
const node = evt.item as INode;
const model = node.getModel() as NodeConfig;
console.log(model);
// TODO
},
});

/**
* @example
* https://g6.antv.vision/zh/docs/api/Behavior
*/
export default () => {
return <Graphin data={data} layout={{ type: 'concentric' }} modes={{ default: ['sampleBehavior'] }} />;
};
37 changes: 37 additions & 0 deletions packages/graphin/docs/register/behaviors/graphin-style.tsx
@@ -0,0 +1,37 @@
import React, { useContext, useEffect } from 'react';
import Graphin, { IG6GraphEvent, Utils, GraphinData, GraphinContext } from '@antv/graphin';
import { INode, NodeConfig } from '@antv/g6';

const data: GraphinData = Utils.mock(8)
.circle()
.graphin();

const SampleBehavior = () => {
const { graph } = useContext(GraphinContext);

useEffect(() => {
const handleClick = (evt: IG6GraphEvent) => {
const node = evt.item as INode;
const model = node.getModel() as NodeConfig;
console.log(model);
// TODO
};
graph.on('node:click', handleClick);
return () => {
graph.off('node:click', handleClick);
};
}, []);
return null;
};

/**
* @example
* https://g6.antv.vision/zh/docs/api/Behavior
*/
export default () => {
return (
<Graphin data={data} layout={{ type: 'concentric' }}>
<SampleBehavior />
</Graphin>
);
};
19 changes: 19 additions & 0 deletions packages/graphin/docs/register/behaviors/index.md
@@ -0,0 +1,19 @@
---
title: 注册Behaviors
order: 0
group:
path: /register
title: 注册机制
nav:
title: Graphin
path: /graphin
order: 5
---

## 【推荐】 Graphin 注册 Behavior

<code src='./graphin-style.tsx'>

## 【兼容】 G6 注册 Behavior 的写法

<code src='./g6-style.tsx'>

0 comments on commit ad46384

Please sign in to comment.