5
5
* found at http://www.apache.org/licenses/LICENSE-2.0.
6
6
*/
7
7
8
- import React , { AnchorHTMLAttributes , LiHTMLAttributes , useRef } from 'react' ;
8
+ import React , { useEffect , useRef } from 'react' ;
9
9
import { StoryFn } from '@storybook/react' ;
10
10
import classNames from 'classnames' ;
11
11
import {
@@ -29,18 +29,20 @@ interface IUseMenuComponentProps extends MenuReturnValue {
29
29
type MenuItemProps = {
30
30
item : IMenuItemBase ;
31
31
getItemProps : IUseMenuComponentProps [ 'getItemProps' ] ;
32
+ getAnchorProps : IUseMenuComponentProps [ 'getAnchorProps' ] ;
32
33
focusedValue : IUseMenuComponentProps [ 'focusedValue' ] ;
33
34
isSelected ?: boolean ;
34
35
} ;
35
36
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 } ) ;
38
40
39
41
const itemChildren = (
40
42
< >
41
43
< 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 ) && '✓' }
44
46
</ span >
45
47
{ item . label || item . value }
46
48
</ >
@@ -54,16 +56,21 @@ const Item = ({ item, getItemProps, focusedValue, isSelected }: MenuItemProps) =
54
56
'cursor-pointer' : ! item . disabled ,
55
57
'cursor-default' : item . disabled
56
58
} ) }
57
- role = { itemProps . href ? 'none' : undefined }
58
- { ...( ! itemProps . href && ( itemProps as LiHTMLAttributes < HTMLLIElement > ) ) }
59
+ { ...itemProps }
59
60
>
60
- { itemProps . href ? (
61
+ { anchorProps ? (
61
62
< 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
+ ) }
64
71
>
65
72
{ itemChildren }
66
- { ! ! item . isExternal && (
73
+ { anchorProps . target === '_blank' && (
67
74
< >
68
75
< span aria-hidden = "true" > ↗</ span >
69
76
< span className = "sr-only" > (opens in new window)</ span >
@@ -85,11 +92,20 @@ const Component = ({
85
92
getTriggerProps,
86
93
getMenuProps,
87
94
getItemProps,
95
+ getAnchorProps,
88
96
getItemGroupProps,
89
97
getSeparatorProps
90
98
} : MenuReturnValue & UseMenuProps ) => {
91
99
const selectedValues = selection . map ( item => item . value ) ;
92
100
101
+ useEffect ( ( ) => {
102
+ const originalWindowOpen = window . open ;
103
+ window . open = ( ) => null ;
104
+ return ( ) => {
105
+ window . open = originalWindowOpen ;
106
+ } ;
107
+ } , [ ] ) ;
108
+
93
109
return (
94
110
< div className = "relative" >
95
111
< button className = "px-2 py-1" type = "button" { ...getTriggerProps ( ) } >
@@ -116,6 +132,7 @@ const Component = ({
116
132
key = { groupItem . value }
117
133
item = { { ...groupItem } }
118
134
getItemProps = { getItemProps }
135
+ getAnchorProps = { getAnchorProps }
119
136
focusedValue = { focusedValue }
120
137
isSelected = { selectedValues . includes ( groupItem . value ) }
121
138
/>
@@ -141,6 +158,8 @@ const Component = ({
141
158
item = { item }
142
159
focusedValue = { focusedValue }
143
160
getItemProps = { getItemProps }
161
+ getAnchorProps = { getAnchorProps }
162
+ isSelected = { selectedValues . includes ( item . value ) }
144
163
/>
145
164
) ;
146
165
} ) }
0 commit comments