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

import { RangeInput as RangeInputUiComponent } from '../ui/RangeInput';

import type { RangeInputProps as RangeInputUiProps } from '../ui/RangeInput';
import type { UseRangeProps } from 'react-instantsearch-hooks';

type UiProps = Pick<
  RangeInputUiProps,
  'disabled' | 'onSubmit' | 'range' | 'start' | 'step' | 'translations'
>;

export type RangeInputProps = Omit<RangeInputUiProps, keyof UiProps> &
  UseRangeProps & { translations?: Partial<UiProps['translations']> };

export function RangeInput({
  attribute,
  min,
  max,
  precision,
  translations,
  ...props
}: RangeInputProps) {
  const { range, start, canRefine, refine } = useRange(
    { attribute, min, max, precision },
    { $$widgetType: 'ais.rangeInput' }
  );

  const step = 1 / Math.pow(10, precision || 0);

  const uiProps: UiProps = {
    range,
    start,
    step,
    disabled: !canRefine,
    onSubmit: refine,
    translations: {
      separatorElementText: 'to',
      submitButtonText: 'Go',
      ...translations,
    },
  };

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