Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): add useNodesState and useEdgesState
- Loading branch information
Showing
8 changed files
with
66 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useState, useCallback, SetStateAction, Dispatch } from 'react'; | ||
|
||
import { applyNodeChanges, applyEdgeChanges } from '../utils/changes'; | ||
import { Node, NodeChange, Edge, EdgeChange } from '../types'; | ||
|
||
type ApplyChanges<ItemType, ChangesType> = (changes: ChangesType[], items: ItemType[]) => ItemType[]; | ||
type OnChange<ChangesType> = (changes: ChangesType[]) => void; | ||
|
||
// returns a hook that can be used liked this: | ||
// const [nodes, setNodes, onNodesChange] = useNodesState(intialNodes); | ||
function createUseItemsState<ItemType, ChangesType>( | ||
applyChangesFunction: ApplyChanges<ItemType, ChangesType> | ||
): (initialItems: ItemType[]) => [ItemType[], Dispatch<SetStateAction<ItemType[]>>, OnChange<ChangesType>] { | ||
return (initialItems: ItemType[]) => { | ||
const [items, setItems] = useState<ItemType[]>(initialItems); | ||
|
||
const onItemsChange = useCallback( | ||
(changes: ChangesType[]) => setItems((items) => applyChangesFunction(changes, items)), | ||
[] | ||
); | ||
|
||
return [items, setItems, onItemsChange]; | ||
}; | ||
} | ||
|
||
export const useNodesState = createUseItemsState<Node, NodeChange>(applyNodeChanges as ApplyChanges<Node, NodeChange>); | ||
export const useEdgesState = createUseItemsState<Edge, EdgeChange>(applyEdgeChanges as ApplyChanges<Edge, EdgeChange>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters