2019-11-01 13:56:14 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
useState,
|
|
|
|
useEffect,
|
|
|
|
useCallback,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
2022-08-24 09:10:21 +00:00
|
|
|
useLayoutEffect,
|
2019-11-01 13:56:14 +00:00
|
|
|
} from '@wordpress/element';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2023-10-18 14:43:34 +00:00
|
|
|
import { FormattedMonetaryAmount } from '@woocommerce/blocks-components';
|
2021-12-15 16:54:49 +00:00
|
|
|
import { Currency, isObject } from '@woocommerce/types';
|
2022-08-16 17:58:55 +00:00
|
|
|
import { useDebouncedCallback } from 'use-debounce';
|
2019-11-01 13:56:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-01-02 13:07:59 +00:00
|
|
|
import { constrainRangeSliderValues } from './constrain-range-slider-values';
|
2019-12-12 19:16:39 +00:00
|
|
|
import FilterSubmitButton from '../filter-submit-button';
|
2022-01-06 14:30:34 +00:00
|
|
|
import { isValidMaxValue, isValidMinValue } from './utils';
|
2022-08-16 17:58:55 +00:00
|
|
|
import FilterResetButton from '../filter-reset-button';
|
2019-11-01 13:56:14 +00:00
|
|
|
|
2021-12-15 16:54:49 +00:00
|
|
|
export interface PriceSliderProps {
|
|
|
|
/**
|
|
|
|
* Currency configuration object.
|
|
|
|
*/
|
|
|
|
currency: Currency;
|
|
|
|
/**
|
|
|
|
* Whether values are loading or not.
|
|
|
|
*/
|
|
|
|
isLoading?: boolean;
|
2022-09-02 13:52:52 +00:00
|
|
|
/**
|
|
|
|
* Whether values are updating or not. The update starts when the price slider is changed.
|
|
|
|
*/
|
|
|
|
isUpdating?: boolean;
|
2021-12-15 16:54:49 +00:00
|
|
|
/**
|
|
|
|
* Maximum constraint.
|
|
|
|
*/
|
2022-06-14 08:48:49 +00:00
|
|
|
maxConstraint: number | null | undefined;
|
2021-12-15 16:54:49 +00:00
|
|
|
/**
|
|
|
|
* Maximum price for slider.
|
|
|
|
*/
|
2022-06-14 08:48:49 +00:00
|
|
|
maxPrice: number | null;
|
2021-12-15 16:54:49 +00:00
|
|
|
/**
|
|
|
|
* Minimum constraint.
|
|
|
|
*/
|
2022-06-14 08:48:49 +00:00
|
|
|
minConstraint: number | null | undefined;
|
2021-12-15 16:54:49 +00:00
|
|
|
/**
|
|
|
|
* Minimum price for slider.
|
|
|
|
*/
|
2022-06-14 08:48:49 +00:00
|
|
|
minPrice: number | null;
|
2021-12-15 16:54:49 +00:00
|
|
|
/**
|
|
|
|
* Function to call on the change event.
|
|
|
|
*/
|
2022-07-01 17:50:44 +00:00
|
|
|
onChange: ( value: [ number, number ] ) => void;
|
2021-12-15 16:54:49 +00:00
|
|
|
/**
|
|
|
|
* Function to call when submit event fires.
|
|
|
|
*/
|
|
|
|
onSubmit?: () => void;
|
|
|
|
/**
|
|
|
|
* Whether to show the filter button for the slider.
|
|
|
|
*/
|
|
|
|
showFilterButton?: boolean;
|
|
|
|
/**
|
|
|
|
* Whether to show input fields for the values or not.
|
|
|
|
*/
|
|
|
|
showInputFields?: boolean;
|
2022-08-16 17:58:55 +00:00
|
|
|
/**
|
|
|
|
* Whether to show input fields inline with the slider or not.
|
|
|
|
*/
|
|
|
|
inlineInput?: boolean;
|
2021-12-15 16:54:49 +00:00
|
|
|
/**
|
|
|
|
* What step values the slider uses.
|
|
|
|
*/
|
2022-06-14 08:48:49 +00:00
|
|
|
step?: number;
|
2023-02-02 13:50:28 +00:00
|
|
|
/**
|
2024-08-19 12:57:38 +00:00
|
|
|
* Whether we're in the editor or not.
|
2023-02-02 13:50:28 +00:00
|
|
|
*/
|
|
|
|
isEditor?: boolean;
|
2021-12-15 16:54:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
const PriceSlider = ( {
|
2019-11-15 14:41:23 +00:00
|
|
|
minPrice,
|
|
|
|
maxPrice,
|
2019-11-01 13:56:14 +00:00
|
|
|
minConstraint,
|
|
|
|
maxConstraint,
|
2022-07-01 17:50:44 +00:00
|
|
|
onChange,
|
2019-12-18 11:29:20 +00:00
|
|
|
step,
|
|
|
|
currency,
|
2019-11-15 14:41:23 +00:00
|
|
|
showInputFields = true,
|
|
|
|
showFilterButton = false,
|
2022-08-16 17:58:55 +00:00
|
|
|
inlineInput = true,
|
2019-11-15 14:41:23 +00:00
|
|
|
isLoading = false,
|
2022-09-02 13:52:52 +00:00
|
|
|
isUpdating = false,
|
2023-02-02 13:50:28 +00:00
|
|
|
isEditor = false,
|
2021-12-15 16:54:49 +00:00
|
|
|
onSubmit = () => void 0,
|
|
|
|
}: PriceSliderProps ): JSX.Element => {
|
|
|
|
const minRange = useRef< HTMLInputElement >( null );
|
|
|
|
const maxRange = useRef< HTMLInputElement >( null );
|
2019-11-15 14:41:23 +00:00
|
|
|
|
2022-05-31 10:44:50 +00:00
|
|
|
// We want step to default to 1 major unit, e.g. $1.
|
|
|
|
const stepValue = step ? step : 10 ** currency.minorUnit;
|
2019-12-18 11:29:20 +00:00
|
|
|
|
|
|
|
const [ minPriceInput, setMinPriceInput ] = useState( minPrice );
|
|
|
|
const [ maxPriceInput, setMaxPriceInput ] = useState( maxPrice );
|
2019-11-01 13:56:14 +00:00
|
|
|
|
2022-08-24 09:10:21 +00:00
|
|
|
const wrapper = useRef< HTMLInputElement >( null );
|
|
|
|
const [ wrapperWidth, setWrapperWidth ] = useState( 0 );
|
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
useEffect( () => {
|
2019-12-18 11:29:20 +00:00
|
|
|
setMinPriceInput( minPrice );
|
|
|
|
}, [ minPrice ] );
|
2019-11-01 13:56:14 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
2019-12-18 11:29:20 +00:00
|
|
|
setMaxPriceInput( maxPrice );
|
|
|
|
}, [ maxPrice ] );
|
2019-11-01 13:56:14 +00:00
|
|
|
|
2022-08-24 09:10:21 +00:00
|
|
|
useLayoutEffect( () => {
|
|
|
|
if ( inlineInput && wrapper.current ) {
|
|
|
|
setWrapperWidth( wrapper.current?.offsetWidth );
|
|
|
|
}
|
|
|
|
}, [ inlineInput, setWrapperWidth ] );
|
|
|
|
|
2019-11-15 14:41:23 +00:00
|
|
|
/**
|
|
|
|
* Checks if the min and max constraints are valid.
|
|
|
|
*/
|
2019-11-11 11:05:18 +00:00
|
|
|
const hasValidConstraints = useMemo( () => {
|
|
|
|
return isFinite( minConstraint ) && isFinite( maxConstraint );
|
|
|
|
}, [ minConstraint, maxConstraint ] );
|
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
/**
|
|
|
|
* Handles styles for the shaded area of the range slider.
|
|
|
|
*/
|
2019-11-11 11:05:18 +00:00
|
|
|
const progressStyles = useMemo( () => {
|
2019-11-06 18:25:37 +00:00
|
|
|
if (
|
|
|
|
! isFinite( minPrice ) ||
|
|
|
|
! isFinite( maxPrice ) ||
|
2019-11-11 11:05:18 +00:00
|
|
|
! hasValidConstraints
|
2019-11-06 18:25:37 +00:00
|
|
|
) {
|
|
|
|
return {
|
|
|
|
'--low': '0%',
|
|
|
|
'--high': '100%',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
const low =
|
2023-04-20 11:41:36 +00:00
|
|
|
100 *
|
|
|
|
( ( minPrice - minConstraint ) /
|
|
|
|
( maxConstraint - minConstraint ) );
|
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
const high =
|
2023-04-20 11:41:36 +00:00
|
|
|
100 *
|
|
|
|
( ( maxPrice - minConstraint ) /
|
|
|
|
( maxConstraint - minConstraint ) );
|
2019-11-01 13:56:14 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'--low': low + '%',
|
|
|
|
'--high': high + '%',
|
|
|
|
};
|
2019-11-11 11:05:18 +00:00
|
|
|
}, [
|
|
|
|
minPrice,
|
|
|
|
maxPrice,
|
|
|
|
minConstraint,
|
|
|
|
maxConstraint,
|
|
|
|
hasValidConstraints,
|
|
|
|
] );
|
2019-11-01 13:56:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Works around an IE issue where only one range selector is visible by changing the display order
|
|
|
|
* based on the mouse position.
|
|
|
|
*
|
2019-12-10 17:17:46 +00:00
|
|
|
* @param {Object} event event data.
|
2019-11-01 13:56:14 +00:00
|
|
|
*/
|
|
|
|
const findClosestRange = useCallback(
|
2021-12-15 16:54:49 +00:00
|
|
|
( event: React.MouseEvent< HTMLDivElement > ) => {
|
|
|
|
if (
|
|
|
|
isLoading ||
|
|
|
|
! hasValidConstraints ||
|
|
|
|
! minRange.current ||
|
|
|
|
! maxRange.current
|
|
|
|
) {
|
2019-11-01 13:56:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-15 16:54:49 +00:00
|
|
|
const bounds = ( event.target as Element ).getBoundingClientRect();
|
2019-11-01 13:56:14 +00:00
|
|
|
const x = event.clientX - bounds.left;
|
|
|
|
const minWidth = minRange.current.offsetWidth;
|
2021-12-15 16:54:49 +00:00
|
|
|
const minValue = +minRange.current.value;
|
2019-11-01 13:56:14 +00:00
|
|
|
const maxWidth = maxRange.current.offsetWidth;
|
2021-12-15 16:54:49 +00:00
|
|
|
const maxValue = +maxRange.current.value;
|
2019-11-01 13:56:14 +00:00
|
|
|
|
|
|
|
const minX = minWidth * ( minValue / maxConstraint );
|
|
|
|
const maxX = maxWidth * ( maxValue / maxConstraint );
|
|
|
|
|
|
|
|
const minXDiff = Math.abs( x - minX );
|
|
|
|
const maxXDiff = Math.abs( x - maxX );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default z-index in the stylesheet as 20. 20 vs 21 is just for determining which range
|
|
|
|
* slider should be at the front and has no meaning beyond
|
|
|
|
*/
|
|
|
|
if ( minXDiff > maxXDiff ) {
|
2021-12-15 16:54:49 +00:00
|
|
|
minRange.current.style.zIndex = '20';
|
|
|
|
maxRange.current.style.zIndex = '21';
|
2019-11-01 13:56:14 +00:00
|
|
|
} else {
|
2021-12-15 16:54:49 +00:00
|
|
|
minRange.current.style.zIndex = '21';
|
|
|
|
maxRange.current.style.zIndex = '20';
|
2019-11-01 13:56:14 +00:00
|
|
|
}
|
|
|
|
},
|
2019-11-11 11:05:18 +00:00
|
|
|
[ isLoading, maxConstraint, hasValidConstraints ]
|
2019-11-01 13:56:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the slider is dragged.
|
|
|
|
*/
|
|
|
|
const rangeInputOnChange = useCallback(
|
2021-12-15 16:54:49 +00:00
|
|
|
( event: React.ChangeEvent< HTMLInputElement > ) => {
|
2019-11-01 13:56:14 +00:00
|
|
|
const isMin = event.target.classList.contains(
|
|
|
|
'wc-block-price-filter__range-input--min'
|
|
|
|
);
|
2021-12-15 16:54:49 +00:00
|
|
|
const targetValue = +event.target.value;
|
|
|
|
const currentValues: [ number, number ] = isMin
|
2020-01-02 11:10:41 +00:00
|
|
|
? [
|
|
|
|
Math.round( targetValue / stepValue ) * stepValue,
|
|
|
|
maxPrice,
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
minPrice,
|
|
|
|
Math.round( targetValue / stepValue ) * stepValue,
|
|
|
|
];
|
2019-11-01 13:56:14 +00:00
|
|
|
const values = constrainRangeSliderValues(
|
|
|
|
currentValues,
|
|
|
|
minConstraint,
|
|
|
|
maxConstraint,
|
2019-12-18 11:29:20 +00:00
|
|
|
stepValue,
|
2019-11-01 13:56:14 +00:00
|
|
|
isMin
|
|
|
|
);
|
2021-12-15 16:54:49 +00:00
|
|
|
onChange( values );
|
2019-11-01 13:56:14 +00:00
|
|
|
},
|
2020-10-27 15:39:08 +00:00
|
|
|
[
|
|
|
|
onChange,
|
|
|
|
minPrice,
|
|
|
|
maxPrice,
|
|
|
|
minConstraint,
|
|
|
|
maxConstraint,
|
|
|
|
stepValue,
|
|
|
|
]
|
2019-11-01 13:56:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a price input loses focus - commit changes to slider.
|
|
|
|
*/
|
|
|
|
const priceInputOnBlur = useCallback(
|
2021-12-15 16:54:49 +00:00
|
|
|
( event: React.FocusEvent< HTMLInputElement > ) => {
|
2019-12-18 11:29:20 +00:00
|
|
|
// Only refresh when finished editing the min and max fields.
|
|
|
|
if (
|
|
|
|
event.relatedTarget &&
|
2021-12-15 16:54:49 +00:00
|
|
|
( event.relatedTarget as Element ).classList &&
|
|
|
|
( event.relatedTarget as Element ).classList.contains(
|
2019-12-18 11:29:20 +00:00
|
|
|
'wc-block-price-filter__amount'
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-06 14:30:34 +00:00
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
const isMin = event.target.classList.contains(
|
|
|
|
'wc-block-price-filter__amount--min'
|
|
|
|
);
|
2022-01-06 14:30:34 +00:00
|
|
|
|
|
|
|
// When the user inserts in the max price input a value less or equal than the current minimum price,
|
|
|
|
// we set to 0 the minimum price.
|
|
|
|
if ( minPriceInput >= maxPriceInput ) {
|
|
|
|
const values = constrainRangeSliderValues(
|
|
|
|
[ 0, maxPriceInput ],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
stepValue,
|
|
|
|
isMin
|
|
|
|
);
|
|
|
|
return onChange( [
|
|
|
|
parseInt( values[ 0 ], 10 ),
|
|
|
|
parseInt( values[ 1 ], 10 ),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
const values = constrainRangeSliderValues(
|
2019-12-18 11:29:20 +00:00
|
|
|
[ minPriceInput, maxPriceInput ],
|
2020-01-02 13:07:59 +00:00
|
|
|
null,
|
|
|
|
null,
|
2019-12-18 11:29:20 +00:00
|
|
|
stepValue,
|
2019-11-01 13:56:14 +00:00
|
|
|
isMin
|
|
|
|
);
|
2021-12-15 16:54:49 +00:00
|
|
|
onChange( values );
|
2019-11-01 13:56:14 +00:00
|
|
|
},
|
2020-10-27 15:39:08 +00:00
|
|
|
[ onChange, stepValue, minPriceInput, maxPriceInput ]
|
2019-11-01 13:56:14 +00:00
|
|
|
);
|
|
|
|
|
2022-08-16 17:58:55 +00:00
|
|
|
const debouncedUpdateQuery = useDebouncedCallback( onSubmit, 600 );
|
|
|
|
|
2024-05-31 03:49:36 +00:00
|
|
|
const classes = clsx(
|
2019-11-01 13:56:14 +00:00
|
|
|
'wc-block-price-filter',
|
2020-06-17 09:53:42 +00:00
|
|
|
'wc-block-components-price-slider',
|
2019-11-01 13:56:14 +00:00
|
|
|
showInputFields && 'wc-block-price-filter--has-input-fields',
|
2020-06-17 09:53:42 +00:00
|
|
|
showInputFields && 'wc-block-components-price-slider--has-input-fields',
|
2019-11-01 13:56:14 +00:00
|
|
|
showFilterButton && 'wc-block-price-filter--has-filter-button',
|
2020-06-17 09:53:42 +00:00
|
|
|
showFilterButton &&
|
|
|
|
'wc-block-components-price-slider--has-filter-button',
|
2022-08-24 09:10:21 +00:00
|
|
|
! hasValidConstraints && 'is-disabled',
|
|
|
|
( inlineInput || wrapperWidth <= 300 ) &&
|
|
|
|
'wc-block-components-price-slider--is-input-inline'
|
2019-11-01 13:56:14 +00:00
|
|
|
);
|
|
|
|
|
2021-05-16 17:59:32 +00:00
|
|
|
const activeElement = isObject( minRange.current )
|
|
|
|
? minRange.current.ownerDocument.activeElement
|
|
|
|
: undefined;
|
2020-01-02 11:10:41 +00:00
|
|
|
const minRangeStep =
|
2021-05-16 17:59:32 +00:00
|
|
|
activeElement && activeElement === minRange.current ? stepValue : 1;
|
2020-01-02 11:10:41 +00:00
|
|
|
const maxRangeStep =
|
2021-05-16 17:59:32 +00:00
|
|
|
activeElement && activeElement === maxRange.current ? stepValue : 1;
|
2020-01-02 11:10:41 +00:00
|
|
|
|
2021-12-15 16:54:49 +00:00
|
|
|
const ariaReadableMinPrice = String(
|
|
|
|
minPriceInput / 10 ** currency.minorUnit
|
|
|
|
);
|
|
|
|
const ariaReadableMaxPrice = String(
|
|
|
|
maxPriceInput / 10 ** currency.minorUnit
|
|
|
|
);
|
2021-10-06 08:48:17 +00:00
|
|
|
|
2022-08-24 09:10:21 +00:00
|
|
|
const inlineInputAvailable = inlineInput && wrapperWidth > 300;
|
|
|
|
|
2022-08-16 17:58:55 +00:00
|
|
|
const slider = (
|
|
|
|
<div
|
2024-05-31 03:49:36 +00:00
|
|
|
className={ clsx(
|
2022-09-02 13:52:52 +00:00
|
|
|
'wc-block-price-filter__range-input-wrapper',
|
|
|
|
'wc-block-components-price-slider__range-input-wrapper',
|
|
|
|
{ 'is-loading': isLoading && isUpdating }
|
|
|
|
) }
|
2022-08-16 17:58:55 +00:00
|
|
|
onMouseMove={ findClosestRange }
|
|
|
|
onFocus={ findClosestRange }
|
|
|
|
>
|
|
|
|
{ hasValidConstraints && (
|
|
|
|
<div aria-hidden={ showInputFields }>
|
|
|
|
<div
|
|
|
|
className="wc-block-price-filter__range-input-progress wc-block-components-price-slider__range-input-progress"
|
|
|
|
style={ progressStyles as React.CSSProperties }
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
type="range"
|
|
|
|
className="wc-block-price-filter__range-input wc-block-price-filter__range-input--min wc-block-components-price-slider__range-input wc-block-components-price-slider__range-input--min"
|
|
|
|
aria-label={ __(
|
|
|
|
'Filter products by minimum price',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2022-08-16 17:58:55 +00:00
|
|
|
) }
|
|
|
|
aria-valuetext={ ariaReadableMinPrice }
|
|
|
|
value={
|
|
|
|
Number.isFinite( minPrice )
|
|
|
|
? minPrice
|
|
|
|
: minConstraint
|
|
|
|
}
|
|
|
|
onChange={ rangeInputOnChange }
|
|
|
|
step={ minRangeStep }
|
|
|
|
min={ minConstraint }
|
|
|
|
max={ maxConstraint }
|
|
|
|
ref={ minRange }
|
|
|
|
disabled={ isLoading && ! hasValidConstraints }
|
|
|
|
tabIndex={ showInputFields ? -1 : 0 }
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
type="range"
|
|
|
|
className="wc-block-price-filter__range-input wc-block-price-filter__range-input--max wc-block-components-price-slider__range-input wc-block-components-price-slider__range-input--max"
|
|
|
|
aria-label={ __(
|
|
|
|
'Filter products by maximum price',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2022-08-16 17:58:55 +00:00
|
|
|
) }
|
|
|
|
aria-valuetext={ ariaReadableMaxPrice }
|
|
|
|
value={
|
|
|
|
Number.isFinite( maxPrice )
|
|
|
|
? maxPrice
|
|
|
|
: maxConstraint
|
|
|
|
}
|
|
|
|
onChange={ rangeInputOnChange }
|
|
|
|
step={ maxRangeStep }
|
|
|
|
min={ minConstraint }
|
|
|
|
max={ maxConstraint }
|
|
|
|
ref={ maxRange }
|
|
|
|
disabled={ isLoading }
|
|
|
|
tabIndex={ showInputFields ? -1 : 0 }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2023-04-06 12:58:07 +00:00
|
|
|
const getInputClassName = ( type: 'min' | 'max' ) =>
|
|
|
|
`wc-block-price-filter__amount wc-block-price-filter__amount--${ type } wc-block-form-text-input wc-block-components-price-slider__amount wc-block-components-price-slider__amount--${ type }`;
|
|
|
|
|
|
|
|
const commonFormattedMonetaryAmountProps = {
|
|
|
|
currency,
|
|
|
|
decimalScale: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
const commonFormattedMonetaryAmountInputProps = {
|
|
|
|
...commonFormattedMonetaryAmountProps,
|
|
|
|
displayType: 'input',
|
|
|
|
allowNegative: false,
|
|
|
|
disabled: isLoading || ! hasValidConstraints,
|
|
|
|
onBlur: priceInputOnBlur,
|
|
|
|
};
|
|
|
|
|
2019-11-01 13:56:14 +00:00
|
|
|
return (
|
2022-08-24 09:10:21 +00:00
|
|
|
<div className={ classes } ref={ wrapper }>
|
|
|
|
{ ( ! inlineInputAvailable || ! showInputFields ) && slider }
|
2022-08-16 17:58:55 +00:00
|
|
|
{ showInputFields && (
|
|
|
|
<div className="wc-block-price-filter__controls wc-block-components-price-slider__controls">
|
2022-09-02 13:52:52 +00:00
|
|
|
{ ! isUpdating ? (
|
|
|
|
<FormattedMonetaryAmount
|
2023-04-06 12:58:07 +00:00
|
|
|
{ ...commonFormattedMonetaryAmountInputProps }
|
|
|
|
className={ getInputClassName( 'min' ) }
|
2022-09-02 13:52:52 +00:00
|
|
|
aria-label={ __(
|
|
|
|
'Filter products by minimum price',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2022-09-02 13:52:52 +00:00
|
|
|
) }
|
|
|
|
isAllowed={ isValidMinValue( {
|
|
|
|
minConstraint,
|
|
|
|
minorUnit: currency.minorUnit,
|
|
|
|
currentMaxValue: maxPriceInput,
|
|
|
|
} ) }
|
|
|
|
onValueChange={ ( value ) => {
|
|
|
|
if ( value === minPriceInput ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setMinPriceInput( value );
|
|
|
|
} }
|
|
|
|
value={ minPriceInput }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="input-loading"></div>
|
|
|
|
) }
|
2022-08-24 09:10:21 +00:00
|
|
|
{ inlineInputAvailable && slider }
|
2022-09-02 13:52:52 +00:00
|
|
|
{ ! isUpdating ? (
|
|
|
|
<FormattedMonetaryAmount
|
2023-04-06 12:58:07 +00:00
|
|
|
{ ...commonFormattedMonetaryAmountInputProps }
|
|
|
|
className={ getInputClassName( 'max' ) }
|
2022-09-02 13:52:52 +00:00
|
|
|
aria-label={ __(
|
|
|
|
'Filter products by maximum price',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2022-09-02 13:52:52 +00:00
|
|
|
) }
|
|
|
|
isAllowed={ isValidMaxValue( {
|
|
|
|
maxConstraint,
|
|
|
|
minorUnit: currency.minorUnit,
|
|
|
|
} ) }
|
|
|
|
onValueChange={ ( value ) => {
|
|
|
|
if ( value === maxPriceInput ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setMaxPriceInput( value );
|
|
|
|
} }
|
|
|
|
value={ maxPriceInput }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="input-loading"></div>
|
|
|
|
) }
|
2022-08-16 17:58:55 +00:00
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
|
|
|
|
{ ! showInputFields &&
|
2022-09-02 13:52:52 +00:00
|
|
|
! isUpdating &&
|
2022-08-16 17:58:55 +00:00
|
|
|
Number.isFinite( minPrice ) &&
|
|
|
|
Number.isFinite( maxPrice ) && (
|
|
|
|
<div className="wc-block-price-filter__range-text wc-block-components-price-slider__range-text">
|
2019-12-18 11:29:20 +00:00
|
|
|
<FormattedMonetaryAmount
|
2023-04-06 12:58:07 +00:00
|
|
|
{ ...commonFormattedMonetaryAmountProps }
|
2022-08-16 17:58:55 +00:00
|
|
|
value={ minPrice }
|
2019-12-18 11:29:20 +00:00
|
|
|
/>
|
|
|
|
<FormattedMonetaryAmount
|
2023-04-06 12:58:07 +00:00
|
|
|
{ ...commonFormattedMonetaryAmountProps }
|
2022-08-16 17:58:55 +00:00
|
|
|
value={ maxPrice }
|
2019-12-18 11:29:20 +00:00
|
|
|
/>
|
2022-08-16 17:58:55 +00:00
|
|
|
</div>
|
|
|
|
) }
|
2022-09-09 11:28:04 +00:00
|
|
|
{
|
2022-09-02 13:52:52 +00:00
|
|
|
<div className="wc-block-components-price-slider__actions">
|
2023-02-02 13:50:28 +00:00
|
|
|
{ ( isEditor ||
|
|
|
|
( ! isUpdating &&
|
|
|
|
( minPrice !== minConstraint ||
|
|
|
|
maxPrice !== maxConstraint ) ) ) && (
|
|
|
|
<FilterResetButton
|
|
|
|
onClick={ () => {
|
|
|
|
onChange( [ minConstraint, maxConstraint ] );
|
|
|
|
debouncedUpdateQuery();
|
|
|
|
} }
|
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Reset price filter',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2023-02-02 13:50:28 +00:00
|
|
|
) }
|
|
|
|
/>
|
|
|
|
) }
|
2022-09-02 13:52:52 +00:00
|
|
|
{ showFilterButton && (
|
|
|
|
<FilterSubmitButton
|
|
|
|
className="wc-block-price-filter__button wc-block-components-price-slider__button"
|
2022-09-09 11:28:04 +00:00
|
|
|
isLoading={ isUpdating }
|
2022-09-02 13:52:52 +00:00
|
|
|
disabled={ isLoading || ! hasValidConstraints }
|
|
|
|
onClick={ onSubmit }
|
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Apply price filter',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2022-09-02 13:52:52 +00:00
|
|
|
) }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
2022-09-09 11:28:04 +00:00
|
|
|
}
|
2019-11-01 13:56:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PriceSlider;
|