fix(validation-example): add custom nodes with validation

This commit is contained in:
moklick
2020-06-02 00:41:10 +02:00
parent 206cd1ded1
commit eae485704b
2 changed files with 35 additions and 11 deletions
+21 -5
View File
@@ -6,9 +6,9 @@ import './validation.css';
const initialElements = [ const initialElements = [
{ id: '0', type: 'custominput', position: { x: 0, y: 150 } }, { id: '0', type: 'custominput', position: { x: 0, y: 150 } },
{ id: 'A', data: { label : 'A' }, position: { x: 250, y: 0 }, targetPosition: 'left', sourcePosition: 'right' }, { id: 'A', type: 'customnode', position: { x: 250, y: 0 } },
{ id: 'B', data: { label : 'B' }, position: { x: 250, y: 150 }, targetPosition: 'left', sourcePosition: 'right' }, { id: 'B', type: 'customnode', position: { x: 250, y: 150 } },
{ id: 'C', data: { label : 'C' }, position: { x: 250, y:300 }, targetPosition: 'left', sourcePosition: 'right' }, { id: 'C', type: 'customnode', position: { x: 250, y:300 } },
]; ];
const CustomInput = () => ( const CustomInput = () => (
@@ -17,12 +17,27 @@ const CustomInput = () => (
<Handle <Handle
type="source" type="source"
position="right" position="right"
style={{ background: '#e6e6e9' }}
isValidConnection={connection => connection.target === 'B'} isValidConnection={connection => connection.target === 'B'}
/> />
</> </>
); );
const CustomNode = ({ id }) => (
<>
<Handle
type="target"
position="left"
isValidConnection={_ => id === 'B'}
/>
<div>{id}</div>
<Handle
type="source"
position="right"
isValidConnection={_ => id === 'B'}
/>
</>
);
const HorizontalFlow = () => { const HorizontalFlow = () => {
const [elements, setElements] = useState(initialElements); const [elements, setElements] = useState(initialElements);
const onConnect = (params) => setElements(els => addEdge(params, els)); const onConnect = (params) => setElements(els => addEdge(params, els));
@@ -35,7 +50,8 @@ const HorizontalFlow = () => {
onLoad={reactflowInstance => reactflowInstance.fitView()} onLoad={reactflowInstance => reactflowInstance.fitView()}
className="validationflow" className="validationflow"
nodeTypes={{ nodeTypes={{
custominput: CustomInput custominput: CustomInput,
customnode: CustomNode
}} }}
/> />
); );
+14 -6
View File
@@ -1,19 +1,27 @@
.validationflow .react-flow__node { .validationflow .react-flow__node {
background: #e6e6e9;
border: 1px solid #ddd;
}
.validationflow .react-flow__node-custominput {
width: 150px; width: 150px;
border-radius: 5px; border-radius: 5px;
padding: 10px; padding: 10px;
background: #fff;
color: #555; color: #555;
border: 1px solid #ddd; border: 1px solid #ddd;
text-align: center; text-align: center;
font-size: 12px; font-size: 12px;
} }
.validationflow .react-flow__node-customnode {
background: #e6e6e9;
border: 1px solid #ddd;
}
.react-flow__node-custominput .react-flow__handle {
background: #e6e6e9;
}
.validationflow .react-flow__node-custominput {
background: #fff;
}
.validationflow .connecting { .validationflow .connecting {
background: #ff6060; background: #ff6060;
} }