mirror of
https://github.com/AykutSarac/jsoncrack.com.git
synced 2025-02-04 01:32:54 +08:00
create react-flow custom node
This commit is contained in:
parent
fcf8dc253a
commit
c039252983
59
src/pages/editor/LiveEditor/Node.tsx
Normal file
59
src/pages/editor/LiveEditor/Node.tsx
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { Handle, Position } from "react-flow-renderer";
|
||||||
|
import styled from "styled-components";
|
||||||
|
|
||||||
|
const StyledWrapper = styled.div<{ isArray?: boolean }>`
|
||||||
|
background: ${({ theme }) => theme.BLACK_SECONDARY};
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 16px;
|
||||||
|
color: ${({ theme, isArray }) => isArray && theme.SEAGREEN};
|
||||||
|
width: auto;
|
||||||
|
min-width: 100px;
|
||||||
|
text-align: ${({ isArray }) => isArray && "center"};
|
||||||
|
`;
|
||||||
|
|
||||||
|
const StyledKey = styled.span`
|
||||||
|
color: ${({ theme }) => theme.BLURPLE};
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const CustomNodeComponent: React.FC<{ data: any; id: string }> = ({
|
||||||
|
id,
|
||||||
|
data,
|
||||||
|
}) => {
|
||||||
|
const { label: json } = data;
|
||||||
|
|
||||||
|
if (typeof json === "string") {
|
||||||
|
return (
|
||||||
|
<StyledWrapper isArray>
|
||||||
|
{json}
|
||||||
|
<Handle type="source" position={Position.Left} />
|
||||||
|
<Handle type="target" position={Position.Right} />
|
||||||
|
</StyledWrapper>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof json === "object") {
|
||||||
|
const keyPairs = Object.entries(json);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<StyledWrapper>
|
||||||
|
{keyPairs.map((entries) => {
|
||||||
|
const isValidValue =
|
||||||
|
typeof entries[1] === "string" || typeof entries[1] === "number";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div key={entries[0]}>
|
||||||
|
<StyledKey>{entries[0]}: </StyledKey>
|
||||||
|
{isValidValue && entries[1]}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
|
||||||
|
<Handle type="source" id={id} position={Position.Left} />
|
||||||
|
<Handle type="target" id={id} position={Position.Right} />
|
||||||
|
</StyledWrapper>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user