refactor(minimap): use memo

This commit is contained in:
moklick
2020-07-27 12:35:40 +02:00
parent f26323a1e8
commit 8d0e8c63fb
2 changed files with 77 additions and 69 deletions
@@ -1,20 +1,17 @@
import React from 'react'; import React, { memo, CSSProperties } from 'react';
import { Node } from '../../types';
interface MiniMapNodeProps { interface MiniMapNodeProps {
node: Node; x: number;
y: number;
width: number;
height: number;
color: string; color: string;
borderRadius: number; borderRadius: number;
style?: CSSProperties;
} }
const MiniMapNode = ({ node, color, borderRadius }: MiniMapNodeProps) => { const MiniMapNode = memo(({ x, y, width, height, style, color, borderRadius }: MiniMapNodeProps) => {
const { const { background, backgroundColor } = style || {};
position: { x, y },
width,
height,
} = node.__rf;
const { background, backgroundColor } = node.style || {};
const fill = (color || background || backgroundColor) as string; const fill = (color || background || backgroundColor) as string;
return ( return (
@@ -29,7 +26,7 @@ const MiniMapNode = ({ node, color, borderRadius }: MiniMapNodeProps) => {
fill={fill} fill={fill}
/> />
); );
}; });
MiniMapNode.displayName = 'MiniMapNode'; MiniMapNode.displayName = 'MiniMapNode';
+16 -5
View File
@@ -1,4 +1,4 @@
import React from 'react'; import React, { memo } from 'react';
import cc from 'classcat'; import cc from 'classcat';
import { useStoreState } from '../../store/hooks'; import { useStoreState } from '../../store/hooks';
@@ -19,13 +19,14 @@ interface MiniMapProps extends React.HTMLAttributes<SVGSVGElement> {
const defaultWidth = 200; const defaultWidth = 200;
const defaultHeight = 150; const defaultHeight = 150;
const MiniMap = ({ const MiniMap = memo(
({
style = { backgroundColor: '#f8f8f8' }, style = { backgroundColor: '#f8f8f8' },
className, className,
nodeColor = '#ddd', nodeColor = '#ddd',
nodeBorderRadius = 5, nodeBorderRadius = 5,
maskColor = 'rgba(10, 10, 10, .25)', maskColor = 'rgba(10, 10, 10, .25)',
}: MiniMapProps) => { }: MiniMapProps) => {
const containerWidth = useStoreState((s) => s.width); const containerWidth = useStoreState((s) => s.width);
const containerHeight = useStoreState((s) => s.height); const containerHeight = useStoreState((s) => s.height);
const [tX, tY, tScale] = useStoreState((s) => s.transform); const [tX, tY, tScale] = useStoreState((s) => s.transform);
@@ -66,7 +67,16 @@ const MiniMap = ({
{nodes {nodes
.filter((node) => !node.isHidden) .filter((node) => !node.isHidden)
.map((node) => ( .map((node) => (
<MiniMapNode key={node.id} node={node} color={nodeColorFunc(node)} borderRadius={nodeBorderRadius} /> <MiniMapNode
key={node.id}
x={node.__rf.position.x}
y={node.__rf.position.y}
width={node.__rf.width}
height={node.__rf.height}
style={node.style}
color={nodeColorFunc(node)}
borderRadius={nodeBorderRadius}
/>
))} ))}
<path <path
className="react-flow__minimap-mask" className="react-flow__minimap-mask"
@@ -77,7 +87,8 @@ const MiniMap = ({
/> />
</svg> </svg>
); );
}; }
);
MiniMap.displayName = 'MiniMap'; MiniMap.displayName = 'MiniMap';