Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ListGroupItem): prevent a div with a href #6462

Merged
merged 5 commits into from Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ListGroupItem.tsx
@@ -1,6 +1,7 @@
import classNames from 'classnames';
import * as React from 'react';
import PropTypes from 'prop-types';
import warning from 'warning';
import useEventCallback from '@restart/hooks/useEventCallback';
import {
useNavItem,
Expand Down Expand Up @@ -101,6 +102,11 @@ const ListGroupItem: BsPrefixRefForwardingComponent<'a', ListGroupItemProps> =
// eslint-disable-next-line no-nested-ternary
const Component = as || (action ? (props.href ? 'a' : 'button') : 'div');

warning(
as || !(!action && props.href),
'`action=false` and `href` should not be used together.',
);

return (
<Component
ref={ref}
Expand Down
26 changes: 26 additions & 0 deletions test/ListGroupItemSpec.tsx
@@ -1,6 +1,7 @@
import { fireEvent, render } from '@testing-library/react';
import { expect } from 'chai';
import sinon from 'sinon';
import { shouldWarn } from './helpers';

import ListGroupItem from '../src/ListGroupItem';

Expand Down Expand Up @@ -89,6 +90,31 @@ describe('<ListGroupItem>', () => {
item.classList.contains('list-group-item-action').should.be.true;
expect(item.getAttribute('href')).to.be.equal('/foo');
});
it('renders a div and show warning', () => {
shouldWarn('together');
const { getByTestId } = render(
<ListGroupItem action={false} href="/foo" data-testid="test" />,
);

const item = getByTestId('test');
item.tagName.toLowerCase().should.equal('div');
item.classList.contains('list-group-item-action').should.be.false;
expect(item.getAttribute('href')).to.be.equal('/foo');
});
it('passes href to custom as components', () => {
const { getByTestId } = render(
<ListGroupItem
as="div"
action={false}
data-testid="test"
href="/foo"
/>,
);
const item = getByTestId('test');
item.tagName.toLowerCase().should.equal('div');
item.classList.contains('list-group-item-action').should.be.false;
expect(item.getAttribute('href')).to.be.equal('/foo');
});
});

describe('onClick', () => {
Expand Down