Skip to content

Commit

Permalink
feat(VirtualizedList): add index to unindexed data
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecColas committed Jul 11, 2023
1 parent 364f282 commit 394a77f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { useMemo } from 'react';
import { ItemWithIndex } from './VirtualizedList';
import { SpatialNavigationNode } from '../Node';
import {
SpatialNavigationVirtualizedListWithScroll,
SpatialNavigationVirtualizedListWithScrollProps,
} from './SpatialNavigationVirtualizedListWithScroll';
import { typedMemo } from '../../helpers/TypedMemo';
import { addIndex } from './helpers/addIndex';

/**
* Use this component to render spatially navigable virtualized lists.
* This component wraps the virtualized list inside a parent navigation node.
* */
export const SpatialNavigationVirtualizedList = typedMemo(
<T extends ItemWithIndex>(props: SpatialNavigationVirtualizedListWithScrollProps<T>) => {
const indexedData = useMemo(() => addIndex(props.data), [props.data]);

return (
<SpatialNavigationNode orientation={props.orientation ?? 'horizontal'}>
<SpatialNavigationVirtualizedListWithScroll {...props} />
<SpatialNavigationVirtualizedListWithScroll {...props} data={indexedData} />
</SpatialNavigationNode>
);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const addIndex = <T>(array: Array<T>) => {
return array.map((value, index) => ({ index, ...value }));
};

0 comments on commit 394a77f

Please sign in to comment.