-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
wip: fix: tablist auto selection #7529
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,8 @@ | |
*/ | ||
|
||
import {act, fireEvent, pointerMap, render, waitFor, within} from '@react-spectrum/test-utils-internal'; | ||
import React from 'react'; | ||
import {Tab, TabList, TabPanel, Tabs} from '../'; | ||
import {Button, Collection, Tab, TabList, TabPanel, Tabs} from '../'; | ||
import React, {useState} from 'react'; | ||
import {TabsExample} from '../stories/Tabs.stories'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
|
@@ -474,4 +474,92 @@ describe('Tabs', () => { | |
expect(innerTabs[0]).toHaveTextContent('One'); | ||
expect(innerTabs[1]).toHaveTextContent('Two'); | ||
}); | ||
|
||
it('can add tabs and keep the current selected key', async () => { | ||
let onSelectionChange = jest.fn(); | ||
function Example(props) { | ||
let [tabs, setTabs] = useState([ | ||
{id: 1, title: 'Tab 1', content: 'Tab body 1'}, | ||
{id: 2, title: 'Tab 2', content: 'Tab body 2'}, | ||
{id: 3, title: 'Tab 3', content: 'Tab body 3'} | ||
]); | ||
|
||
const [selectedTabId, setSelectedTabId] = useState(tabs[0].id); | ||
|
||
let addTab = () => { | ||
const tabId = tabs.length + 1; | ||
|
||
setTabs((prevTabs) => [ | ||
...prevTabs, | ||
{ | ||
id: tabId, | ||
title: `Tab ${tabId}`, | ||
content: `Tab body ${tabId}` | ||
} | ||
]); | ||
|
||
// Use functional update to ensure you're working with the most recent state | ||
setSelectedTabId((prevSelectedTabId) => tabId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't |
||
}; | ||
|
||
let removeTab = () => { | ||
if (tabs.length > 1) { | ||
setTabs((prevTabs) => { | ||
const updatedTabs = prevTabs.slice(0, -1); | ||
// Update selectedTabId to the last remaining tab's ID if the current selected tab is removed | ||
const newSelectedTabId = updatedTabs[updatedTabs.length - 1].id; | ||
setSelectedTabId(newSelectedTabId); | ||
return updatedTabs; | ||
}); | ||
} | ||
}; | ||
|
||
const onSelectionChange = (value) => { | ||
setSelectedTabId(value); | ||
props.onSelectionChange(value); | ||
}; | ||
|
||
return ( | ||
<Tabs selectedKey={selectedTabId} onSelectionChange={onSelectionChange}> | ||
<div style={{display: 'flex'}}> | ||
<TabList aria-label="Dynamic tabs" items={tabs} style={{flex: 1}}> | ||
{(item) => ( | ||
<Tab> | ||
{({isSelected}) => ( | ||
<p | ||
style={{ | ||
color: isSelected ? 'red' : 'black' | ||
}}> | ||
{item.title} | ||
</p> | ||
)} | ||
</Tab> | ||
)} | ||
</TabList> | ||
<div className="button-group"> | ||
<Button onPress={addTab}>Add tab</Button> | ||
<Button onPress={removeTab}>Remove tab</Button> | ||
</div> | ||
</div> | ||
<Collection items={tabs}> | ||
{(item) => ( | ||
<TabPanel | ||
style={{ | ||
borderTop: '2px solid black' | ||
}}> | ||
{item.content} | ||
</TabPanel> | ||
)} | ||
</Collection> | ||
</Tabs> | ||
); | ||
} | ||
render(<Example onSelectionChange={onSelectionChange} />); | ||
await user.tab(); | ||
await user.keyboard('{ArrowRight}'); | ||
await user.tab(); | ||
onSelectionChange.mockClear(); | ||
await user.keyboard('{Enter}'); | ||
expect(onSelectionChange).not.toHaveBeenCalled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we confirm which tab is selected? |
||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need this to also update when
selectedKey
changes, or ifselectionManager.isEmpty
or a bunch of other conditions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I'd like to get more at the root cause
I'd hoped to change the first
if
in the effect to includeand then leave off the dependency array, but it's not working for some reason. probably should have opened as a draft, but I forgot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snowystinger That's weird, that works for me 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? tests all pass?
have I messed up my own logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, all tests pass exactly like this 😅 Also did a clean install and cleared the parcel cache just to make sure...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get 8 failures... even on a brand new pull. lets see if CI disagrees with me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I misunderstood what you meant by all tests 😓
Running the whole test pipeline i also get 8 failures in
Tabs.test.js
. I was running this test file in isolation, so it appears to be some sort of cascading failure because the DOM isn't cleared.Can you check whether running just
Tabs.test.js
also passes for you?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's a cascade failure, we have more behavior tests in React Spectrum, those are the ones failing, for the reasons outlined by @devongovett
The crux of the issue is that the selectedkey is set at the same time as the item with that key is added.
When the selectedkey changes (regardless of the collection changing), we need to check to see if we should set a forced default since "no tab selected" is not an option.
At this point though, the collection isn't updated, that takes one more render pass.
As a result, the new key doesn't exist when we go to check if we need to set a default, so we set a default which triggers onSelectionChange.
Then the collection is ready, but it's too late.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see now, thanks 👍