<RangeInput>
This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.
If you were using React InstantSearch v6, you can upgrade to v7.
If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.
If you want to keep using React InstantSearch v6, you can find the archived documentation.
<RangeInput attribute={string} // Optional parameters min={number} max={number} precision={number} classNames={object} translations={object} ...props={ComponentProps<'div'>} />
1
import { RangeInput } from 'react-instantsearch';
About this widget
<RangeInput>
is a widget to let users select a numeric range using minimum and maximum inputs.
You need to specify the attribute
prop in attributes for faceting, either on the dashboard) or using attributesForFaceting
with the API. The attribute values must be numbers, not strings.
You can also create your own UI with
useRange()
.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, RangeInput } from 'react-instantsearch';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<RangeInput attribute="price" />
</InstantSearch>
);
}
Props
Parameter | Description | ||
---|---|---|---|
attribute
|
type: string
Required
The name of the attribute in the records. |
||
Copy
|
|||
min
|
type: number
The minimum value for the input. When not provided, the minimum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
max
|
type: number
The maximum value for the input. When not provided, the maximum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
precision
|
type: number
default: 0
The number of digits to use after the decimal point. |
||
Copy
|
|||
classNames
|
type: Partial<RangeInputClassNames>
CSS classes to pass to the widget’s elements. This is useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.
|
||
Copy
|
|||
translations
|
type: Partial<RangeInputTranslations>
A mapping of keys to translation values.
|
||
Copy
|
|||
...props
|
type: React.ComponentProps<'div'>
Any |
||
Copy
|
Hook
React InstantSearch let you create your own UI for the <RangeInput>
widget with useRange()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useRange()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useRange } from 'react-instantsearch';
function CustomRangeInput(props) {
const {
start,
range,
canRefine,
refine,
sendEvent,
} = useRange(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomRangeInput {...props} />
Parameters
Hooks accept parameters. You can pass them manually, or forward the props from your custom component.
When you provide a function to Hooks, make sure to pass a stable reference to avoid rendering endlessly (for example, with useCallback()
). Objects and arrays are memoized; you don’t need to stabilize them.
Parameter | Description | ||
---|---|---|---|
attribute
|
type: string
Required
The name of the attribute in the records. |
||
Copy
|
|||
min
|
type: number
The minimum value for the input. When not provided, the minimum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
max
|
type: number
The maximum value for the input. When not provided, the maximum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
precision
|
type: number
default: 0
The number of digits to use after the decimal point. |
||
Copy
|
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
Parameter | Description | ||
---|---|---|---|
start
|
type: RangeBoundaries
The current value for the refinement, with
Copy
|
||
range
|
type: Range
The current available value for the range.
Copy
|
||
canRefine
|
type: boolean
Whether the user can further refine. |
||
refine
|
type: (rangeValue: RangeBoundaries) => void
Sets a range to filter the results on. Both values are optional, and default to the higher and lower bounds. You can use
Copy
|
||
sendEvent
|
type: (eventType: string, facetValue: string, eventName?: string) => void
Sends an event to the Insights middleware. |
Example
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
import React, { useState } from 'react';
import { useRange } from 'react-instantsearch';
const unsetNumberInputValue = '';
function CustomRangeInput(props) {
const { start, range, canRefine, precision, refine } = useRange(props);
const step = 1 / Math.pow(10, precision || 0);
const values = {
min:
start[0] !== -Infinity && start[0] !== range.min
? start[0]
: unsetNumberInputValue,
max:
start[1] !== Infinity && start[1] !== range.max
? start[1]
: unsetNumberInputValue,
};
const [prevValues, setPrevValues] = useState(values);
const [{ from, to }, setRange] = useState({
from: values.min?.toString(),
to: values.max?.toString(),
});
if (values.min !== prevValues.min || values.max !== prevValues.max) {
setRange({ from: values.min?.toString(), to: values.max?.toString() });
setPrevValues(values);
}
return (
<form
onSubmit={(event) => {
event.preventDefault();
refine([from ? Number(from) : undefined, to ? Number(to) : undefined]);
}}
>
<label>
<input
type="number"
min={range.min}
max={range.max}
value={stripLeadingZeroFromInput(from || unsetNumberInputValue)}
step={step}
placeholder={range.min?.toString()}
disabled={!canRefine}
onInput={({ currentTarget }) => {
const value = currentTarget.value;
setRange({ from: value || unsetNumberInputValue, to });
}}
/>
</label>
<span>to</span>
<label>
<input
type="number"
min={range.min}
max={range.max}
value={stripLeadingZeroFromInput(to || unsetNumberInputValue)}
step={step}
placeholder={range.max?.toString()}
disabled={!canRefine}
onInput={({ currentTarget }) => {
const value = currentTarget.value;
setRange({ from, to: value || unsetNumberInputValue });
}}
/>
</label>
<button type="submit">Go</button>
</form>
);
}
function stripLeadingZeroFromInput(value) {
return value.replace(/^(0+)\d/, (part) => Number(part).toString());
}