Skip to content

Commit

Permalink
fix(ListGroupItem): fix .active not being set with activeKey in ListG…
Browse files Browse the repository at this point in the history
…roup (#6002)
  • Loading branch information
kyletsang committed Sep 21, 2021
1 parent ec285fb commit cdaa039
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/ListGroupItem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import classNames from 'classnames';
import * as React from 'react';
import { useCallback } from 'react';
import PropTypes from 'prop-types';
import BaseNavItem, {
import useEventCallback from '@restart/hooks/useEventCallback';
import {
useNavItem,
NavItemProps as BaseNavItemProps,
} from '@restart/ui/NavItem';
import { makeEventKey } from '@restart/ui/SelectableContext';
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
import { Variant } from './types';
Expand Down Expand Up @@ -58,59 +60,57 @@ const propTypes = {
as: PropTypes.elementType,
};

const defaultProps = {
variant: undefined,
active: false,
disabled: false,
};

const ListGroupItem: BsPrefixRefForwardingComponent<'a', ListGroupItemProps> =
React.forwardRef<HTMLElement, ListGroupItemProps>(
(
{
bsPrefix,
active,
disabled,
eventKey,
className,
variant,
action,
as,
onClick,
...props
},
ref,
) => {
bsPrefix = useBootstrapPrefix(bsPrefix, 'list-group-item');
const [navItemProps, meta] = useNavItem({
key: makeEventKey(eventKey, props.href),
active,
...props,
});

const handleClick = useCallback(
(event) => {
if (disabled) {
event.preventDefault();
event.stopPropagation();
return;
}

onClick?.(event);
},
[disabled, onClick],
);
const handleClick = useEventCallback((event) => {
if (disabled) {
event.preventDefault();
event.stopPropagation();
return;
}

navItemProps.onClick(event);
});

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

// eslint-disable-next-line no-nested-ternary
const Component = as || (action ? (props.href ? 'a' : 'button') : 'div');

return (
<BaseNavItem
<Component
ref={ref}
{...props}
// eslint-disable-next-line no-nested-ternary
as={as || (action ? (props.href ? 'a' : 'button') : 'div')}
{...navItemProps}
onClick={handleClick}
className={classNames(
className,
bsPrefix,
active && 'active',
meta.isActive && 'active',
disabled && 'disabled',
variant && `${bsPrefix}-${variant}`,
action && `${bsPrefix}-action`,
Expand All @@ -121,7 +121,6 @@ const ListGroupItem: BsPrefixRefForwardingComponent<'a', ListGroupItemProps> =
);

ListGroupItem.propTypes = propTypes;
ListGroupItem.defaultProps = defaultProps;
ListGroupItem.displayName = 'ListGroupItem';

export default ListGroupItem;
13 changes: 13 additions & 0 deletions test/ListGroupSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mount } from 'enzyme';
import { render } from '@testing-library/react';

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

Expand Down Expand Up @@ -51,4 +52,16 @@ describe('<ListGroup>', () => {
it('accepts as prop', () => {
mount(<ListGroup as="ul" />).assertSingle('ul.list-group');
});

it('should set active class on list item if activeKey set on parent', () => {
const { getByTestId } = render(
<ListGroup activeKey="1">
<ListGroup.Item eventKey="1" data-testid="list-item">
test
</ListGroup.Item>
</ListGroup>,
);

getByTestId('list-item').classList.contains('active').should.be.true;
});
});

0 comments on commit cdaa039

Please sign in to comment.