Skip to content

Commit

Permalink
fix(react): fix react component update not effect (#6656)
Browse files Browse the repository at this point in the history
* refactor: fix demo types

* fix(react): fix react component update not effect

* chore: config react test env

* test: add test case

* chore: update version
  • Loading branch information
Aarebecca authored Dec 19, 2024
1 parent 769fdc3 commit 59267bf
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/g6-extension-3d/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g6-extension-3d",
"version": "0.1.11",
"version": "0.1.12",
"description": "3D extension for G6",
"keywords": [
"antv",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ReactNode } from '../../src';

it('attribute changed callback', () => {
const oldComponent = () => <div>test</div>;
const node = new ReactNode({
style: {
component: oldComponent,
},
});
Object.assign(node, {
isConnected: true,
ownerDocument: {
defaultView: {
dispatchEvent: () => {},
},
},
});

const spy = jest.spyOn(node, 'attributeChangedCallback');

const component = () => <div>test1</div>;

try {
node.update({ component });
} catch (e) {
// ignore
}

expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenLastCalledWith('component', oldComponent, component, undefined, undefined);
});
19 changes: 18 additions & 1 deletion packages/g6-extension-react/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
const esm = ['internmap', 'd3-*', 'lodash-es', 'chalk'].map((d) => `_${d}|${d}`).join('|');

module.exports = {
transform: {
'^.+\\.[tj]s$': ['@swc/jest'],
'^.+\\.[tj]sx?$': [
'@swc/jest',
{
jsc: {
parser: {
syntax: 'typescript',
decorators: true,
jsx: true,
},
},
},
],
},
testRegex: '(/__tests__/.*\\.(test|spec))\\.(ts|tsx|js)$',
collectCoverageFrom: ['src/**/*.ts'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'],
transformIgnorePatterns: [`<rootDir>/node_modules/.pnpm/(?!(${esm}))`],
moduleNameMapper: {
'@antv/g6': '<rootDir>/../g6/src',
},
};
2 changes: 1 addition & 1 deletion packages/g6-extension-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g6-extension-react",
"version": "0.1.12",
"version": "0.1.13",
"description": "Using React Component to Define Your G6 Graph Node",
"keywords": [
"antv",
Expand Down
11 changes: 10 additions & 1 deletion packages/g6-extension-react/src/elements/react/node.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { HTMLStyleProps as GHTMLStyleProps } from '@antv/g';
import type { DisplayObjectConfig, HTMLStyleProps as GHTMLStyleProps } from '@antv/g';
import type { BaseNodeStyleProps, HTMLStyleProps } from '@antv/g6';
import { HTML } from '@antv/g6';
import type { FC, ReactElement } from 'react';
Expand All @@ -18,6 +18,14 @@ export class ReactNode extends HTML {
return { ...super.getKeyStyle(attributes) };
}

constructor(options: DisplayObjectConfig<ReactNodeStyleProps>) {
super(options as any);
}

public update(attr?: Partial<ReactNodeStyleProps> | undefined): void {
super.update(attr);
}

public connectedCallback() {
super.connectedCallback();
// this.root = createRoot(this.getDomElement());
Expand All @@ -28,6 +36,7 @@ export class ReactNode extends HTML {
}

public attributeChangedCallback(name: any, oldValue: any, newValue: any) {
super.attributeChangedCallback(name, oldValue, newValue);
if (name === 'component' && oldValue !== newValue) {
render(
(this.attributes as unknown as ReactNodeStyleProps).component as unknown as ReactElement,
Expand Down
2 changes: 1 addition & 1 deletion packages/g6-ssr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g6-ssr",
"version": "0.0.6",
"version": "0.0.7",
"description": "Support SSR for G6",
"keywords": [
"antv",
Expand Down
2 changes: 1 addition & 1 deletion packages/g6/__tests__/demos/case-language-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const caseLanguageTree: TestCase = async (context) => {
layout: {
type: 'd3-force',
link: {
distance: (edge) => size(edge.source) + size(edge.target),
distance: (edge: any) => size(edge.source) + size(edge.target),
},
collide: {
radius: (node: NodeData) => size(node) + 1,
Expand Down
6 changes: 5 additions & 1 deletion packages/g6/__tests__/demos/case-unicorns-investors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export const caseUnicornsInvestors: TestCase = async (context) => {
},
layout: {
type: 'd3-force',
link: { distance: (edge) => size(edge.source) + size(edge.target) },
link: {
distance: (edge: any) => {
size(edge.source) + size(edge.target);
},
},
collide: { radius: (node: NodeData) => size(node) },
manyBody: { strength: (node: NodeData) => -4 * size(node) },
animation: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/g6/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g6",
"version": "5.0.37",
"version": "5.0.38",
"description": "A Graph Visualization Framework in JavaScript",
"keywords": [
"antv",
Expand Down
8 changes: 5 additions & 3 deletions packages/g6/src/utils/element.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AABB, BaseStyleProps, DisplayObject, TextStyleProps } from '@antv/g';
import type { AABB, DisplayObject, TextStyleProps } from '@antv/g';
import { get, isNumber, isString, set } from '@antv/util';
import { BaseCombo, BaseEdge, BaseNode } from '../elements';
import type { BaseShape } from '../elements/shapes';
import type { Combo, Edge, Element, Node, NodePortStyleProps, Placement, Point, TriangleDirection } from '../types';
import type { NodeLabelStyleProps, Port } from '../types/node';
import { getBBoxHeight, getBBoxWidth } from './bbox';
Expand Down Expand Up @@ -474,8 +475,8 @@ export function isVisible(element: DisplayObject) {
* @param element - <zh/> 元素 | <en/> element
* @param style - <zh/> 样式 | <en/> style
*/
export function setAttributes(element: DisplayObject, style: BaseStyleProps) {
const { zIndex, transform, transformOrigin, visibility, cursor, clipPath, ...rest } = style;
export function setAttributes(element: BaseShape<any>, style: Record<string, any>) {
const { zIndex, transform, transformOrigin, visibility, cursor, clipPath, component, ...rest } = style;
Object.assign(element.attributes, rest);

if (transform) element.setAttribute('transform', transform);
Expand All @@ -484,6 +485,7 @@ export function setAttributes(element: DisplayObject, style: BaseStyleProps) {
if (visibility) element.setAttribute('visibility', visibility);
if (cursor) element.setAttribute('cursor', cursor);
if (clipPath) element.setAttribute('clipPath', clipPath);
if (component) element.setAttribute('component', component);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/g6/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '5.0.37';
export const version = '5.0.38';

0 comments on commit 59267bf

Please sign in to comment.