forked from woocommerce/woocommerce-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storybook and TS migration of
PriceSlider
component (woocommerce#5253…
…) (woocommerce#5293) * Add full documentation to the component props in Storybook * Fix `FormattedMonetaryAmount` TypeScript errors Since this component is passing on props to `NumberFormat`, it needs to extend all of its original props, except for `onValueChange` which we wrap in order to accept only `number`s. * Convert `constrainRangeSliderValues` to TypeScript * Add docs to `Currency` types * Convert `PriceSlider` to TypeScript *Note:* All TypeScript errors were fixed during the conversion, except for the IE workaround which wasn't clear to me at this time. Opened an issue: woocommerce#5276 * Fix Price Slider story (woocommerce#5253) * Migrate stories for `ProductName` to latest Storybook Also add props documentation and add named export.
- Loading branch information
1 parent
de8931f
commit 9ad3e68
Showing
8 changed files
with
242 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
assets/js/base/components/price-slider/constrain-range-slider-values.js
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
assets/js/base/components/price-slider/constrain-range-slider-values.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Validate a min and max value for a range slider against defined constraints (min, max, step). | ||
* | ||
* @return {[number, number]} Validated and updated min/max values that fit within the range slider constraints. | ||
*/ | ||
export const constrainRangeSliderValues = ( | ||
/** | ||
* Tuple containing min and max values. | ||
*/ | ||
values: [ number, number ], | ||
/** | ||
* Min allowed value for the sliders. | ||
*/ | ||
min?: number | null, | ||
/** | ||
* Max allowed value for the sliders. | ||
*/ | ||
max?: number | null, | ||
/** | ||
* Step value for the sliders. | ||
*/ | ||
step = 1, | ||
/** | ||
* Whether we're currently interacting with the min range slider or not, so we update the correct values. | ||
*/ | ||
isMin = false | ||
): [ number, number ] => { | ||
let [ minValue, maxValue ] = values; | ||
|
||
const isFinite = ( n: number | undefined ): n is number => | ||
Number.isFinite( n ); | ||
|
||
if ( ! isFinite( minValue ) ) { | ||
minValue = min || 0; | ||
} | ||
|
||
if ( ! isFinite( maxValue ) ) { | ||
maxValue = max || step; | ||
} | ||
|
||
if ( isFinite( min ) && min > minValue ) { | ||
minValue = min; | ||
} | ||
|
||
if ( isFinite( max ) && max <= minValue ) { | ||
minValue = max - step; | ||
} | ||
|
||
if ( isFinite( min ) && min >= maxValue ) { | ||
maxValue = min + step; | ||
} | ||
|
||
if ( isFinite( max ) && max < maxValue ) { | ||
maxValue = max; | ||
} | ||
|
||
if ( ! isMin && minValue >= maxValue ) { | ||
minValue = maxValue - step; | ||
} | ||
|
||
if ( isMin && maxValue <= minValue ) { | ||
maxValue = minValue + step; | ||
} | ||
|
||
return [ minValue, maxValue ]; | ||
}; |
Oops, something went wrong.