@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
255 lines (249 loc) • 7.69 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useFloatingTree } from '@floating-ui/react';
import { useMenuRadioItem } from './useMenuRadioItem.js';
import { useMenuRootContext } from '../root/MenuRootContext.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useBaseUiId } from '../../utils/useBaseUiId.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { useMenuRadioGroupContext } from '../radio-group/MenuRadioGroupContext.js';
import { MenuRadioItemContext } from './MenuRadioItemContext.js';
import { itemMapping } from '../utils/styleHookMapping.js';
import { useCompositeListItem } from '../../composite/list/useCompositeListItem.js';
import { jsx as _jsx } from "react/jsx-runtime";
const InnerMenuRadioItem = /*#__PURE__*/React.forwardRef(function InnerMenuItem(props, forwardedRef) {
const {
checked,
setChecked,
className,
closeOnClick,
disabled = false,
highlighted,
id,
menuEvents,
propGetter,
render,
allowMouseUpTriggerRef,
typingRef,
...other
} = props;
const {
getRootProps
} = useMenuRadioItem({
checked,
setChecked,
closeOnClick,
disabled,
highlighted,
id,
menuEvents,
ref: forwardedRef,
allowMouseUpTriggerRef,
typingRef
});
const state = {
disabled,
highlighted,
checked
};
const {
renderElement
} = useComponentRenderer({
render: render || 'div',
className,
state,
propGetter: externalProps => propGetter(getRootProps(externalProps)),
customStyleHookMapping: itemMapping,
extraProps: other
});
return renderElement();
});
/**
* A menu item that works like a radio button in a given group.
* Renders a `<div>` element.
*
* Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
*/
process.env.NODE_ENV !== "production" ? InnerMenuRadioItem.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @ignore
*/
allowMouseUpTriggerRef: PropTypes.shape({
current: PropTypes.bool.isRequired
}).isRequired,
/**
* @ignore
*/
checked: PropTypes.bool.isRequired,
/**
* @ignore
*/
children: PropTypes.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* Whether to close the menu when the item is clicked.
*/
closeOnClick: PropTypes.bool.isRequired,
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: PropTypes.bool,
/**
* @ignore
*/
highlighted: PropTypes.bool.isRequired,
/**
* @ignore
*/
id: PropTypes.string,
/**
* Overrides the text label to use when the item is matched during keyboard text navigation.
*/
label: PropTypes.string,
/**
* @ignore
*/
menuEvents: PropTypes.shape({
emit: PropTypes.func.isRequired,
off: PropTypes.func.isRequired,
on: PropTypes.func.isRequired
}).isRequired,
/**
* The click handler for the menu item.
*/
onClick: PropTypes.func,
/**
* @ignore
*/
propGetter: PropTypes.func.isRequired,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
/**
* @ignore
*/
setChecked: PropTypes.func.isRequired,
/**
* @ignore
*/
typingRef: PropTypes.shape({
current: PropTypes.bool.isRequired
}).isRequired
} : void 0;
const MemoizedInnerMenuRadioItem = /*#__PURE__*/React.memo(InnerMenuRadioItem);
/**
* A menu item that works like a radio button in a given group.
* Renders a `<div>` element.
*
* Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
*/
const MenuRadioItem = /*#__PURE__*/React.forwardRef(function MenuRadioItem(props, forwardedRef) {
const {
id: idProp,
value,
label,
disabled = false,
closeOnClick = false,
...other
} = props;
const itemRef = React.useRef(null);
const listItem = useCompositeListItem({
label
});
const mergedRef = useForkRef(forwardedRef, listItem.ref, itemRef);
const {
getItemProps,
activeIndex,
allowMouseUpTriggerRef,
typingRef
} = useMenuRootContext();
const id = useBaseUiId(idProp);
const highlighted = listItem.index === activeIndex;
const {
events: menuEvents
} = useFloatingTree();
const {
value: selectedValue,
setValue: setSelectedValue
} = useMenuRadioGroupContext();
// This wrapper component is used as a performance optimization.
// MenuRadioItem reads the context and re-renders the actual MenuRadioItem
// only when it needs to.
const checked = selectedValue === value;
const setChecked = React.useCallback(event => {
setSelectedValue(value, event);
}, [setSelectedValue, value]);
const contextValue = React.useMemo(() => ({
checked,
highlighted,
disabled
}), [checked, highlighted, disabled]);
return /*#__PURE__*/_jsx(MenuRadioItemContext.Provider, {
value: contextValue,
children: /*#__PURE__*/_jsx(MemoizedInnerMenuRadioItem, {
...other,
id: id,
ref: mergedRef,
highlighted: highlighted,
menuEvents: menuEvents,
propGetter: getItemProps,
allowMouseUpTriggerRef: allowMouseUpTriggerRef,
checked: selectedValue === value,
setChecked: setChecked,
typingRef: typingRef,
closeOnClick: closeOnClick
})
});
});
process.env.NODE_ENV !== "production" ? MenuRadioItem.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @ignore
*/
children: PropTypes.node,
/**
* Whether to close the menu when the item is clicked.
* @default false
*/
closeOnClick: PropTypes.bool,
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: PropTypes.bool,
/**
* @ignore
*/
id: PropTypes.string,
/**
* Overrides the text label to use when the item is matched during keyboard text navigation.
*/
label: PropTypes.string,
/**
* The click handler for the menu item.
*/
onClick: PropTypes.func,
/**
* Value of the radio item.
* This is the value that will be set in the MenuRadioGroup when the item is selected.
*/
value: PropTypes.any.isRequired
} : void 0;
export { MenuRadioItem };