Function useRange

  • A hook that returns range information and functions to update the range. useRangeSlider is an alias of useRange, maintained for backward compatibility with older implementations that uses sliders.

    Parameters

    • id: string

      The identifier for the specific range facet.

    Returns {
        max: number;
        min: number;
        range: number[];
        updateRange: (
            __namedParameters: [undefined | number, undefined | number],
        ) => void;
    }

    An object containing range information and functions to update the range.

    • max: number

      Max value

    • min: number

      Min value

    • range: number[]

      Range value

    • updateRange: (__namedParameters: [undefined | number, undefined | number]) => void

      Update range function

    General usage with useRange:

    import { useRange } from "./useRange";
    import { useState } from "react";

    const Component = ({ facetId }) => {
    const { min, max, range, updateRange } = useRange(facetId);

    return (
    <div>
    Current Range: {range[0]} to {range[1]}
    <button onClick={() => updateRange([min, max])}>Reset Range</button>
    </div>
    );
    };

    Usage with useRangeSlider for slider components (backward compatibility):

    import { RangeSlider, useRangeSlider } from "@nosto/preact"
    import { useState } from "preact/hooks"
    import RangeInput from "./elements/RangeInput"

    const RangeFacet = ({ facet }) => {
    const { min, max, range, updateRange } = useRangeSlider(facet.id);

    return (
    <li>
    <div>
    <div>
    <label>Min.</label>
    <RangeInput
    min={min}
    max={max}
    value={range[0]}
    onChange={e => updateRange([parseFloat(e.currentTarget.value) || undefined, range[1]])}
    />
    <label>Max.</label>
    <RangeInput
    min={min}
    max={max}
    value={range[1]}
    onChange={e => updateRange([range[0], parseFloat(e.currentTarget.value) || undefined])}
    />
    </div>
    <RangeSlider id={facet.id} />
    </div>
    </li>
    );
    };