Skip to content

Commit

Permalink
fix(Tabs): do not dismiss disabled tabs (#13919)
Browse files Browse the repository at this point in the history
* fix(Tabs): do not dismiss disabled tabs

* fix(Tabs): fix typo

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
francinelucca and kodiakhq[bot] authored Jun 6, 2023
1 parent d1ce510 commit 9f726ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
29 changes: 16 additions & 13 deletions packages/react/src/components/Tabs/Tabs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ First, we'll want to import the components from the react package.
import { Tabs, TabList, Tab, TabPanels, TabPanel } from '@carbon/react';
```

In order to be able to "close" or "remove" tabs, we'll need to access and
render your tabs programmatically. For such purposes we'll need to store your
tabs in an array variable and put them in the state
In order to be able to "close" or "remove" tabs, we'll need to access and render
your tabs programmatically. For such purposes we'll need to store your tabs in
an array variable and put them in the state

```js
const tabs = [
Expand All @@ -91,13 +91,13 @@ const tabs = [
const [renderedTabs, setRenderedTabs] = useState(tabs);
```

The `Tabs` component accepts an `onTabCloseRequest` prop function that will forward
us the index of the requested tab to be closed. You may use this function to
open up a confirmation modal previous to deleting the tab; in our case, we'll
just go ahead and remove that tab directly from the `renderedTab`s array and
cover for some `selectedIndex` edge cases (i.e., when a tab is removed from the
array, the selectedIndex might need to be updated to point to a new tab or
index).
The `Tabs` component accepts an `onTabCloseRequest` prop function that will
forward us the index of the requested tab to be closed. You may use this
function to open up a confirmation modal previous to deleting the tab; in our
case, we'll just go ahead and remove that tab directly from the `renderedTab`s
array and cover for some `selectedIndex` edge cases (i.e., when a tab is removed
from the array, the selectedIndex might need to be updated to point to a new tab
or index).

In order to be able to modify the `selectedIndex` on our end we'll need to use
the `Tabs` component in a controlled state, which means we'll also have to
Expand All @@ -112,6 +112,11 @@ const handleTabChange = (evt) => {
};

const handleCloseTabRequest = (tabIndex) => {
// ignore close requests on disabled tabs
if (renderedTabs[tabIndex].disabled) {
return;
}

const selectedTab = renderedTabs[selectedIndex];

const filteredTabs = renderedTabs.filter((_, index) => index !== tabIndex);
Expand Down Expand Up @@ -154,9 +159,7 @@ return (
onTabCloseRequest={handleCloseTabRequest}>
<TabList aria-label="List of tabs">
{renderedTabs.map((tab, index) => (
<Tab
key={index}
disabled={tab.disabled}>
<Tab key={index} disabled={tab.disabled}>
{tab.label}
</Tab>
))}
Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/components/Tabs/Tabs.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export const Dismissable = () => {
};

const handleCloseTabRequest = (tabIndex) => {
if (renderedTabs[tabIndex].disabled) {
return;
}
const selectedTab = renderedTabs[selectedIndex];

const filteredTabs = renderedTabs.filter((_, index) => index !== tabIndex);
Expand Down Expand Up @@ -168,6 +171,9 @@ export const DismissableWithIcons = ({ contained }) => {
};

const handleCloseTabRequest = (tabIndex) => {
if (renderedTabs[tabIndex].disabled) {
return;
}
const selectedTab = renderedTabs[selectedIndex];

const filteredTabs = renderedTabs.filter((_, index) => index !== tabIndex);
Expand Down
1 change: 0 additions & 1 deletion packages/styles/scss/components/tabs/_tabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ $icon-tab-size: custom-property.get-var('icon-tab-size', rem(40px));
.#{$prefix}--tabs__nav-item--close-icon {
padding: $spacing-02;
margin: -$spacing-02;
cursor: pointer;
line-height: 0;
pointer-events: auto;
}
Expand Down

0 comments on commit 9f726ac

Please sign in to comment.