File size: 4,627 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, { useState } from 'react';

import { cx } from './lib/cx';

import type { useRange } from 'react-instantsearch-hooks';

type RangeRenderState = ReturnType<typeof useRange>;

export type RangeInputProps = Omit<React.ComponentProps<'div'>, 'onSubmit'> &
  Pick<RangeRenderState, 'range' | 'start'> & {
    classNames?: Partial<RangeInputClassNames>;
    disabled: boolean;
    onSubmit: RangeRenderState['refine'];
    step?: number;
    translations: RangeInputTranslations;
  };

export type RangeInputClassNames = {
  /**
   * Class names to apply to the root element
   */
  root: string;
  /**
   * Class names to apply to the root element when there are no refinements possible
   */
  noRefinementRoot: string;
  /**
   * Class names to apply to the form element
   */
  form: string;
  /**
   * Class names to apply to each label element
   */
  label: string;
  /**
   * Class names to apply to each input element
   */
  input: string;
  /**
   * Class names to apply to the minimum input element
   */
  inputMin: string;
  /**
   * Class names to apply to the maximum input element
   */
  inputMax: string;
  /**
   * Class names to apply to the separator element
   */
  separator: string;
  /**
   * Class names to apply to the submit button
   */
  submit: string;
};

export type RangeInputTranslations = {
  /**
   * The label of the separator, between the minimum and maximum inputs
   */
  separatorElementText: string;
  /**
   * The label of the submit button
   */
  submitButtonText: string;
};

// if the default value is undefined, React considers the component uncontrolled initially, which we don't want 0 or NaN as the default value
const unsetNumberInputValue = '' as unknown as number;

export function RangeInput({
  classNames = {},
  range: { min, max },
  start: [minValue, maxValue],
  step = 1,
  disabled,
  onSubmit,
  translations,
  ...props
}: RangeInputProps) {
  const values = {
    min:
      minValue !== -Infinity && minValue !== min
        ? minValue
        : unsetNumberInputValue,
    max:
      maxValue !== Infinity && maxValue !== max
        ? maxValue
        : unsetNumberInputValue,
  };
  const [prevValues, setPrevValues] = useState(values);

  const [{ from, to }, setRange] = useState({
    from: values.min,
    to: values.max,
  });

  if (values.min !== prevValues.min || values.max !== prevValues.max) {
    setRange({ from: values.min, to: values.max });
    setPrevValues(values);
  }

  return (
    <div
      {...props}
      className={cx(
        cx('ais-RangeInput', classNames.root),
        disabled &&
          cx('ais-RangeInput--noRefinement', classNames.noRefinementRoot),
        props.className
      )}
    >
      <form
        className={cx('ais-RangeInput-form', classNames.form)}
        onSubmit={(event) => {
          event.preventDefault();
          onSubmit([from, to]);
        }}
      >
        <label className={cx('ais-RangeInput-label', classNames.label)}>
          <input
            className={cx(
              'ais-RangeInput-input',
              classNames.input,
              'ais-RangeInput-input--min',
              classNames.inputMin
            )}
            type="number"
            min={min}
            max={max}
            value={from?.toString()} // Strips leading `0` from a positive number value
            step={step}
            placeholder={min?.toString()}
            disabled={disabled}
            onInput={({ currentTarget }) =>
              setRange({ from: Number(currentTarget.value), to })
            }
          />
        </label>
        <span className={cx('ais-RangeInput-separator', classNames.separator)}>
          {translations.separatorElementText}
        </span>
        <label className={cx('ais-RangeInput-label', classNames.label)}>
          <input
            className={cx(
              'ais-RangeInput-input',
              classNames.input,
              'ais-RangeInput-input--max',
              classNames.inputMax
            )}
            type="number"
            min={min}
            max={max}
            value={to?.toString()} // Strips leading `0` from a positive number value
            step={step}
            placeholder={max?.toString()}
            disabled={disabled}
            onInput={({ currentTarget }) =>
              setRange({ from, to: Number(currentTarget.value) })
            }
          />
        </label>
        <button
          className={cx('ais-RangeInput-submit', classNames.submit)}
          type="submit"
        >
          {translations.submitButtonText}
        </button>
      </form>
    </div>
  );
}