Skip to content

Commit b683f82

Browse files
authored
feat: x6-angular-shape support pass arguments (antvis#1250)
* feat: ✨ x6-angular-shape support pass arguments * docs: 📚️ add demo about pass arguments
1 parent 12f396f commit b683f82

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

packages/x6-angular-shape/README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ $ yarn add @antv/x6-angular-shape
1616

1717
### Render component
1818
```ts
19+
import { Component, Input } from '@angular/core';
20+
21+
@Component({
22+
selector: 'app-node',
23+
template: `<div>{{ title }}</div>`
24+
})
25+
export class NodeComponent {
26+
@Input() title: string;
27+
}
28+
```
29+
```ts
1930
// other package from angular
2031
import '@antv/x6-angular-shape'
2132

@@ -29,6 +40,13 @@ export class AppComponent {
2940
addAngularComponent(): void {
3041
Graph.registerAngularContent('demo-component', { injector: this.injector, content: NodeComponent });
3142
this.graph.addNode({
43+
data: {
44+
// You can pass data to the component, only if you wrap attribute with ngArguments
45+
ngArguments: {
46+
// Declare @Input() in the component, then it will be assignmented
47+
title: 'Angular Component'
48+
}
49+
},
3250
x: 40,
3351
y: 40,
3452
width: 160,
@@ -42,8 +60,8 @@ export class AppComponent {
4260

4361
### Render templateRef
4462
```html
45-
<ng-template #demoTpl>
46-
<div>Angular Template</div>
63+
<ng-template #demoTpl let-data="ngArguments">
64+
<div>{{ data.title }}</div>
4765
</ng-template>
4866
```
4967
```ts
@@ -60,13 +78,17 @@ export class AppComponent {
6078
addAngularTemplate(): void {
6179
Graph.registerAngularContent('demo-template', { injector: this.injector, content: this.demoTpl });
6280
this.graph.addNode({
81+
data: {
82+
ngArguments: {
83+
title: 'Angular Template'
84+
}
85+
},
6386
x: 240,
6487
y: 40,
6588
width: 160,
6689
height: 30,
6790
shape: 'angular-shape',
6891
componentName: 'demo-template'
69-
});
7092
}
7193
}
7294
```
@@ -91,6 +113,11 @@ export class AppComponent {
91113
return { injector: this.injector, content: this.demoTpl };
92114
});
93115
this.graph.addNode({
116+
data: {
117+
ngArguments: {
118+
title: 'Angular Callback'
119+
}
120+
},
94121
x: 240,
95122
y: 40,
96123
width: 160,

packages/x6-angular-shape/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/x6-angular-shape",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "X6 shape for rendering angular components.",
55
"main": "lib/index.js",
66
"module": "es/index.js",

packages/x6-angular-shape/src/view.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,20 @@ export class AngularShapeView extends NodeView<AngularShape> {
4343
applicationRef,
4444
injector,
4545
)
46+
const ngArguments =
47+
(node.data?.ngArguments as { [key: string]: any }) || {}
4648
if (content instanceof TemplateRef) {
47-
const portal = new TemplatePortal(content, viewContainerRef)
49+
const portal = new TemplatePortal(content, viewContainerRef, {
50+
ngArguments,
51+
})
4852
domOutlet.attachTemplatePortal(portal)
4953
} else {
50-
const portal = new ComponentPortal(content as any, viewContainerRef)
51-
domOutlet.attachComponentPortal(portal)
54+
const portal = new ComponentPortal(content, viewContainerRef)
55+
const componentRef = domOutlet.attachComponentPortal(portal)
56+
Object.keys(ngArguments).forEach(
57+
(v) => (componentRef.instance[v] = ngArguments[v]),
58+
)
59+
componentRef.changeDetectorRef.detectChanges()
5260
}
5361
}
5462
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "../../tsconfig.json",
2+
"extends": "../../tsconfig.json"
33
}

sites/x6-sites/docs/tutorial/advanced/react.zh.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,18 @@ yarn add @antv/x6-angular-shape
424424

425425
```
426426

427-
#### 使用
427+
将Angular component作为节点渲染,并且允许你传递参数给组件。
428+
```ts
429+
import { Component, Input } from '@angular/core';
428430

429-
将Angular component作为节点渲染。
431+
@Component({
432+
selector: 'app-node',
433+
template: `<div>{{ title }}</div>`
434+
})
435+
export class NodeComponent {
436+
@Input() title: string;
437+
}
438+
```
430439
```ts
431440
// other package from angular
432441
import '@antv/x6-angular-shape'
@@ -441,6 +450,13 @@ export class AppComponent {
441450
addAngularComponent(): void {
442451
Graph.registerAngularContent('demo-component', { injector: this.injector, content: NodeComponent });
443452
this.graph.addNode({
453+
data: {
454+
// You can pass data to the component, only if you wrap attribute with ngArguments
455+
ngArguments: {
456+
// Declare @Input() in the component, then it will be assignmented
457+
title: 'Angular Component'
458+
}
459+
},
444460
x: 40,
445461
y: 40,
446462
width: 160,
@@ -452,10 +468,10 @@ export class AppComponent {
452468
}
453469
```
454470

455-
将Angular template作为节点渲染。
471+
将Angular template作为节点渲染,并且允许你传递参数给模板
456472
```html
457-
<ng-template #demoTpl>
458-
<div>Angular Template</div>
473+
<ng-template #demoTpl let-data="ngArguments">
474+
<div>{{ data.title }}</div>
459475
</ng-template>
460476
```
461477
```ts
@@ -472,19 +488,22 @@ export class AppComponent {
472488
addAngularTemplate(): void {
473489
Graph.registerAngularContent('demo-template', { injector: this.injector, content: this.demoTpl });
474490
this.graph.addNode({
491+
data: {
492+
ngArguments: {
493+
title: 'Angular Template'
494+
}
495+
},
475496
x: 240,
476497
y: 40,
477498
width: 160,
478499
height: 30,
479500
shape: 'angular-shape',
480501
componentName: 'demo-template'
481-
});
482502
}
483503
}
484504
```
485505
486506
在注册函数中, 使用回调函数进行渲染,这种方式使得你可以读取节点中的一些属性。
487-
488507
```ts
489508
// other package from angular
490509
import '@antv/x6-angular-shape'
@@ -503,6 +522,11 @@ export class AppComponent {
503522
return { injector: this.injector, content: this.demoTpl };
504523
});
505524
this.graph.addNode({
525+
data: {
526+
ngArguments: {
527+
title: 'Angular Callback'
528+
}
529+
},
506530
x: 240,
507531
y: 40,
508532
width: 160,

0 commit comments

Comments
 (0)