feat(react): infer node types from passed nodes

This commit is contained in:
moklick
2024-01-24 18:25:31 +01:00
parent 8983d64397
commit a345b2670b
15 changed files with 364 additions and 354 deletions
@@ -68,7 +68,9 @@ const reactFlowFieldsToTrack = [
] as const;
type ReactFlowFieldsToTrack = (typeof reactFlowFieldsToTrack)[number];
type StoreUpdaterProps = Pick<ReactFlowProps, ReactFlowFieldsToTrack> & { rfId: string };
type StoreUpdaterProps<NodeType extends Node = Node> = Pick<ReactFlowProps<NodeType>, ReactFlowFieldsToTrack> & {
rfId: string;
};
// rfId doesn't exist in ReactFlowProps, but it's one of the fields we want to update
const fieldsToTrack = [...reactFlowFieldsToTrack, 'rfId'] as const;
@@ -84,7 +86,7 @@ const selector = (s: ReactFlowState) => ({
reset: s.reset,
});
export function StoreUpdater(props: StoreUpdaterProps) {
export function StoreUpdater<NodeType extends Node = Node>(props: StoreUpdaterProps<NodeType>) {
const {
setNodes,
setEdges,
@@ -106,7 +108,7 @@ export function StoreUpdater(props: StoreUpdaterProps) {
};
}, []);
const previousFields = useRef<Partial<StoreUpdaterProps>>({
const previousFields = useRef<Partial<StoreUpdaterProps<NodeType>>>({
// these are values that are also passed directly to other components
// than the StoreUpdater. We can reduce the number of setStore calls
// by setting the same values here as prev fields.