UNPKG

@syncfusion/react-base

Version:

A common package of core React base, methods and class definitions

71 lines (70 loc) 2.45 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { createContext, useContext, useState, useRef } from 'react'; const DragDropContext = createContext(undefined); /** * Custom hook that provides access to the droppable context functionality. * * @private * @returns {DragDropContextProps} The droppable context methods and state */ export const useDragDropContext = () => { const context = useContext(DragDropContext); if (!context) { return undefined; } return context; }; /** * Provider component that manages droppable instances throughout the application, provides registration and retrieval methods for droppable elements. * * @param {DragDropProps} props - The component props * @param {ReactNode} props.children - The child elements to render within the provider * @returns {Element} The rendered DragDrop provider component */ export const DragDrop = ({ children }) => { const [droppables, setDroppables] = useState({}); const currentDroppables = useRef({}); /** * Registers a droppable instance with a unique identifier. * * @param {string} id - The unique identifier for the droppable instance * @param {DragDropContext} instance - The droppable instance to register * @returns {void} */ const registerDroppable = (id, instance) => { setDroppables((prev) => { const updated = { ...prev, [id]: instance }; currentDroppables.current = updated; return updated; }); }; /** * Unregisters a droppable instance by its identifier. * * @param {string} id - The unique identifier of the droppable instance to unregister * @returns {void} */ const unregisterDroppable = (id) => { setDroppables((prev) => { const newDroppables = { ...prev }; delete newDroppables[`${id}`]; return newDroppables; }); }; /** * Retrieves all registered droppable instances. * * @returns {Record<string, DragDropContext>} A record of all droppable instances indexed by their identifiers */ const getAllDroppables = () => { return currentDroppables.current || droppables; }; return (_jsx(DragDropContext.Provider, { value: { registerDroppable, unregisterDroppable, getAllDroppables }, children: children })); };