@atlaskit/dropdown-menu
Version:
A dropdown menu displays a list of actions or options to a user.
49 lines • 1.37 kB
JavaScript
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 const SelectionStoreContext = /*#__PURE__*/createContext({
setItemState: noop,
getItemState: () => undefined,
setGroupState: noop,
getGroupState: () => ({})
});
/**
* 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.
*/
const SelectionStore = props => {
const {
children
} = props;
const store = useRef({});
const context = useMemo(() => ({
setItemState: (group, id, value) => {
if (!store.current[group]) {
store.current[group] = {};
}
store.current[group][id] = value;
},
getItemState: (group, id) => {
if (!store.current[group]) {
return undefined;
}
return store.current[group][id];
},
setGroupState: (group, value) => {
store.current[group] = value;
},
getGroupState: group => {
return store.current[group] || {};
}
}), []);
return /*#__PURE__*/React.createElement(SelectionStoreContext.Provider, {
value: context
}, children);
};
export default SelectionStore;