🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
UI libraries / InstantSearch.js / Widgets
Signature
rangeInput({
  container: string|HTMLElement,
  attribute: string,
  // Optional parameters
  min: number,
  max: number,
  precision: number,
  templates: object,
  cssClasses: object,
});
Import
1
import { rangeInput } from 'instantsearch.js/es/widgets';

About this widget

The rangeInput widget allows a user to select a numeric range using a minimum and maximum input.

Requirements

The attribute provided to the widget must be 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
4
rangeInput({
  container: '#range-input',
  attribute: 'price',
});

Options

Parameter Description
container
type: string|HTMLElement
Required

The CSS Selector or HTMLElement to insert the widget into.

1
2
3
4
rangeInput({
  // ...
  container: '#range-input',
});
attribute
type: string
Required

The name of the attribute in the record.

1
2
3
4
rangeInput({
  // ...
  attribute: 'price',
});
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.

1
2
3
4
rangeInput({
  // ...
  min: 10,
});
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.

1
2
3
4
rangeInput({
  // ...
  max: 500,
});
precision
type: number
default: 0
Optional

The number of digits after the decimal point to use.

1
2
3
4
rangeInput({
  // ...
  precision: 2,
});
templates
type: object
Optional

The templates to use for the widget.

1
2
3
4
5
6
rangeInput({
  // ...
  templates: {
    // ...
  },
});
cssClasses
type: object
default: {}
Optional

The CSS classes you can override:

  • root: the root element of the widget.
  • form: the form element.
  • label: the label elements.
  • input: the input elements.
  • inputMin: the minimum input element.
  • inputMax: the maximum input element.
  • separator: the separator element.
  • submit: the submit button.
1
2
3
4
5
6
7
8
9
10
rangeInput({
  // ...
  cssClasses: {
    root: 'MyCustomRangeInput',
    form: [
      'MyCustomRangeInputForm',
      'MyCustomRangeInputForm--subclass',
    ],
  },
});

Templates

You can customize parts of the widget’s UI using the Templates API.

Every template provides an html function you can use as a tagged template. Using html lets you safely provide templates as an HTML string. It works directly in the browser without a build step. See Templating your UI for more information.

The html function is available starting from v4.46.0.

Parameter Description
separatorText
type: string|function
Optional

The template for the separator, between the minimum and maximum inputs.

1
2
3
4
5
6
7
8
rangeInput({
  // ...
  templates: {
    separatorText({ cssClasses }, { html }) {
      return html`<span class="${cssClasses.separator}">to</span>`;
    },
  },
});
submitText
type: string|function
Optional

The template for the submit button.

1
2
3
4
5
6
7
8
rangeInput({
  // ...
  templates: {
    submitText({ cssClasses }, { html }) {
      return html`<span class="${cssClasses.submit}">Go</span>`;
    },
  },
});

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">Go</button>
  </form>
</div>

Customize the UI with connectRange

If you want to create your own UI of the rangeInput widget, you can use connectors.

This connector is also used to build other widgets: RangeSlider

To use connectRange, you can import it with the declaration relevant to how you installed InstantSearch.js.

1
import { connectRange } from 'instantsearch.js/es/connectors';

Then it’s a 3-step process:

// 1. Create a render function
const renderRangeInput = (renderOptions, isFirstRender) => {
  // Rendering logic
};

// 2. Create the custom widget
const customRangeInput = connectRange(
  renderRangeInput
);

// 3. Instantiate
search.addWidgets([
  customRangeInput({
    // instance params
  })
]);

Create a render function

This rendering function is called before the first search (init lifecycle step) and each time results come back from Algolia (render lifecycle step).

const renderRangeInput = (renderOptions, isFirstRender) => {
  const {
    number[] start,
    object range,
    boolean canRefine,
    function refine,
    function sendEvent,
    object widgetParams,
  } = renderOptions;

  if (isFirstRender) {
    // Do some initial rendering and bind events
  }

  // Render the widget
}

Rendering options

Parameter Description
start
type: number[]

The current value for the refinement, with start[0] as the minimum value and start[1] as the maximum value.

1
2
3
4
5
6
7
8
9
10
11
12
const renderRangeInput = (renderOptions, isFirstRender) => {
  const { start } = renderOptions;
  const [min, max] = start;

  document.querySelector('#range-input').innerHTML = `
    <form>
      <input type="number" value="${Number.isFinite(min) ? min : ''}" />
      <span>to</span>
      <input type="number" value="${Number.isFinite(max) ? max : ''}" />
    </form>
  `;
};
range
type: object

The current available value for the range.

1
2
3
4
5
6
7
8
9
10
11
const renderRangeInput = (renderOptions, isFirstRender) => {
  const { range } = renderOptions;

  document.querySelector('#range-input').innerHTML = `
    <form>
      <input type="number" placeholder="${range.min}" />
      <span>to</span>
      <input type="number" placeholder="${range.max}" />
    </form>
  `;
};
canRefine
type: boolean
Required

