RangeInput
<RangeInput attribute={string} // Optional parameters defaultRefinement={object} min={number} max={number} precision={number} translations={object} />
About this widget
The RangeInput
allows a user to select a numeric range using a minimum and maximum input.
Requirements
The attribute provided to the widget must be added in attributes for faceting, either on the dashboard or using attributesForFaceting with the API. The values inside the attribute must be numbers, not strings.
Examples
1
2
3
import { RangeInput } from 'react-instantsearch-dom';
<RangeInput attribute="price" />
Props
attribute
|
type: string
Required
The name of the attribute in the record. |
||
Copy
|
|||
defaultRefinement
|
type: object
Optional
The default state of the widget containing the |
||
Copy
|
|||
min
|
type: number
Optional
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
Optional
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
Optional
The number of digits after the decimal point to use. |
||
Copy
|
|||
translations
|
type: object
Optional
A mapping of keys to translation values.
|
||
Copy
|
Customize the UI - connectRange
If you want to create your own UI of the RangeInput
widget or use another UI library, you can use connectors.
Connectors are higher-order components. They encapsulate the logic for a specific kind of widget and they provide a way to interact with the InstantSearch context.
They have an outer component API that we call exposed props, and they provide some other props to the wrapped components which are called the provided props.
This connector is also used to build other widgets: RangeSlider RatingMenu
It’s a 3-step process:
// 1. Create a React component
const RangeInput = () => {
// return the DOM output
};
// 2. Connect the component using the connector
const CustomRangeInput = connectRange(RangeInput);
// 3. Use your connected widget
<CustomRangeInput />
Create a React component
const RangeInput = ({
object currentRefinement,
number min,
number max,
number precision,
function refine,
__function_ createURL,
}) => {
// return the DOM output
};
Provided Props
currentRefinement
|
type: object
The currently applied refinement or the minimum/maximum value. |
||
Copy
|
|||
min
|
type: number
The minimum available value. |
||
Copy
|
|||
max
|
type: number
The maximum available value. |
||
Copy
|
|||
precision
|
type: number
The number of digits after the decimal point to use. |
||
Copy
|
|||
refine
|
type: function
Selects a range. |
||
Copy
|
|||
createURL
|
type: function
Generates a URL for the corresponding search state. |
||
Copy
|
Create and instantiate your connected widget
const CustomRangeInput = connectRange(RangeInput);
<CustomRangeInput
attribute={string}
// Optional parameters
defaultRefinement={object}
min={number}
max={number}
precision={number}
/>
Exposed Props
attribute
|
type: string
Required
The name of the attribute in the record. |
||
Copy
|
|||
defaultRefinement
|
type: object
Optional
The default state of the widget containing the |
||
Copy
|
|||
min
|
type: number
Optional
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
Optional
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
Optional
The number of digits after the decimal point to use. |
||
Copy
|
Full 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
import { connectRange } from 'react-instantsearch-dom';
const RangeInput = ({ currentRefinement, min, max, precision, refine }) => (
<form>
<input
type="number"
min={min}
max={max}
step={1 / Math.pow(10, precision)}
value={currentRefinement.min || ''}
onChange={event =>
refine({
...currentRefinement,
min: event.currentTarget.value,
})
}
/>
{' - '}
<input
type="number"
min={min}
max={max}
step={1 / Math.pow(10, precision)}
value={currentRefinement.max || ''}
onChange={event =>
refine({
...currentRefinement,
max: event.currentTarget.value,
})
}
/>
</form>
);
const CustomRangeInput = connectRange(RangeInput);
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div class="ais-RangeInput">
<form class="ais-RangeInput-form">
<label class="ais-RangeInput-label">
<input
class="ais-RangeInput-input ais-RangeInput-input--min"
type="number"
placeholder=""
step="1"
/>
</label>
<span class="ais-RangeInput-separator">to</span>
<label class="ais-RangeInput-label">
<input
class="ais-RangeInput-input ais-RangeInput-input--max"
type="number"
placeholder=""
step="1"
/>
</label>
<button class="ais-RangeInput-submit" type="submit">ok</button>
</form>
</div>