Skip to content

Commit

Permalink
fix: tab deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Manson authored and neolitec committed Oct 4, 2024
1 parent d0ec349 commit e65c12e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ const Tabs = ({

const idsToCreate = tabNames.length - tabIds.current.length

tabIds.current.push(
...Array.from(new Array(idsToCreate)).map(
(_, index) => `${id}-${tabNames.length + index - 1}`,
),
)
if (idsToCreate > 0) {
tabIds.current.push(
...Array.from(new Array(idsToCreate)).map(
(_, index) => `${id}-${tabNames.length + index - 1}`,
),
)
}

return React.Children.map(children, (child) => {
if (isTabListElement(child)) {
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/Tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const ui = {
prevButton: byRole('button', { name: 'Prev' }),
nextButton: byRole('button', { name: 'Next' }),
addButton: byRole('button', { name: 'Add' }),
removeButton: byRole('button', { name: 'Remove' }),
externalButton: byRole('button', { name: 'Action' }),
externalParagraph: byText('text text'),
asyncTab: byRole('tab', { name: 'Async' }),
Expand Down Expand Up @@ -290,6 +291,17 @@ describe('Tabs', () => {
})
})
})

describe('when removing a tab', () => {
beforeEach(async () => {
expect(ui.tab2.get()).toBeInTheDocument()
await userEvent.click(ui.removeButton.get())
})

it('should remove the second tab', () => {
expect(ui.tab2.query()).not.toBeInTheDocument()
})
})
})

describe('keyboard management', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/suts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export function displayComponentWithControls() {
setTabs([...tabs, `Tab ${tabs.length + 1}`])
}

const remove = () => {
setTabs(tabs.slice(0, tabs.length - 1))
}

return (
<>
<Tabs onSelect={setSelected} selected={selected}>
Expand All @@ -79,6 +83,9 @@ export function displayComponentWithControls() {
<button type="button" onClick={add}>
Add
</button>
<button type="button" onClick={remove}>
Remove
</button>
</>
)
}
Expand Down

0 comments on commit e65c12e

Please sign in to comment.