@atlaskit/dropdown-menu
Version:
A dropdown menu displays a list of actions or options to a user.
80 lines (78 loc) • 3.71 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "hasSeparator", "id", "isList", "isScrollable", "testId", "title"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { createContext, useContext, useState } from 'react';
import noop from '@atlaskit/ds-lib/noop';
import { useId } from '@atlaskit/ds-lib/use-id';
import { Section } from '@atlaskit/menu';
import GroupTitle from '../internal/components/group-title';
import { SelectionStoreContext } from '../internal/context/selection-store';
import resetOptionsInGroup from '../internal/utils/reset-options-in-group';
/**
* __Radio group context__
* Context provider that wraps each DropdownItemRadioGroup
*/
export var RadioGroupContext = /*#__PURE__*/createContext({
id: '',
radioGroupState: {},
selectRadioItem: noop
});
/**
* __Dropdown item radio group__
* Store which manages the selection state for each DropdownItemRadio
* across mount and unmounts.
*
*/
var DropdownItemRadioGroup = function DropdownItemRadioGroup(_ref) {
var children = _ref.children,
hasSeparator = _ref.hasSeparator,
id = _ref.id,
isList = _ref.isList,
isScrollable = _ref.isScrollable,
testId = _ref.testId,
title = _ref.title,
rest = _objectWithoutProperties(_ref, _excluded);
var _useContext = useContext(SelectionStoreContext),
setGroupState = _useContext.setGroupState,
getGroupState = _useContext.getGroupState;
var uid = useId();
var titleId = "dropdown-menu-item-radio-group-title-".concat(uid);
/**
* - initially `radioGroupState` is from selection store, so it's safe to update without re-rendering
* - we flush a render by updating this local radio group state
*/
var _useState = useState(function () {
return getGroupState(id);
}),
_useState2 = _slicedToArray(_useState, 2),
radioGroupState = _useState2[0],
setRadioGroupState = _useState2[1];
var selectRadioItem = function selectRadioItem(childId, value) {
var newValue = _objectSpread(_objectSpread({}, resetOptionsInGroup(getGroupState(id))), {}, _defineProperty({}, childId, value));
setRadioGroupState(newValue);
setGroupState(id, newValue);
};
return /*#__PURE__*/React.createElement(RadioGroupContext.Provider, {
value: {
id: id,
radioGroupState: radioGroupState,
selectRadioItem: selectRadioItem
}
}, /*#__PURE__*/React.createElement(Section, _extends({
hasSeparator: hasSeparator,
id: id,
isList: isList,
isScrollable: isScrollable,
testId: testId,
titleId: title ? titleId : undefined
// eslint-disable-next-line @repo/internal/react/no-unsafe-spread-props
}, rest), title && /*#__PURE__*/React.createElement(GroupTitle, {
id: titleId,
title: title
}), children));
};
export default DropdownItemRadioGroup;