Skip to content

Commit

Permalink
fix: useNode
Browse files Browse the repository at this point in the history
  • Loading branch information
sumbatx15 committed Jun 2, 2023
1 parent 0fff870 commit d9bdd0f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
5 changes: 5 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
html,
body {
margin: 0;
padding: 0;
}
.node {
border-radius: 4px;
position: relative;
Expand Down
36 changes: 15 additions & 21 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,21 @@ function App() {
};

return (
<div>
<div style={{ width: "100vw", height: "150vh", padding: "100px" }}>
<DiagramView
id="diagram-1"
style={{
width: "1000px",
height: "600px",
}}
nodeTypes={{
custom: CustomNode,
}}
>
<div style={{ position: "absolute", zIndex: 100 }}>
<button onClick={handleAdd}>addnode</button>
<button onClick={addMore}>add more</button>
<button onClick={() => randomizePositions()}>randomize</button>
<button onClick={() => fitView()}>fitView</button>
</div>
<Background id="diagram-1" color="black" />
</DiagramView>
</div>
<div style={{ width: "100vw", height: "100vh" }}>
<DiagramView
id="diagram-1"
nodeTypes={{
custom: CustomNode,
}}
>
<div style={{ position: "absolute", zIndex: 100 }}>
<button onClick={handleAdd}>addnode</button>
<button onClick={addMore}>add more</button>
<button onClick={() => randomizePositions()}>randomize</button>
<button onClick={() => fitView()}>fitView</button>
</div>
<Background id="diagram-1" color="black" />
</DiagramView>
</div>
);
}
Expand Down
19 changes: 15 additions & 4 deletions src/components/Diagram/WrappedNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ const NodeContextProvider: React.FC<{
);
};

export const useNode = <T,>(selector?: (node: DiagramNode) => T) => {
// overload when selector is provided
export function useNode<T extends (node: DiagramNode) => any>(
selector: T
): ReturnType<T>;

// overload when selector is not provided
export function useNode(selector?: undefined): DiagramNode;

// implementation
export function useNode<T extends (node: DiagramNode) => any | undefined>(
selector?: T
) {
const { nodeId } = useNodeContext();
const useDiagram = useGetDiagramStore();
return useDiagram((state) =>
selector ? selector(state.getNode(nodeId)!) : state.getNode(nodeId)
) as T extends unknown ? DiagramNode : T;
};
);
}

export const useNodeData = <T,>() => {
const { nodeId } = useNodeContext();
Expand All @@ -49,7 +60,7 @@ export const useNodeData = <T,>() => {
export const useNodePosition = () => {
const { nodeId } = useNodeContext();
const useDiagram = useGetDiagramStore();

const position = useDiagram((state) => state.nodePositions[nodeId]);
const setter = useMemo(
() => useDiagram.getState().updateNodePosition.bind(null, nodeId),
Expand Down
6 changes: 4 additions & 2 deletions src/components/Node/CustomNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ const Inp = () => {
};
export const CustomNode: NodeFC = ({ id }) => {
const [show, toggle] = useToggle(true);
const node = useNode();
console.log("node:", node);

return (
<div
// shadow="xl"
style={{
borderRadius: "4px",
position: "relative",
background: "#070708",
padding: "16px",
boxShadow: "0 0 15px rgba(0,0,0,0.2)",
}}
>
{show && <Handle id="in" type="target" placement="left" />}
Expand All @@ -31,7 +34,6 @@ export const CustomNode: NodeFC = ({ id }) => {

<p /* onClick={toggle} */>CustomNode - {id}</p>
<Inp />
<p>Heloo</p>
<button onClick={toggle}>Toggle handles</button>
{show && <Handle id="out" type="source" placement="right" />}
</div>
Expand Down

0 comments on commit d9bdd0f

Please sign in to comment.