UNPKG

@atlaskit/dropdown-menu

Version:

A dropdown menu displays a list of actions or options to a user.

53 lines 1.59 kB
import React, { createContext, useMemo, useRef } from 'react'; import noop from '@atlaskit/ds-lib/noop'; /** * * SelectionStoreContext maintains the state of the selected items * and getter setters. * */ export var SelectionStoreContext = /*#__PURE__*/createContext({ setItemState: noop, getItemState: function getItemState() { return undefined; }, setGroupState: noop, getGroupState: function getGroupState() { return {}; } }); /** * Selection store will persist data as long as it remains mounted. * It handles the uncontrolled story for dropdown menu when the menu * items can be mounted/unmounted depending if the menu is open or closed. */ var SelectionStore = function SelectionStore(props) { var children = props.children; var store = useRef({}); var context = useMemo(function () { return { setItemState: function setItemState(group, id, value) { if (!store.current[group]) { store.current[group] = {}; } store.current[group][id] = value; }, getItemState: function getItemState(group, id) { if (!store.current[group]) { return undefined; } return store.current[group][id]; }, setGroupState: function setGroupState(group, value) { store.current[group] = value; }, getGroupState: function getGroupState(group) { return store.current[group] || {}; } }; }, []); return /*#__PURE__*/React.createElement(SelectionStoreContext.Provider, { value: context }, children); }; export default SelectionStore;