1- import React , { ReactNode , Ref , SyntheticEvent } from 'react' ;
1+ import React , { ReactNode , Ref , SyntheticEvent , useMemo } from 'react' ;
22
33import classNames from 'classnames' ;
44import isEmpty from 'lodash/isEmpty' ;
55
66import { ListProps , Size } from '@lumx/react' ;
77import { GenericProps } from '@lumx/react/utils/type' ;
8+ import { onEnterPressed , onButtonPressed } from '@lumx/core/js/utils' ;
89import { getRootClassName , handleBasicClasses } from '@lumx/core/js/utils/className' ;
10+ import { renderLink } from '@lumx/react/utils/react/renderLink' ;
911import { forwardRef } from '@lumx/react/utils/react/forwardRef' ;
1012import { useDisableStateProps } from '@lumx/react/utils/disabled/useDisableStateProps' ;
1113import { HasAriaDisabled } from '@lumx/react/utils/type/HasAriaDisabled' ;
12- import { RawClickable } from '@lumx/react/utils/react/RawClickable' ;
1314
1415export type ListItemSize = Extract < Size , 'tiny' | 'regular' | 'big' | 'huge' > ;
1516
@@ -93,6 +94,13 @@ export const ListItem = forwardRef<ListItemProps, HTMLLIElement>((props, ref) =>
9394 ...forwardedProps
9495 } = otherProps ;
9596
97+ const role = linkAs || linkProps . href ? 'link' : 'button' ;
98+ const onKeyDown = useMemo ( ( ) => {
99+ if ( onItemSelected && role === 'link' ) return onEnterPressed ( onItemSelected as any ) ;
100+ if ( onItemSelected && role === 'button' ) return onButtonPressed ( onItemSelected as any ) ;
101+ return undefined ;
102+ } , [ role , onItemSelected ] ) ;
103+
96104 const content = (
97105 < >
98106 { before && < div className = { `${ CLASSNAME } __before` } > { before } </ div > }
@@ -115,23 +123,28 @@ export const ListItem = forwardRef<ListItemProps, HTMLLIElement>((props, ref) =>
115123 >
116124 { isClickable ( { linkProps, onItemSelected } ) ? (
117125 /* Clickable list item */
118- < RawClickable
119- as = { linkAs || ( linkProps . href ? 'a' : 'button' ) }
120- { ...linkProps }
121- { ...disabledStateProps }
122- className = { classNames (
123- handleBasicClasses ( {
124- prefix : `${ CLASSNAME } __link` ,
125- isHighlighted,
126- isSelected,
127- isDisabled : isAnyDisabled ,
128- } ) ,
129- ) }
130- onClick = { onItemSelected }
131- ref = { linkRef }
132- >
133- { content }
134- </ RawClickable >
126+ renderLink (
127+ {
128+ linkAs,
129+ tabIndex : ! disabledStateProps . disabled ? 0 : undefined ,
130+ role,
131+ 'aria-disabled' : isAnyDisabled ,
132+ ...linkProps ,
133+ href : isAnyDisabled ? undefined : linkProps . href ,
134+ className : classNames (
135+ handleBasicClasses ( {
136+ prefix : `${ CLASSNAME } __link` ,
137+ isHighlighted,
138+ isSelected,
139+ isDisabled : isAnyDisabled ,
140+ } ) ,
141+ ) ,
142+ onClick : isAnyDisabled ? undefined : onItemSelected ,
143+ onKeyDown : isAnyDisabled ? undefined : onKeyDown ,
144+ ref : linkRef ,
145+ } ,
146+ content ,
147+ )
135148 ) : (
136149 /* Non clickable list item */
137150 < div className = { `${ CLASSNAME } __wrapper` } > { content } </ div >
0 commit comments