Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/components/UncontrolledTabs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,27 +373,49 @@ const UncontrolledTabs = (props) => {
return false;
}
const {
children, // unused
className,
disabledTabClassName, // unused
className: propClassName,
domRef,
focus, // unused
forceRenderTabPanel, // unused
onSelect, // unused
selectedIndex, // unused
selectedTabClassName, // unused
selectedTabPanelClassName, // unused
environment, // unused
disableUpDownKeys, // unused
disableLeftRightKeys, // unused
...attributes
} = {
...defaultProps,
...props,
};
children, // unused here
disabledTabClassName, // unused here
focus, // unused here
forceRenderTabPanel, // unused here
onSelect, // unused here
selectedIndex, // unused here
selectedTabClassName, // unused here
selectedTabPanelClassName, // unused here
environment, // unused here
disableUpDownKeys, // unused here
disableLeftRightKeys, // unused here
...restProps
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
...restProps
...attributes

} = props;

// Only pass valid DOM attributes and data- attributes
const domAttributes = {};
Object.keys(restProps).forEach((key) => {
if (
key.startsWith('data-') ||
key === 'id' ||
key === 'style' ||
key === 'title' ||
key === 'role' ||
key === 'tabIndex' ||
key === 'aria-label' ||
key === 'aria-labelledby' ||
key === 'aria-describedby' ||
key === 'aria-controls' ||
key === 'aria-selected' ||
key === 'aria-disabled'
) {
domAttributes[key] = restProps[key];
}
});
Comment on lines +392 to +411
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This domAttributes is not needed. We remove all custom props above and pass on everything else.

Suggested change
// Only pass valid DOM attributes and data- attributes
const domAttributes = {};
Object.keys(restProps).forEach((key) => {
if (
key.startsWith('data-') ||
key === 'id' ||
key === 'style' ||
key === 'title' ||
key === 'role' ||
key === 'tabIndex' ||
key === 'aria-label' ||
key === 'aria-labelledby' ||
key === 'aria-describedby' ||
key === 'aria-controls' ||
key === 'aria-selected' ||
key === 'aria-disabled'
) {
domAttributes[key] = restProps[key];
}
});


const className =
propClassName == null ? defaultProps.className : propClassName;
Comment on lines +413 to +414
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const className =
propClassName == null ? defaultProps.className : propClassName;
const className = propClassName ?? defaultProps.className;


return (
<div
{...attributes}
{...domAttributes}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{...domAttributes}
{...attributes}

className={cx(className)}
onClick={handleClick}
onKeyDown={handleKeyDown}
Expand Down
36 changes: 36 additions & 0 deletions src/components/__tests__/Tabs-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import { cleanup, render, screen, within } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import '@testing-library/jest-dom/vitest';
import UncontrolledTabs from '../UncontrolledTabs';
import Tab from '../Tab';
import TabList from '../TabList';
import TabPanel from '../TabPanel';
Expand Down Expand Up @@ -651,3 +652,38 @@ describe('<Tabs />', () => {
assertTabSelected(1);
});
});


test('preserves default className when className is undefined', () => {
const { container } = render(
<UncontrolledTabs
className={undefined}
selectedIndex={0}
onSelect={() => {}}
>
<TabList>
<Tab>Tab</Tab>
</TabList>
<TabPanel>Panel</TabPanel>
</UncontrolledTabs>
);

expect(container.firstChild).toHaveClass('react-tabs');
});

test('preserves default className when className is null', () => {
const { container } = render(
<UncontrolledTabs
className={null}
selectedIndex={0}
onSelect={() => {}}
>
<TabList>
<Tab>Tab</Tab>
</TabList>
<TabPanel>Panel</TabPanel>
</UncontrolledTabs>
);

expect(container.firstChild).toHaveClass('react-tabs');
});