File size: 1,548 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React, { forwardRef } from 'react';
import { StyleSheet, View, FlatList } from 'react-native';
import { Hit as AlgoliaHit } from '@algolia/client-search';
import {
  useInfiniteHits,
  UseInfiniteHitsProps,
} from 'react-instantsearch-hooks';

type InfiniteHitsProps<THit> = UseInfiniteHitsProps & {
  hitComponent: (props: { hit: THit }) => JSX.Element;
};

export const InfiniteHits = forwardRef(
  <THit extends AlgoliaHit<Record<string, unknown>>>(
    { hitComponent: Hit, ...props }: InfiniteHitsProps<THit>,
    ref: React.ForwardedRef<FlatList<THit>>
  ) => {
    const { hits, isLastPage, showMore } = useInfiniteHits(props);

    return (
      <FlatList
        ref={ref}
        data={hits as unknown as THit[]}
        keyExtractor={(item) => item.objectID}
        ItemSeparatorComponent={() => <View style={styles.separator} />}
        onEndReached={() => {
          if (!isLastPage) {
            showMore();
          }
        }}
        renderItem={({ item }) => (
          <View style={styles.item}>
            <Hit hit={item as unknown as THit} />
          </View>
        )}
      />
    );
  }
);

const styles = StyleSheet.create({
  separator: {
    borderBottomWidth: 1,
    borderColor: '#ddd',
  },
  item: {
    padding: 18,
  },
});

declare module 'react' {
  // eslint-disable-next-line no-shadow
  function forwardRef<TRef, TProps = unknown>(
    render: (props: TProps, ref: React.Ref<TRef>) => React.ReactElement | null
  ): (props: TProps & React.RefAttributes<TRef>) => React.ReactElement | null;
}