UNPKG

@atlaskit/dropdown-menu

Version:

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

115 lines (114 loc) • 5.15 kB
import _extends from "@babel/runtime/helpers/extends"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["children", "defaultSelected", "testId", "id", "title", "description", "isDisabled", "isSelected", "shortcut", "onClick", "shouldDescriptionWrap", "shouldTitleWrap", "interactionName"]; import React, { useCallback } from 'react'; import noop from '@atlaskit/ds-lib/noop'; import ButtonItem from '@atlaskit/menu/button-item'; import { SELECTION_STYLE_CONTEXT_DO_NOT_USE } from '@atlaskit/menu/selection-style-context-do-not-use'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import NewDropdownMenuItem from '../dropdown-menu-item-new'; import RadioIcon from '../internal/components/radio-icon'; import useRadioState from '../internal/hooks/use-radio-state'; import useRegisterItemWithFocusManager from '../internal/hooks/use-register-item-with-focus-manager'; /** * __Dropdown item radio__ * * A dropdown item radio displays groups that have a single selection. * * - [Examples](https://atlassian.design/components/dropdown-menu/dropdown-item-radio/examples) * - [Code](https://atlassian.design/components/dropdown-menu/dropdown-item-radio/code) * - [Usage](https://atlassian.design/components/dropdown-menu/dropdown-item-radio/usage) */ export var DropdownItemRadio = function DropdownItemRadio(_ref) { var children = _ref.children, defaultSelected = _ref.defaultSelected, testId = _ref.testId, id = _ref.id, title = _ref.title, description = _ref.description, isDisabled = _ref.isDisabled, isSelected = _ref.isSelected, shortcut = _ref.shortcut, _ref$onClick = _ref.onClick, providedOnClick = _ref$onClick === void 0 ? noop : _ref$onClick, _ref$shouldDescriptio = _ref.shouldDescriptionWrap, shouldDescriptionWrap = _ref$shouldDescriptio === void 0 ? true : _ref$shouldDescriptio, _ref$shouldTitleWrap = _ref.shouldTitleWrap, shouldTitleWrap = _ref$shouldTitleWrap === void 0 ? true : _ref$shouldTitleWrap, interactionName = _ref.interactionName, rest = _objectWithoutProperties(_ref, _excluded); if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production' && typeof isSelected !== 'undefined' && typeof defaultSelected !== 'undefined') { // eslint-disable-next-line no-console console.warn("[DropdownItemRadio] You've used both `defaultSelected` and `isSelected` props. This is dangerous and can lead to unexpected results. Use one or the other depending if you want to control the components state yourself."); } var _useRadioState = useRadioState({ id: id, isSelected: isSelected, defaultSelected: defaultSelected }), _useRadioState2 = _slicedToArray(_useRadioState, 2), selected = _useRadioState2[0], setSelected = _useRadioState2[1]; var handleItemClick = useCallback(function (event) { setSelected(function (selected) { return !selected; }); providedOnClick(event); }, [providedOnClick, setSelected]); var itemRef = useRegisterItemWithFocusManager(); // Just to force the toggle in atlaspack workbench // @ts-ignore var _nothing = fg('platform-dst-tokens-finesse'); var radioItem = fg('platform-dst-new-menu') ? /*#__PURE__*/ // eslint-disable-next-line @atlassian/a11y/aria-toggle-field-name -- Linter demands the component has children, but the title prop here handles children which maintains accessibility. React.createElement(NewDropdownMenuItem, { title: children, description: description, elemBefore: /*#__PURE__*/React.createElement(RadioIcon, { checked: selected, disabled: isDisabled }), isSelected: selected, "aria-checked": selected, isDisabled: isDisabled, shouldTitleWrap: shouldTitleWrap, shouldDescriptionWrap: shouldDescriptionWrap, shortcut: shortcut, onClick: handleItemClick, role: "menuitemradio", testId: testId, id: id, ref: itemRef }) : /*#__PURE__*/React.createElement(ButtonItem, _extends({ "aria-checked": selected, description: description, iconBefore: /*#__PURE__*/React.createElement(RadioIcon, { checked: selected }), id: id, isDisabled: isDisabled, isSelected: selected, onClick: handleItemClick, ref: itemRef, role: "menuitemradio", shouldDescriptionWrap: shouldDescriptionWrap, shouldTitleWrap: shouldTitleWrap, testId: testId // Thanks to spread props, this attribute are passed to ButtonItem, even though // it's not in the component's prop types. // @ts-expect-error , title: title, interactionName: interactionName // eslint-disable-next-line @repo/internal/react/no-unsafe-spread-props }, rest, { // Override ButtonItem's default aria-current, as aria-checked is the correct attribute for menuitemradio (A11Y-37930) "aria-current": undefined }), children); return /*#__PURE__*/React.createElement(SELECTION_STYLE_CONTEXT_DO_NOT_USE.Provider, { value: "none" }, radioItem); }; export default DropdownItemRadio;