Skip to content

Commit cdaa039

Browse files
authored
fix(ListGroupItem): fix .active not being set with activeKey in ListGroup (react-bootstrap#6002)
1 parent ec285fb commit cdaa039

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

src/ListGroupItem.tsx

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import classNames from 'classnames';
22
import * as React from 'react';
3-
import { useCallback } from 'react';
43
import PropTypes from 'prop-types';
5-
import BaseNavItem, {
4+
import useEventCallback from '@restart/hooks/useEventCallback';
5+
import {
6+
useNavItem,
67
NavItemProps as BaseNavItemProps,
78
} from '@restart/ui/NavItem';
9+
import { makeEventKey } from '@restart/ui/SelectableContext';
810
import { useBootstrapPrefix } from './ThemeProvider';
911
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
1012
import { Variant } from './types';
@@ -58,59 +60,57 @@ const propTypes = {
5860
as: PropTypes.elementType,
5961
};
6062

61-
const defaultProps = {
62-
variant: undefined,
63-
active: false,
64-
disabled: false,
65-
};
66-
6763
const ListGroupItem: BsPrefixRefForwardingComponent<'a', ListGroupItemProps> =
6864
React.forwardRef<HTMLElement, ListGroupItemProps>(
6965
(
7066
{
7167
bsPrefix,
7268
active,
7369
disabled,
70+
eventKey,
7471
className,
7572
variant,
7673
action,
7774
as,
78-
onClick,
7975
...props
8076
},
8177
ref,
8278
) => {
8379
bsPrefix = useBootstrapPrefix(bsPrefix, 'list-group-item');
80+
const [navItemProps, meta] = useNavItem({
81+
key: makeEventKey(eventKey, props.href),
82+
active,
83+
...props,
84+
});
8485

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+
});
9795

9896
if (disabled && props.tabIndex === undefined) {
9997
props.tabIndex = -1;
10098
props['aria-disabled'] = true;
10199
}
102100

101+
// eslint-disable-next-line no-nested-ternary
102+
const Component = as || (action ? (props.href ? 'a' : 'button') : 'div');
103+
103104
return (
104-
<BaseNavItem
105+
<Component
105106
ref={ref}
106107
{...props}
107-
// eslint-disable-next-line no-nested-ternary
108-
as={as || (action ? (props.href ? 'a' : 'button') : 'div')}
108+
{...navItemProps}
109109
onClick={handleClick}
110110
className={classNames(
111111
className,
112112
bsPrefix,
113-
active && 'active',
113+
meta.isActive && 'active',
114114
disabled && 'disabled',
115115
variant && `${bsPrefix}-${variant}`,
116116
action && `${bsPrefix}-action`,
@@ -121,7 +121,6 @@ const ListGroupItem: BsPrefixRefForwardingComponent<'a', ListGroupItemProps> =
121121
);
122122

123123
ListGroupItem.propTypes = propTypes;
124-
ListGroupItem.defaultProps = defaultProps;
125124
ListGroupItem.displayName = 'ListGroupItem';
126125

127126
export default ListGroupItem;

test/ListGroupSpec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { mount } from 'enzyme';
2+
import { render } from '@testing-library/react';
23

34
import ListGroup from '../src/ListGroup';
45

@@ -51,4 +52,16 @@ describe('<ListGroup>', () => {
5152
it('accepts as prop', () => {
5253
mount(<ListGroup as="ul" />).assertSingle('ul.list-group');
5354
});
55+
56+
it('should set active class on list item if activeKey set on parent', () => {
57+
const { getByTestId } = render(
58+
<ListGroup activeKey="1">
59+
<ListGroup.Item eventKey="1" data-testid="list-item">
60+
test
61+
</ListGroup.Item>
62+
</ListGroup>,
63+
);
64+
65+
getByTestId('list-item').classList.contains('active').should.be.true;
66+
});
5467
});

0 commit comments

Comments
 (0)