import { useEffect, useRef } from 'react'; import { ListState, Node, OverlayTriggerState } from '../react-aria'; // from react-stately import { SvgIcon } from '../Icons'; import { useOption } from '../react-aria'; // from react-aria import classNames from 'classnames'; export function getOptionId(rootId: string, index: number): string { return `${rootId}__item--${index}`; } export interface DropdownMenuOptionProps { componentClass: string; item: Node; state: ListState & OverlayTriggerState; rootId?: string; } export function DropdownMenuOption({ componentClass, item, state, rootId, }: DropdownMenuOptionProps) { const ref = useRef(null); const { optionProps, isSelected, isFocused, isDisabled } = useOption( { key: item.key, shouldSelectOnPressUp: false }, state, ref ); const selectedIndicator = ( ); const { textValue, ...extraAttrs } = item.props; // Work around [this issue](https://github.com/adobe/react-spectrum/issues/4974) by manually // scrolling the selected option into view. At the time of writing this, we are using // `@react-aria/selection` version 3.16.1 useEffect(() => { if (state.isOpen && isSelected) { ref.current?.scrollIntoView({ block: 'nearest' }); } }, [isSelected, state.isOpen]); return (
  • {isSelected && selectedIndicator} {/* Wrapping in a span fixes an issue with Google Translate */} {item.rendered}
  • ); } export default DropdownMenuOption;