Skip to content

Commit

Permalink
Handle a couple extra undefined cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mofojed committed Nov 27, 2024
1 parent ca2f18d commit 2868ee5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/components/src/spectrum/utils/itemUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ export function isNormalizedItemsWithKeysList<
}

return (
!isItemOrSection(node[0]) && typeof node[0] === 'object' && 'key' in node[0]
!isItemOrSection(node[0]) &&
node[0] != null &&
typeof node[0] === 'object' &&
'key' in node[0]
);
}

Expand Down
12 changes: 10 additions & 2 deletions packages/react-hooks/src/SelectionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function isSelectionMaybeInvertedEqual<T>(
*/
export function mapSelection<TItem, TMap>(
selectedItemKeys: Selection,
getItem: (key: React.Key) => KeyedItem<TItem>,
getItem: (key: React.Key) => KeyedItem<TItem> | undefined,
mapItem: (item: KeyedItem<TItem>) => TMap
): SelectionT<TMap> {
if (selectedItemKeys === 'all') {
Expand All @@ -83,7 +83,15 @@ export function mapSelection<TItem, TMap>(

const keys = [...selectedItemKeys.keys()];

return new Set(keys.map(key => mapItem(getItem(key))));
return new Set(
keys.map(key => {
const item = getItem(key);
if (item === undefined) {
throw new Error(`Could not find item with key ${key}`);
}
return mapItem(item);
})
);
}

/**
Expand Down

0 comments on commit 2868ee5

Please sign in to comment.