File size: 854 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
import React from 'react';
import { useSortBy } from 'react-instantsearch-hooks';

import { SortBy as SortByUiComponent } from '../ui/SortBy';

import type { SortByProps as SortByUiComponentProps } from '../ui/SortBy';
import type { UseSortByProps } from 'react-instantsearch-hooks';

type UiProps = Pick<SortByUiComponentProps, 'items' | 'value' | 'onChange'>;

export type SortByProps = Omit<SortByUiComponentProps, keyof UiProps> &
  UseSortByProps;

export function SortBy({ items, transformItems, ...props }: SortByProps) {
  const { currentRefinement, options, refine } = useSortBy(
    {
      items,
      transformItems,
    },
    {
      $$widgetType: 'ais.sortBy',
    }
  );

  const uiProps: UiProps = {
    items: options,
    value: currentRefinement,
    onChange: refine,
  };

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