import React, { useState } from 'react' import styles from './RangeFilter.module.scss' import TextInput from '../Form/TextInput' import { c } from '../../translations/LibraryTranslationService' export type MinMaxRange = { min?: number max?: number } export type RangeProps = { /** Selected min-max values */ selectedValues: MinMaxRange /** Function to update filter values */ onChangeCallout?: (value: MinMaxRange) => void /** Optional label for min input */ labelTextMin?: string /** Optional label for max input */ labelTextMax?: string /** Optional placeholder for min input */ placeholderMin?: string /** Optional placeholder for max input */ placeholderMax?: string } const RangeFilter = (filterValues: RangeProps): React.JSX.Element => { const { selectedValues, onChangeCallout, labelTextMin, labelTextMax, placeholderMin, placeholderMax, } = filterValues const [error, setError] = useState(false) return (
{ const updatedMin = value === '' ? undefined : Number(value) if ( selectedValues?.max !== undefined && updatedMin !== undefined && updatedMin > selectedValues.max ) { setError(true) } else { setError(false) const updatedRange = { ...selectedValues, min: updatedMin } onChangeCallout?.(updatedRange) } }} />
-
{ const updatedMax = value === '' ? undefined : Number(value) if ( selectedValues?.min !== undefined && updatedMax !== undefined && updatedMax < selectedValues.min ) { setError(true) } else { setError(false) const updatedRange = { ...selectedValues, max: updatedMax } onChangeCallout?.(updatedRange) } }} />
{error ? (
{c('minValueCantBeGreaterThanMaxValue')}
) : null}
) } export default RangeFilter