Skip to content

Commit

Permalink
remove forced selection in controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
snowystinger committed Dec 17, 2024
1 parent 93d6770 commit 3183d8c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 4 deletions.
4 changes: 1 addition & 3 deletions packages/@react-stately/tabs/src/useTabListState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ export function useTabListState<T extends object>(props: TabListStateOptions<T>)
} = state;

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))) {
if (props.selectedKey == null && (selectionManager.isEmpty || selectedKey == null || !collection.getItem(selectedKey))) {
selectedKey = findDefaultSelectedKey(collection, state.disabledKeys);
if (selectedKey != null) {
// directly set selection because replace/toggle selection won't consider disabled keys
Expand All @@ -57,7 +56,6 @@ export function useTabListState<T extends object>(props: TabListStateOptions<T>)
selectionManager.setFocusedKey(selectedKey);
}
lastSelectedKey.current = selectedKey;
lastCollection.current = collection;
});

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-aria-components/test/Tabs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ describe('Tabs', () => {
]);

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

let removeTab = () => {
Expand Down

0 comments on commit 3183d8c

Please sign in to comment.