import React, { useState } from 'react'; import { cx } from './lib/cx'; import type { useRange } from 'react-instantsearch-hooks'; type RangeRenderState = ReturnType; export type RangeInputProps = Omit, 'onSubmit'> & Pick & { classNames?: Partial; disabled: boolean; onSubmit: RangeRenderState['refine']; step?: number; translations: RangeInputTranslations; }; export type RangeInputClassNames = { /** * Class names to apply to the root element */ root: string; /** * Class names to apply to the root element when there are no refinements possible */ noRefinementRoot: string; /** * Class names to apply to the form element */ form: string; /** * Class names to apply to each label element */ label: string; /** * Class names to apply to each input element */ input: string; /** * Class names to apply to the minimum input element */ inputMin: string; /** * Class names to apply to the maximum input element */ inputMax: string; /** * Class names to apply to the separator element */ separator: string; /** * Class names to apply to the submit button */ submit: string; }; export type RangeInputTranslations = { /** * The label of the separator, between the minimum and maximum inputs */ separatorElementText: string; /** * The label of the submit button */ submitButtonText: string; }; // if the default value is undefined, React considers the component uncontrolled initially, which we don't want 0 or NaN as the default value const unsetNumberInputValue = '' as unknown as number; export function RangeInput({ classNames = {}, range: { min, max }, start: [minValue, maxValue], step = 1, disabled, onSubmit, translations, ...props }: RangeInputProps) { const values = { min: minValue !== -Infinity && minValue !== min ? minValue : unsetNumberInputValue, max: maxValue !== Infinity && maxValue !== max ? maxValue : unsetNumberInputValue, }; const [prevValues, setPrevValues] = useState(values); const [{ from, to }, setRange] = useState({ from: values.min, to: values.max, }); if (values.min !== prevValues.min || values.max !== prevValues.max) { setRange({ from: values.min, to: values.max }); setPrevValues(values); } return (
{ event.preventDefault(); onSubmit([from, to]); }} > {translations.separatorElementText}
); }