File size: 1,021 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
import React from 'react';
import { useHitsPerPage } from 'react-instantsearch-hooks';

import { HitsPerPage as HitsPerPageUiComponent } from '../ui/HitsPerPage';

import type { HitsPerPageProps as HitsPerPageUiComponentProps } from '../ui/HitsPerPage';
import type { UseHitsPerPageProps } from 'react-instantsearch-hooks';

type UiProps = Pick<
  HitsPerPageUiComponentProps,
  'items' | 'onChange' | 'currentValue'
>;

export type HitsPerPageProps = Omit<
  HitsPerPageUiComponentProps,
  keyof UiProps
> &
  UseHitsPerPageProps;

export function HitsPerPage({
  items: userItems,
  transformItems,
  ...props
}: HitsPerPageProps) {
  const { items, refine } = useHitsPerPage(
    { items: userItems, transformItems },
    { $$widgetType: 'ais.hitsPerPage' }
  );
  const { value: currentValue } =
    items.find(({ isRefined }) => isRefined)! || {};

  const uiProps: UiProps = {
    items,
    currentValue,
    onChange: (value) => refine(value),
  };

  return <HitsPerPageUiComponent {...props} {...uiProps} />;
}