Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit b6598a9

Browse files
authored
Use placeholder when contraints are invalid (#1145)
* Use placeholder when contraints are invalid * Update assets/js/base/components/price-slider/style.scss Co-Authored-By: Albert Juhé Lluveras <[email protected]> * Update assets/js/base/components/price-slider/style.scss Co-Authored-By: Albert Juhé Lluveras <[email protected]> * Move hasValidConstraints
1 parent 86420f7 commit b6598a9

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

assets/js/base/components/price-slider/index.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ const PriceSlider = ( {
5151
const prevMaxConstraint = usePrevious( maxConstraint );
5252

5353
useEffect( () => {
54+
if ( isNaN( minConstraint ) ) {
55+
setMinPrice( 0 );
56+
return;
57+
}
5458
if (
5559
minPrice === undefined ||
5660
minConstraint > minPrice ||
@@ -61,6 +65,10 @@ const PriceSlider = ( {
6165
}, [ minConstraint ] );
6266

6367
useEffect( () => {
68+
if ( isNaN( maxConstraint ) ) {
69+
setMaxPrice( 100 );
70+
return;
71+
}
6472
if (
6573
maxPrice === undefined ||
6674
maxConstraint < maxPrice ||
@@ -82,21 +90,24 @@ const PriceSlider = ( {
8290
);
8391
}, [ maxPrice, priceFormat, currencySymbol ] );
8492

93+
const hasValidConstraints = useMemo( () => {
94+
return isFinite( minConstraint ) && isFinite( maxConstraint );
95+
}, [ minConstraint, maxConstraint ] );
96+
8597
useEffect( () => {
86-
if ( ! showFilterButton && ! isLoading ) {
98+
if ( ! showFilterButton && ! isLoading && hasValidConstraints ) {
8799
triggerChange();
88100
}
89101
}, [ debouncedChangeValue ] );
90102

91103
/**
92104
* Handles styles for the shaded area of the range slider.
93105
*/
94-
const getProgressStyle = useMemo( () => {
106+
const progressStyles = useMemo( () => {
95107
if (
96108
! isFinite( minPrice ) ||
97109
! isFinite( maxPrice ) ||
98-
! isFinite( minConstraint ) ||
99-
! isFinite( maxConstraint )
110+
! hasValidConstraints
100111
) {
101112
return {
102113
'--low': '0%',
@@ -121,7 +132,13 @@ const PriceSlider = ( {
121132
'--low': low + '%',
122133
'--high': high + '%',
123134
};
124-
}, [ minPrice, maxPrice, minConstraint, maxConstraint ] );
135+
}, [
136+
minPrice,
137+
maxPrice,
138+
minConstraint,
139+
maxConstraint,
140+
hasValidConstraints,
141+
] );
125142

126143
/**
127144
* Trigger the onChange prop callback with new values.
@@ -138,7 +155,7 @@ const PriceSlider = ( {
138155
*/
139156
const findClosestRange = useCallback(
140157
( event ) => {
141-
if ( isLoading ) {
158+
if ( isLoading || ! hasValidConstraints ) {
142159
return;
143160
}
144161
const bounds = event.target.getBoundingClientRect();
@@ -166,7 +183,7 @@ const PriceSlider = ( {
166183
maxRange.current.style.zIndex = 20;
167184
}
168185
},
169-
[ isLoading, maxConstraint ]
186+
[ isLoading, maxConstraint, hasValidConstraints ]
170187
);
171188

172189
/**
@@ -256,7 +273,8 @@ const PriceSlider = ( {
256273
'wc-block-price-filter',
257274
showInputFields && 'wc-block-price-filter--has-input-fields',
258275
showFilterButton && 'wc-block-price-filter--has-filter-button',
259-
isLoading && 'is-loading'
276+
isLoading && 'is-loading',
277+
! hasValidConstraints && 'is-disabled'
260278
);
261279

262280
return (
@@ -266,11 +284,11 @@ const PriceSlider = ( {
266284
onMouseMove={ findClosestRange }
267285
onFocus={ findClosestRange }
268286
>
269-
{ ! isLoading && (
287+
{ ! isLoading && hasValidConstraints && (
270288
<Fragment>
271289
<div
272290
className="wc-block-price-filter__range-input-progress"
273-
style={ getProgressStyle }
291+
style={ progressStyles }
274292
/>
275293
<input
276294
type="range"
@@ -306,7 +324,7 @@ const PriceSlider = ( {
306324
<div className="wc-block-price-filter__controls">
307325
{ showInputFields ? (
308326
<PriceInput
309-
disabled={ isLoading }
327+
disabled={ isLoading || ! hasValidConstraints }
310328
onChange={ priceInputOnChange }
311329
onBlur={ priceInputOnBlur }
312330
minPrice={ formattedMinPrice }
@@ -320,7 +338,7 @@ const PriceSlider = ( {
320338
) }
321339
{ showFilterButton && (
322340
<SubmitButton
323-
disabled={ isLoading }
341+
disabled={ isLoading || ! hasValidConstraints }
324342
onClick={ triggerChange }
325343
/>
326344
) }

assets/js/base/components/price-slider/style.scss

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,23 @@
180180
}
181181
}
182182

183-
&.is-loading {
183+
&.is-loading,
184+
&.is-disabled {
184185
.wc-block-price-filter__range-input-wrapper,
185186
.wc-block-price-filter__amount,
186187
.wc-block-price-filter__button {
187188
@include placeholder();
188189
box-shadow: none;
189190
}
190191
}
192+
193+
&.is-disabled:not(.is-loading) {
194+
.wc-block-price-filter__range-input-wrapper,
195+
.wc-block-price-filter__amount,
196+
.wc-block-price-filter__button {
197+
animation: none;
198+
}
199+
}
191200
}
192201

193202
@mixin ie {
@@ -247,12 +256,19 @@
247256
}
248257
}
249258

250-
&.is-loading {
259+
&.is-loading,
260+
&.is-disabled {
251261
.wc-block-price-filter__range-input-wrapper {
252262
@include placeholder();
253263
box-shadow: none;
254264
}
255265
}
266+
267+
&.is-disabled:not(.is-loading) {
268+
.wc-block-price-filter__range-input-wrapper {
269+
animation: none;
270+
}
271+
}
256272
}
257273
}
258274

0 commit comments

Comments
 (0)