55 * found at http://www.apache.org/licenses/LICENSE-2.0.
66 */
77
8- import React , { AnchorHTMLAttributes , LiHTMLAttributes , useRef } from 'react' ;
8+ import React , { useEffect , useRef } from 'react' ;
99import { StoryFn } from '@storybook/react' ;
1010import classNames from 'classnames' ;
1111import {
@@ -29,18 +29,20 @@ interface IUseMenuComponentProps extends MenuReturnValue {
2929type MenuItemProps = {
3030 item : IMenuItemBase ;
3131 getItemProps : IUseMenuComponentProps [ 'getItemProps' ] ;
32+ getAnchorProps : IUseMenuComponentProps [ 'getAnchorProps' ] ;
3233 focusedValue : IUseMenuComponentProps [ 'focusedValue' ] ;
3334 isSelected ?: boolean ;
3435} ;
3536
36- const Item = ( { item, getItemProps, focusedValue, isSelected } : MenuItemProps ) => {
37- const itemProps = getItemProps ( { item } ) ;
37+ const Item = ( { item, getAnchorProps, getItemProps, focusedValue, isSelected } : MenuItemProps ) => {
38+ const itemProps = getItemProps < HTMLLIElement > ( { item } ) ;
39+ const anchorProps = getAnchorProps ( { item } ) ;
3840
3941 const itemChildren = (
4042 < >
4143 < span className = "inline-flex justify-center items-center w-4" >
42- { item ? .type === 'radio' && ! ! isSelected && '•' }
43- { item ? .type === 'checkbox' && ! ! isSelected && '✓' }
44+ { ! ! isSelected && item . type === 'radio' && '•' }
45+ { ! ! isSelected && ( item . type === 'checkbox' || ! ! item . href ) && '✓' }
4446 </ span >
4547 { item . label || item . value }
4648 </ >
@@ -54,16 +56,21 @@ const Item = ({ item, getItemProps, focusedValue, isSelected }: MenuItemProps) =
5456 'cursor-pointer' : ! item . disabled ,
5557 'cursor-default' : item . disabled
5658 } ) }
57- role = { itemProps . href ? 'none' : undefined }
58- { ...( ! itemProps . href && ( itemProps as LiHTMLAttributes < HTMLLIElement > ) ) }
59+ { ...itemProps }
5960 >
60- { itemProps . href ? (
61+ { anchorProps ? (
6162 < a
62- { ...( itemProps as AnchorHTMLAttributes < HTMLAnchorElement > ) }
63- className = "w-full rounded-sm outline-offset-0 transition-none border-width-none"
63+ { ...anchorProps }
64+ className = { classNames (
65+ ' w-full rounded-sm outline-offset-0 transition-none border-width-none' ,
66+ {
67+ 'text-grey-400' : item . disabled ,
68+ 'cursor-default' : item . disabled
69+ }
70+ ) }
6471 >
6572 { itemChildren }
66- { ! ! item . isExternal && (
73+ { anchorProps . target === '_blank' && (
6774 < >
6875 < span aria-hidden = "true" > ↗</ span >
6976 < span className = "sr-only" > (opens in new window)</ span >
@@ -85,11 +92,20 @@ const Component = ({
8592 getTriggerProps,
8693 getMenuProps,
8794 getItemProps,
95+ getAnchorProps,
8896 getItemGroupProps,
8997 getSeparatorProps
9098} : MenuReturnValue & UseMenuProps ) => {
9199 const selectedValues = selection . map ( item => item . value ) ;
92100
101+ useEffect ( ( ) => {
102+ const originalWindowOpen = window . open ;
103+ window . open = ( ) => null ;
104+ return ( ) => {
105+ window . open = originalWindowOpen ;
106+ } ;
107+ } , [ ] ) ;
108+
93109 return (
94110 < div className = "relative" >
95111 < button className = "px-2 py-1" type = "button" { ...getTriggerProps ( ) } >
@@ -116,6 +132,7 @@ const Component = ({
116132 key = { groupItem . value }
117133 item = { { ...groupItem } }
118134 getItemProps = { getItemProps }
135+ getAnchorProps = { getAnchorProps }
119136 focusedValue = { focusedValue }
120137 isSelected = { selectedValues . includes ( groupItem . value ) }
121138 />
@@ -141,6 +158,8 @@ const Component = ({
141158 item = { item }
142159 focusedValue = { focusedValue }
143160 getItemProps = { getItemProps }
161+ getAnchorProps = { getAnchorProps }
162+ isSelected = { selectedValues . includes ( item . value ) }
144163 />
145164 ) ;
146165 } ) }
0 commit comments