Function useRangeSelector

  • Get range selector state

    Parameters

    • id: string

      Id of the range Selector

    • rangeSize: number

    Returns {
        handleMaxChange: (value: number) => void;
        handleMinChange: (value: number) => void;
        isSelected: boolean;
        max: number;
        min: number;
        range: number[];
        ranges: { max: number; min: number; selected: null | boolean }[];
        updateRange: (
            __namedParameters: [undefined | number, undefined | number],
        ) => void;
    }

    • handleMaxChange: (value: number) => void

      Handle max change

    • handleMinChange: (value: number) => void

      Handle min change

    • isSelected: boolean

      Is selected

    • max: number

      Maximum value

    • min: number

      Minimum value

    • range: number[]

      Range value

    • ranges: { max: number; min: number; selected: null | boolean }[]

      Ranges

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

      Update range function

    import { useRangeCheckboxes } from "@nosto/preact"
    import { useState } from "preact/hooks"
    import RadioButton from "./elements/RadioButton"
    import RangeInput from "./elements/RangeInput"

    export default function RangeSelector({ facet }) {
    const {
    min,
    max,
    range,
    ranges,
    updateRange,
    handleMinChange,
    handleMaxChange,
    isSelected,
    } = useRangeSelector(facet.id, 100)

    return (
    <li>
    <div>
    <ul>
    {ranges.map(({ min, max, selected }, index) => {
    return (
    <li>
    <RadioButton
    key={index}
    value={`${min} - ${max}`}
    selected={selected}
    onChange={() => updateRange([min, max])}
    />
    </li>
    )
    })}
    <div>
    <div>
    <label htmlFor={`ns-${facet.id}-min`}>
    Min.
    </label>
    <RangeInput
    id={`ns-${facet.id}-min`}
    min={min}
    max={max}
    range={range}
    value={range[0] ?? min}
    onChange={e => handleMinChange(parseFloat(e.currentTarget.value) || min)}
    />
    </div>
    <div>
    <label htmlFor={`ns-${facet.id}-max`}>
    Max.
    </label>
    <RangeInput
    id={`ns-${facet.id}-max`}
    min={min}
    max={max}
    range={range}
    value={range[1] ?? max}
    onChange={e => handleMaxChange(parseFloat(e.currentTarget.value) || max)}
    />
    </div>
    </div>
    </ul>
    </div>
    </li>
    )
    }