1
1
import classNames from 'classnames' ;
2
2
import * as React from 'react' ;
3
- import { useCallback } from 'react' ;
4
3
import PropTypes from 'prop-types' ;
5
- import BaseNavItem , {
4
+ import useEventCallback from '@restart/hooks/useEventCallback' ;
5
+ import {
6
+ useNavItem ,
6
7
NavItemProps as BaseNavItemProps ,
7
8
} from '@restart/ui/NavItem' ;
9
+ import { makeEventKey } from '@restart/ui/SelectableContext' ;
8
10
import { useBootstrapPrefix } from './ThemeProvider' ;
9
11
import { BsPrefixProps , BsPrefixRefForwardingComponent } from './helpers' ;
10
12
import { Variant } from './types' ;
@@ -58,59 +60,57 @@ const propTypes = {
58
60
as : PropTypes . elementType ,
59
61
} ;
60
62
61
- const defaultProps = {
62
- variant : undefined ,
63
- active : false ,
64
- disabled : false ,
65
- } ;
66
-
67
63
const ListGroupItem : BsPrefixRefForwardingComponent < 'a' , ListGroupItemProps > =
68
64
React . forwardRef < HTMLElement , ListGroupItemProps > (
69
65
(
70
66
{
71
67
bsPrefix,
72
68
active,
73
69
disabled,
70
+ eventKey,
74
71
className,
75
72
variant,
76
73
action,
77
74
as,
78
- onClick,
79
75
...props
80
76
} ,
81
77
ref ,
82
78
) => {
83
79
bsPrefix = useBootstrapPrefix ( bsPrefix , 'list-group-item' ) ;
80
+ const [ navItemProps , meta ] = useNavItem ( {
81
+ key : makeEventKey ( eventKey , props . href ) ,
82
+ active,
83
+ ...props ,
84
+ } ) ;
84
85
85
- const handleClick = useCallback (
86
- ( event ) => {
87
- if ( disabled ) {
88
- event . preventDefault ( ) ;
89
- event . stopPropagation ( ) ;
90
- return ;
91
- }
92
-
93
- onClick ?.( event ) ;
94
- } ,
95
- [ disabled , onClick ] ,
96
- ) ;
86
+ const handleClick = useEventCallback ( ( event ) => {
87
+ if ( disabled ) {
88
+ event . preventDefault ( ) ;
89
+ event . stopPropagation ( ) ;
90
+ return ;
91
+ }
92
+
93
+ navItemProps . onClick ( event ) ;
94
+ } ) ;
97
95
98
96
if ( disabled && props . tabIndex === undefined ) {
99
97
props . tabIndex = - 1 ;
100
98
props [ 'aria-disabled' ] = true ;
101
99
}
102
100
101
+ // eslint-disable-next-line no-nested-ternary
102
+ const Component = as || ( action ? ( props . href ? 'a' : 'button' ) : 'div' ) ;
103
+
103
104
return (
104
- < BaseNavItem
105
+ < Component
105
106
ref = { ref }
106
107
{ ...props }
107
- // eslint-disable-next-line no-nested-ternary
108
- as = { as || ( action ? ( props . href ? 'a' : 'button' ) : 'div' ) }
108
+ { ...navItemProps }
109
109
onClick = { handleClick }
110
110
className = { classNames (
111
111
className ,
112
112
bsPrefix ,
113
- active && 'active' ,
113
+ meta . isActive && 'active' ,
114
114
disabled && 'disabled' ,
115
115
variant && `${ bsPrefix } -${ variant } ` ,
116
116
action && `${ bsPrefix } -action` ,
@@ -121,7 +121,6 @@ const ListGroupItem: BsPrefixRefForwardingComponent<'a', ListGroupItemProps> =
121
121
) ;
122
122
123
123
ListGroupItem . propTypes = propTypes ;
124
- ListGroupItem . defaultProps = defaultProps ;
125
124
ListGroupItem . displayName = 'ListGroupItem' ;
126
125
127
126
export default ListGroupItem ;
0 commit comments