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

wip: fix: tablist auto selection #7529

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

snowystinger
Copy link
Member

@snowystinger snowystinger commented Dec 17, 2024

Closes #7526

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

🧢 Your Project:

@rspbot
Copy link

rspbot commented Dec 17, 2024

@snowystinger snowystinger changed the title Fix tablist auto selection fix: tablist auto selection Dec 17, 2024
]);

// Use functional update to ensure you're working with the most recent state
setSelectedTabId((prevSelectedTabId) => tabId);
Copy link
Member

Choose a reason for hiding this comment

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

Why isn't prevSelectedTabId not being used a lint error?
Does the most recent state matter if we're setting the state to the newly created tabId?

await user.tab();
onSelectionChange.mockClear();
await user.keyboard('{Enter}');
expect(onSelectionChange).not.toHaveBeenCalled();
Copy link
Member

@ktabors ktabors Dec 17, 2024

Choose a reason for hiding this comment

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

Should we confirm which tab is selected?

@@ -56,7 +56,7 @@ export function useTabListState<T extends object>(props: TabListStateOptions<T>)
selectionManager.setFocusedKey(selectedKey);
}
lastSelectedKey.current = selectedKey;
});
}, [collection]);
Copy link
Member

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 if selectionManager.isEmpty or a bunch of other conditions?

Copy link
Member Author

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 include

lastCollection.current !== collection

and then leave off the dependency array, but it's not working for some reason. probably should have opened as a draft, but I forgot

Copy link
Contributor

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 🤷

Copy link
Member Author

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?

  let lastSelectedKey = useRef(currentSelectedKey);
  let lastCollection = useRef(collection);
  useEffect(() => {
    // Ensure a tab is always selected (in case no selected key was specified or if selected item was deleted from collection)
    let selectedKey = currentSelectedKey;
    if (lastCollection.current !== collection && (selectionManager.isEmpty || selectedKey == null || !collection.getItem(selectedKey))) {
...
    lastSelectedKey.current = selectedKey;
    lastCollection.current = collection;
  });

Copy link
Contributor

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...

Copy link
Member Author

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.

Copy link
Contributor

@nwidynski nwidynski Dec 17, 2024

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?

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see now, thanks 👍

@snowystinger snowystinger changed the title fix: tablist auto selection wip: fix: tablist auto selection Dec 17, 2024
@rspbot
Copy link

rspbot commented Dec 17, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The selected tab is not being set correctly [Controlled].
5 participants