Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ VirtualizedGrids only support vertical orientation (vertically scrollable), but
| `descendingArrowContainerStyle` | `ViewStyle` | For web TVs cursor handling. Style of the view which wraps the descending arrow. Hover this view will trigger the scroll. |
| `scrollInterval` | `number` | For web TVs cursor handling. Speed of the pointer scroll. It represents the interval in ms between every item scrolled. Default value is set to 100. |

The `SpatialNavigationVirtualizedGrid` component ref expose the following methods:

| Name | Type | Description |
| ---------- | ------------------------- | -------------------------------------------------------------- |
| `focus` | `(index: number) => void` | Give the focus to the selected node and scroll if needed. |
| `scrollTo` | `(index: number) => void` | Scroll to the selected node if needed, without appyling focus. |

## Example Usage

```jsx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useSpatialNavigator } from '../../context/SpatialNavigatorContext';
import { ParentIdContext, useParentId } from '../../context/ParentIdContext';
import { typedMemo } from '../../helpers/TypedMemo';
import { convertToGrid } from './helpers/convertToGrid';
import { SpatialNavigationVirtualizedGridRef } from '../../types/SpatialNavigationVirtualizedGridRef';

type SpatialNavigationVirtualizedGridProps<T> = Pick<
SpatialNavigationVirtualizedListWithScrollProps<T>,
Expand All @@ -35,6 +36,7 @@ type SpatialNavigationVirtualizedGridProps<T> = Pick<
numberOfColumns: number;
/** Used to modify every row style */
rowContainerStyle?: ViewStyle;
ref?: React.LegacyRef<SpatialNavigationVirtualizedGridRef>;
};

export type GridRowType<T> = {
Expand Down Expand Up @@ -194,6 +196,7 @@ export const SpatialNavigationVirtualizedGrid = typedMemo(
onEndReachedThresholdRowsNumber,
nbMaxOfItems,
rowContainerStyle,
ref,
...props
}: SpatialNavigationVirtualizedGridProps<T>) => {
if (header && !headerSize) throw new Error('You must provide a headerSize when using a header');
Expand Down Expand Up @@ -247,6 +250,7 @@ export const SpatialNavigationVirtualizedGrid = typedMemo(

return (
<SpatialNavigationVirtualizedList
ref={ref}
data={gridRowsWithHeaderIfProvided}
itemSize={itemSize}
additionalItemsRendered={additionalRenderedRows}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,15 @@ export const SpatialNavigationVirtualizedListWithScroll = typedMemo(
[deviceTypeRef],
);

const scrollTo = useCallback((index: number) => {
if (idRef.current) {
const newId = idRef.current.getNthVirtualNodeID(index);
spatialNavigator.grabFocusDeferred(newId);
}
}, [idRef, spatialNavigator]);
const scrollTo = useCallback(
(index: number) => {
if (idRef.current) {
const newId = idRef.current.getNthVirtualNodeID(index);
spatialNavigator.grabFocusDeferred(newId);
}
},
[idRef, spatialNavigator],
);

useImperativeHandle(
ref,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SpatialNavigationVirtualizedListRef } from './SpatialNavigationVirtualizedListRef';

export type SpatialNavigationVirtualizedGridRef = SpatialNavigationVirtualizedListRef;