Skip to content

Commit

Permalink
Make ViewToken properly typed
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap authored and yayvery committed Aug 22, 2023
1 parent de760dd commit 91d56d0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

- Update types to match `react-native@0.72` view types.
- https://github.com/Shopify/flash-list/pull/890
- Add type definition for `ViewToken["item"]`
- https://github.com/Shopify/flash-list/pull/817

## [1.5.0] - 2023-07-12

Expand Down
10 changes: 5 additions & 5 deletions documentation/docs/fundamentals/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,17 @@ This event is raised once the list has drawn items on the screen. It also report
### `onViewableItemsChanged`

```tsx
interface ViewToken {
interface ViewToken<TItem> {
index: number;
isViewable: boolean;
item: string;
item: TItem;
key: string;
timestamp: number;
}

onViewableItemsChanged?: ((info: {
viewableItems: ViewToken[];
changed: ViewToken[];
viewableItems: ViewToken<TItem>[];
changed: ViewToken<TItem>[];
}) => void) | null | undefined
```

Expand Down Expand Up @@ -518,7 +518,7 @@ type ViewabilityConfigCallbackPairs = ViewabilityConfigCallbackPair[];
interface ViewabilityConfigCallbackPair {
viewabilityConfig: ViewabilityConfig;
onViewableItemsChanged:
| ((info: { viewableItems: ViewToken[]; changed: ViewToken[] }) => void)
| ((info: { viewableItems: ViewToken<TItem>[]; changed: ViewToken<TItem>[] }) => void)
| null;
}

Expand Down
5 changes: 4 additions & 1 deletion src/FlashListProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ export interface FlashListProps<TItem> extends ScrollViewProps {
* they might be deferred until JS thread is less busy.
*/
onViewableItemsChanged?:
| ((info: { viewableItems: ViewToken[]; changed: ViewToken[] }) => void)
| ((info: {
viewableItems: ViewToken<TItem>[];
changed: ViewToken<TItem>[];
}) => void)
| null
| undefined;

Expand Down
13 changes: 12 additions & 1 deletion src/MasonryFlashList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface MasonryFlashListProps<T>
| "onBlankArea"
| "renderItem"
| "viewabilityConfigCallbackPairs"
| "onViewableItemsChanged"
> {
/**
* Allows you to change the column widths of the list. This is helpful if you want some columns to be wider than the others.
Expand All @@ -59,6 +60,14 @@ export interface MasonryFlashListProps<T>
* longer be linearly distributed across the columns; instead they are allocated to the column with the least estimated height.
*/
renderItem: MasonryListRenderItem<T> | null | undefined;

onViewableItemsChanged?:
| ((info: {
viewableItems: ViewToken<MasonryListItem<T>>[];
changed: ViewToken<MasonryListItem<T>>[];
}) => void)
| null
| undefined;
}

type OnScrollCallback = ScrollViewProps["onScroll"];
Expand Down Expand Up @@ -434,7 +443,9 @@ const getFlashListScrollView = (
FlashListScrollView.displayName = "FlashListScrollView";
return FlashListScrollView;
};
const updateViewTokens = (tokens: ViewToken[]) => {
const updateViewTokens = <T extends MasonryListItem<any>>(
tokens: ViewToken<T | undefined>[]
) => {
const length = tokens.length;
for (let i = 0; i < length; i++) {
const token = tokens[i];
Expand Down
4 changes: 2 additions & 2 deletions src/viewability/ViewToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default interface ViewToken {
item: any;
export default interface ViewToken<TItem> {
item: TItem;
key: string;
index: number | null;
isViewable: boolean;
Expand Down
12 changes: 6 additions & 6 deletions src/viewability/ViewabilityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ export default class ViewabilityManager<T> {
private createViewabilityHelper = (
viewabilityConfig: ViewabilityConfig | null | undefined,
onViewableItemsChanged:
| ((info: { viewableItems: ViewToken[]; changed: ViewToken[] }) => void)
| ((info: {
viewableItems: ViewToken<T>[];
changed: ViewToken<T>[];
}) => void)
| null
| undefined
) => {
const mapViewToken: (index: number, isViewable: boolean) => ViewToken = (
index: number,
isViewable: boolean
) => {
const mapViewToken = (index: number, isViewable: boolean) => {
const item = this.flashListRef.props.data?.[index];
const key =
item === undefined || this.flashListRef.props.keyExtractor === undefined
Expand All @@ -114,7 +114,7 @@ export default class ViewabilityManager<T> {
item,
key,
timestamp: Date.now(),
};
} as ViewToken<T>;
};
return new ViewabilityHelper(
viewabilityConfig,
Expand Down

0 comments on commit 91d56d0

Please sign in to comment.