Indicates if search state can be refined.

1
2
3
4
5
6
7
8
const renderRangeInput = (renderOptions, isFirstRender) => {
  const { range, canRefine } = renderOptions;

  if (!canRefine) {
    document.querySelector('#range-input').innerHTML = '';
    return;
  }
};
refine
type: function

Sets a range to filter the results on. Both values are optional, and default to the higher and lower bounds. You can use undefined to remove a previously set bound or to set an infinite bound.

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
const renderRangeInput = (renderOptions, isFirstRender) => {
  const { start, refine } = renderOptions;
  const [min, max] = start;

  const container = document.querySelector('#range-input');

  if (isFirstRender) {
    const form = document.createElement('form');

    form.addEventListener('submit', event => {
      event.preventDefault();

      const [minInput, maxInput] = event.target.elements;

      const rawMinInputValue = parseFloat(minInput.value);
      const rawMaxInputValue = parseFloat(maxInput.value);

      refine([
        Number.isFinite(rawMinInputValue) ? rawMinInputValue : undefined,
        Number.isFinite(rawMaxInputValue) ? rawMaxInputValue : undefined,
      ]);
    });

    container.appendChild(form);
  }

  container.querySelector('form').innerHTML = `
    <input
      name="min"
      type="number"
      value="${Number.isFinite(min) ? min : ''}"
    />
    <span>to</span>
    <input
      name="max"
      type="number"
      value="${Number.isFinite(max) ? max : ''}"
    />
    <button>Go</button>
  `;
};
sendEvent
type: (eventType, facetValue) => void

The function to send click events. The click event is automatically sent when refine is called. You can learn more about the insights middleware.

  • eventType: 'click'
  • facetValue: string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// For example,
sendEvent('click', [10, 30]);

/*
  A payload like the following will be sent to the `insights` middleware.
  {
    eventType: 'click',
    insightsMethod: 'clickedFilters',
    payload: {
      eventName: 'Filter Applied',
      filters: ['price>=10', 'price<=30'],
      index: '',
    },
    widgetType: 'ais.range',
  }
*/
widgetParams
type: object

All original widget options forwarded to the render function.

1
2
3
4
5
6
7
8
9
10
11
12
13
const renderRangeInput = (renderOptions, isFirstRender) => {
  const { widgetParams } = renderOptions;

  widgetParams.container.innerHTML = '...';
};

// ...

search.addWidgets([
  customRangeInput({
    container: document.querySelector('#range-input'),
  })
]);

Create and instantiate the custom widget

We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:

  • Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
  • Your own parameters: to make the custom widget generic.

Both instance and custom parameters are available in connector.widgetParams, inside the renderFunction.

const customRangeInput = connectRange(
  renderRangeInput
);

search.addWidgets([
  customRangeInput({
    attribute: string,
    // Optional parameters
    min: number,
    max: number,
    precision: number,
  })
]);

Instance options

Parameter Description
attribute
type: string
Required

The name of the attribute in the record.

1
2
3
customRangeInput({
  attribute: 'price',
});
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.

1
2
3
4
customRangeInput({
  // ...
  min: 10,
});
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.

1
2
3
4
customRangeInput({
  // ...
  max: 500,
});
precision
type: number
default: 0
Optional

The number of digits after the decimal point to use.

1
2
3
4
customRangeInput({
  // ...
  precision: 2,
});

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Create the render function
const renderRangeInput = (renderOptions, isFirstRender) => {
  const { start, range, refine, widgetParams } = renderOptions;
  const [min, max] = start;

  if (isFirstRender) {
    const form = document.createElement('form');

    form.addEventListener('submit', event => {
      event.preventDefault();

      const rawMinInputValue = parseFloat(event.target.elements.min.value);
      const rawMaxInputValue = parseFloat(event.target.elements.max.value);

      refine([
        Number.isFinite(rawMinInputValue) ? rawMinInputValue : undefined,
        Number.isFinite(rawMaxInputValue) ? rawMaxInputValue : undefined,
      ]);
    });

    widgetParams.container.appendChild(form);

    return;
  }

  widgetParams.container.querySelector('form').innerHTML = `
    <input
      type="number"
      name="min"
      placeholder="${range.min}"
      value="${Number.isFinite(min) ? min : ''}"
    />
    <span>to</span>
    <input
      type="number"
      name="max"
      placeholder="${range.max}"
      value="${Number.isFinite(max) ? max : ''}"
    />
    <input type="submit" hidden />
  `;
};

// Create the custom widget
const customRangeInput = connectRange(
  renderRangeInput
);

// Instantiate the custom widget
search.addWidgets([
  customRangeInput({
    container: document.querySelector('#range-input'),
    attribute: 'price',
  })
]);
Did you find this page helpful?