@faststore/ui 1.10.6 → 1.10.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/dist/atoms/Slider/Slider.d.ts +11 -2
- package/dist/atoms/Slider/Slider.js +15 -4
- package/dist/atoms/Slider/Slider.js.map +1 -1
- package/dist/molecules/PriceRange/PriceRange.d.ts +25 -1
- package/dist/molecules/PriceRange/PriceRange.js +12 -4
- package/dist/molecules/PriceRange/PriceRange.js.map +1 -1
- package/package.json +2 -2
- package/src/atoms/Slider/Slider.tsx +97 -70
- package/src/molecules/PriceRange/PriceRange.tsx +61 -40
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.10.7](https://github.com/vtex/faststore/compare/v1.10.6...v1.10.7) (2022-07-06)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Supports update of `Slider` and `PriceRange` values ([#1380](https://github.com/vtex/faststore/issues/1380)) ([41dca35](https://github.com/vtex/faststore/commit/41dca35f0c320cf1659e8a74032cc2ee755a9f6f))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## 1.10.6 (2022-07-05)
|
|
7
18
|
|
|
8
19
|
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* This code is inspired by the work of [sandra-lewis](https://codesandbox.io/u/sandra-lewis)
|
|
3
|
+
*/
|
|
4
|
+
import React from 'react';
|
|
2
5
|
interface Range {
|
|
3
6
|
absolute: number;
|
|
4
7
|
selected: number;
|
|
@@ -41,5 +44,11 @@ export declare type SliderProps = {
|
|
|
41
44
|
*/
|
|
42
45
|
className?: string;
|
|
43
46
|
};
|
|
44
|
-
declare
|
|
47
|
+
declare type SliderRefType = {
|
|
48
|
+
setSliderValues: (values: {
|
|
49
|
+
min: number;
|
|
50
|
+
max: number;
|
|
51
|
+
}) => void;
|
|
52
|
+
};
|
|
53
|
+
declare const Slider: React.ForwardRefExoticComponent<SliderProps & React.RefAttributes<SliderRefType | undefined>>;
|
|
45
54
|
export default Slider;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This code is inspired by the work of [sandra-lewis](https://codesandbox.io/u/sandra-lewis)
|
|
3
3
|
*/
|
|
4
|
-
import React, { useState, useMemo } from 'react';
|
|
4
|
+
import React, { useState, useMemo, useImperativeHandle, forwardRef, } from 'react';
|
|
5
5
|
const percent = (value, min, max) => Math.round(((value - min) / (max - min)) * 100);
|
|
6
|
-
const Slider = ({ min, max, onChange, onEnd, testId = 'store-slider', getAriaValueText, className, })
|
|
6
|
+
const Slider = forwardRef(function Slider({ min, max, onChange, onEnd, testId = 'store-slider', getAriaValueText, className, }, ref) {
|
|
7
7
|
const [minPercent, setMinPercent] = useState(() => percent(min.selected, min.absolute, max.absolute));
|
|
8
8
|
const [maxPercent, setMaxPercent] = useState(() => percent(max.selected, min.absolute, max.absolute));
|
|
9
9
|
const { minVal, maxVal } = useMemo(() => {
|
|
@@ -13,8 +13,19 @@ const Slider = ({ min, max, onChange, onEnd, testId = 'store-slider', getAriaVal
|
|
|
13
13
|
maxVal: min.absolute + maxPercent * widthPercent,
|
|
14
14
|
};
|
|
15
15
|
}, [min, max, maxPercent, minPercent]);
|
|
16
|
+
useImperativeHandle(ref, () => ({
|
|
17
|
+
setSliderValues: (values) => {
|
|
18
|
+
const sliderMinValue = Math.min(Number(values.min), maxVal);
|
|
19
|
+
const sliderMaxValue = Math.max(Number(values.max), minVal);
|
|
20
|
+
setMinPercent(percent(sliderMinValue, min.absolute, max.absolute));
|
|
21
|
+
setMaxPercent(percent(sliderMaxValue, min.absolute, max.absolute));
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
16
24
|
return (React.createElement("div", { "data-store-slider": true, "data-testid": testId, className: className },
|
|
17
|
-
React.createElement("div", { style: {
|
|
25
|
+
React.createElement("div", { style: {
|
|
26
|
+
left: `${minPercent}%`,
|
|
27
|
+
width: `${maxPercent - minPercent}%`,
|
|
28
|
+
}, "data-slider-range": true }),
|
|
18
29
|
React.createElement("input", { type: "range", min: min.absolute, max: max.absolute, value: minVal, onMouseUp: () => onEnd?.({ min: minVal, max: maxVal }), onTouchEnd: () => onEnd?.({ min: minVal, max: maxVal }), onChange: (event) => {
|
|
19
30
|
const minValue = Math.min(Number(event.target.value), maxVal);
|
|
20
31
|
setMinPercent(percent(minValue, min.absolute, max.absolute));
|
|
@@ -25,6 +36,6 @@ const Slider = ({ min, max, onChange, onEnd, testId = 'store-slider', getAriaVal
|
|
|
25
36
|
setMaxPercent(percent(maxValue, min.absolute, max.absolute));
|
|
26
37
|
onChange?.({ min: minVal, max: maxValue });
|
|
27
38
|
}, "data-slider-thumb": "right", "aria-valuemin": min.absolute, "aria-valuemax": max.absolute, "aria-valuenow": maxVal, "aria-label": String(maxVal), "aria-labelledby": getAriaValueText?.(maxVal, 'max') })));
|
|
28
|
-
};
|
|
39
|
+
});
|
|
29
40
|
export default Slider;
|
|
30
41
|
//# sourceMappingURL=Slider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Slider.js","sourceRoot":"","sources":["../../../src/atoms/Slider/Slider.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"Slider.js","sourceRoot":"","sources":["../../../src/atoms/Slider/Slider.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,EACZ,QAAQ,EACR,OAAO,EACP,mBAAmB,EACnB,UAAU,GACX,MAAM,OAAO,CAAA;AA4Cd,MAAM,OAAO,GAAG,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW,EAAE,EAA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
1
|
+
import React from 'react';
|
|
1
2
|
import type { AriaAttributes } from 'react';
|
|
2
3
|
import type { PriceProps } from '../../atoms/Price';
|
|
3
4
|
import type { SliderProps } from '../../atoms/Slider';
|
|
@@ -19,5 +20,28 @@ export declare type PriceRangeProps = SliderProps & {
|
|
|
19
20
|
*/
|
|
20
21
|
'aria-label'?: AriaAttributes['aria-label'];
|
|
21
22
|
};
|
|
22
|
-
declare
|
|
23
|
+
declare type PriceRangeRefType = {
|
|
24
|
+
setPriceRangeValues: (values: {
|
|
25
|
+
min: number;
|
|
26
|
+
max: number;
|
|
27
|
+
}) => void;
|
|
28
|
+
};
|
|
29
|
+
declare const PriceRange: React.ForwardRefExoticComponent<SliderProps & {
|
|
30
|
+
/**
|
|
31
|
+
* The current use case variant for prices.
|
|
32
|
+
*/
|
|
33
|
+
variant?: PriceProps['variant'];
|
|
34
|
+
/**
|
|
35
|
+
* Formatter function that transforms the raw price value and render the result.
|
|
36
|
+
*/
|
|
37
|
+
formatter: PriceProps['formatter'];
|
|
38
|
+
/**
|
|
39
|
+
* Returns the value of element's class content attribute.
|
|
40
|
+
*/
|
|
41
|
+
className?: string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Defines a string value that labels the current element.
|
|
44
|
+
*/
|
|
45
|
+
'aria-label'?: AriaAttributes['aria-label'];
|
|
46
|
+
} & React.RefAttributes<PriceRangeRefType | undefined>>;
|
|
23
47
|
export default PriceRange;
|
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
import React, { useState } from 'react';
|
|
1
|
+
import React, { useState, useRef, useImperativeHandle, forwardRef } from 'react';
|
|
2
2
|
import Price from '../../atoms/Price';
|
|
3
3
|
import Slider from '../../atoms/Slider';
|
|
4
|
-
const PriceRange = ({ className, formatter, max, min, onChange, onEnd, testId = 'store-price-range', variant, 'aria-label': ariaLabel, })
|
|
4
|
+
const PriceRange = forwardRef(function PriceRange({ className, formatter, max, min, onChange, onEnd, testId = 'store-price-range', variant, 'aria-label': ariaLabel, }, ref) {
|
|
5
|
+
const sliderRef = useRef();
|
|
5
6
|
const [edges, setEdges] = useState({ min: min.selected, max: max.selected });
|
|
7
|
+
useImperativeHandle(ref, () => ({
|
|
8
|
+
setPriceRangeValues: (values) => {
|
|
9
|
+
setEdges(values);
|
|
10
|
+
onChange?.(values);
|
|
11
|
+
sliderRef.current?.setSliderValues(values);
|
|
12
|
+
},
|
|
13
|
+
}));
|
|
6
14
|
return (React.createElement("div", { "data-store-price-range": true, "data-testid": testId, className: className },
|
|
7
|
-
React.createElement(Slider, { min: min, max: max, onEnd: onEnd, onChange: (value) => {
|
|
15
|
+
React.createElement(Slider, { ref: sliderRef, min: min, max: max, onEnd: onEnd, onChange: (value) => {
|
|
8
16
|
setEdges(value);
|
|
9
17
|
onChange?.(value);
|
|
10
18
|
}, "aria-label": ariaLabel }),
|
|
11
19
|
React.createElement("div", { "data-price-range-values": true },
|
|
12
20
|
React.createElement(Price, { formatter: formatter, "data-price-range-value": "min", value: edges.min, variant: variant }),
|
|
13
21
|
React.createElement(Price, { formatter: formatter, "data-price-range-value": "max", value: edges.max, variant: variant }))));
|
|
14
|
-
};
|
|
22
|
+
});
|
|
15
23
|
export default PriceRange;
|
|
16
24
|
//# sourceMappingURL=PriceRange.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PriceRange.js","sourceRoot":"","sources":["../../../src/molecules/PriceRange/PriceRange.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"PriceRange.js","sourceRoot":"","sources":["../../../src/molecules/PriceRange/PriceRange.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAGhF,OAAO,KAAK,MAAM,mBAAmB,CAAA;AACrC,OAAO,MAAM,MAAM,oBAAoB,CAAA;AA2B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AE,OAAO,GAChB,CACE,CACF,CACP,CAAA;AACH,CAAC,CACF,CAAA;AAED,eAAe,UAAU,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/ui",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.7",
|
|
4
4
|
"description": "A lightweight, framework agnostic component library for React",
|
|
5
5
|
"author": "emersonlaurentino",
|
|
6
6
|
"license": "MIT",
|
|
@@ -86,5 +86,5 @@
|
|
|
86
86
|
"tsdx": "^0.14.1",
|
|
87
87
|
"typescript": "^4.2.4"
|
|
88
88
|
},
|
|
89
|
-
"gitHead": "
|
|
89
|
+
"gitHead": "ab0f41d7b3fba483a6ba1afcbb78e6dfc766380d"
|
|
90
90
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This code is inspired by the work of [sandra-lewis](https://codesandbox.io/u/sandra-lewis)
|
|
3
3
|
*/
|
|
4
|
-
import React, {
|
|
4
|
+
import React, {
|
|
5
|
+
useState,
|
|
6
|
+
useMemo,
|
|
7
|
+
useImperativeHandle,
|
|
8
|
+
forwardRef,
|
|
9
|
+
} from 'react'
|
|
5
10
|
|
|
6
11
|
interface Range {
|
|
7
12
|
absolute: number
|
|
@@ -41,83 +46,105 @@ export type SliderProps = {
|
|
|
41
46
|
className?: string
|
|
42
47
|
}
|
|
43
48
|
|
|
49
|
+
type SliderRefType = {
|
|
50
|
+
setSliderValues: (values: { min: number; max: number }) => void
|
|
51
|
+
}
|
|
52
|
+
|
|
44
53
|
const percent = (value: number, min: number, max: number) =>
|
|
45
54
|
Math.round(((value - min) / (max - min)) * 100)
|
|
46
55
|
|
|
47
|
-
const Slider = (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const Slider = forwardRef<SliderRefType | undefined, SliderProps>(
|
|
57
|
+
function Slider(
|
|
58
|
+
{
|
|
59
|
+
min,
|
|
60
|
+
max,
|
|
61
|
+
onChange,
|
|
62
|
+
onEnd,
|
|
63
|
+
testId = 'store-slider',
|
|
64
|
+
getAriaValueText,
|
|
65
|
+
className,
|
|
66
|
+
},
|
|
67
|
+
ref
|
|
68
|
+
) {
|
|
69
|
+
const [minPercent, setMinPercent] = useState(() =>
|
|
70
|
+
percent(min.selected, min.absolute, max.absolute)
|
|
71
|
+
)
|
|
59
72
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
73
|
+
const [maxPercent, setMaxPercent] = useState(() =>
|
|
74
|
+
percent(max.selected, min.absolute, max.absolute)
|
|
75
|
+
)
|
|
63
76
|
|
|
64
|
-
|
|
65
|
-
|
|
77
|
+
const { minVal, maxVal } = useMemo(() => {
|
|
78
|
+
const widthPercent = (max.absolute - min.absolute) / 100
|
|
66
79
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
80
|
+
return {
|
|
81
|
+
minVal: min.absolute + minPercent * widthPercent,
|
|
82
|
+
maxVal: min.absolute + maxPercent * widthPercent,
|
|
83
|
+
}
|
|
84
|
+
}, [min, max, maxPercent, minPercent])
|
|
72
85
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
data-slider-range
|
|
78
|
-
/>
|
|
79
|
-
<input
|
|
80
|
-
type="range"
|
|
81
|
-
min={min.absolute}
|
|
82
|
-
max={max.absolute}
|
|
83
|
-
value={minVal}
|
|
84
|
-
onMouseUp={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
85
|
-
onTouchEnd={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
86
|
-
onChange={(event) => {
|
|
87
|
-
const minValue = Math.min(Number(event.target.value), maxVal)
|
|
86
|
+
useImperativeHandle(ref, () => ({
|
|
87
|
+
setSliderValues: (values: { min: number; max: number }) => {
|
|
88
|
+
const sliderMinValue = Math.min(Number(values.min), maxVal)
|
|
89
|
+
const sliderMaxValue = Math.max(Number(values.max), minVal)
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
aria-valuemin={min.absolute}
|
|
94
|
-
aria-valuemax={max.absolute}
|
|
95
|
-
aria-valuenow={minVal}
|
|
96
|
-
aria-label={String(minVal)}
|
|
97
|
-
aria-labelledby={getAriaValueText?.(minVal, 'min')}
|
|
98
|
-
/>
|
|
99
|
-
<input
|
|
100
|
-
type="range"
|
|
101
|
-
min={min.absolute}
|
|
102
|
-
max={max.absolute}
|
|
103
|
-
value={maxVal}
|
|
104
|
-
onMouseUp={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
105
|
-
onTouchEnd={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
106
|
-
onChange={(event) => {
|
|
107
|
-
const maxValue = Math.max(Number(event.target.value), minVal)
|
|
91
|
+
setMinPercent(percent(sliderMinValue, min.absolute, max.absolute))
|
|
92
|
+
setMaxPercent(percent(sliderMaxValue, min.absolute, max.absolute))
|
|
93
|
+
},
|
|
94
|
+
}))
|
|
108
95
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
96
|
+
return (
|
|
97
|
+
<div data-store-slider data-testid={testId} className={className}>
|
|
98
|
+
<div
|
|
99
|
+
style={{
|
|
100
|
+
left: `${minPercent}%`,
|
|
101
|
+
width: `${maxPercent - minPercent}%`,
|
|
102
|
+
}}
|
|
103
|
+
data-slider-range
|
|
104
|
+
/>
|
|
105
|
+
<input
|
|
106
|
+
type="range"
|
|
107
|
+
min={min.absolute}
|
|
108
|
+
max={max.absolute}
|
|
109
|
+
value={minVal}
|
|
110
|
+
onMouseUp={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
111
|
+
onTouchEnd={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
112
|
+
onChange={(event) => {
|
|
113
|
+
const minValue = Math.min(Number(event.target.value), maxVal)
|
|
114
|
+
|
|
115
|
+
setMinPercent(percent(minValue, min.absolute, max.absolute))
|
|
116
|
+
onChange?.({ min: minValue, max: maxVal })
|
|
117
|
+
}}
|
|
118
|
+
data-slider-thumb="left"
|
|
119
|
+
aria-valuemin={min.absolute}
|
|
120
|
+
aria-valuemax={max.absolute}
|
|
121
|
+
aria-valuenow={minVal}
|
|
122
|
+
aria-label={String(minVal)}
|
|
123
|
+
aria-labelledby={getAriaValueText?.(minVal, 'min')}
|
|
124
|
+
/>
|
|
125
|
+
<input
|
|
126
|
+
type="range"
|
|
127
|
+
min={min.absolute}
|
|
128
|
+
max={max.absolute}
|
|
129
|
+
value={maxVal}
|
|
130
|
+
onMouseUp={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
131
|
+
onTouchEnd={() => onEnd?.({ min: minVal, max: maxVal })}
|
|
132
|
+
onChange={(event) => {
|
|
133
|
+
const maxValue = Math.max(Number(event.target.value), minVal)
|
|
134
|
+
|
|
135
|
+
setMaxPercent(percent(maxValue, min.absolute, max.absolute))
|
|
136
|
+
onChange?.({ min: minVal, max: maxValue })
|
|
137
|
+
}}
|
|
138
|
+
data-slider-thumb="right"
|
|
139
|
+
aria-valuemin={min.absolute}
|
|
140
|
+
aria-valuemax={max.absolute}
|
|
141
|
+
aria-valuenow={maxVal}
|
|
142
|
+
aria-label={String(maxVal)}
|
|
143
|
+
aria-labelledby={getAriaValueText?.(maxVal, 'max')}
|
|
144
|
+
/>
|
|
145
|
+
</div>
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
)
|
|
122
149
|
|
|
123
150
|
export default Slider
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useState } from 'react'
|
|
1
|
+
import React, { useState, useRef, useImperativeHandle, forwardRef } from 'react'
|
|
2
2
|
import type { AriaAttributes } from 'react'
|
|
3
3
|
|
|
4
4
|
import Price from '../../atoms/Price'
|
|
@@ -25,47 +25,68 @@ export type PriceRangeProps = SliderProps & {
|
|
|
25
25
|
'aria-label'?: AriaAttributes['aria-label']
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
max,
|
|
32
|
-
min,
|
|
33
|
-
onChange,
|
|
34
|
-
onEnd,
|
|
35
|
-
testId = 'store-price-range',
|
|
36
|
-
variant,
|
|
37
|
-
'aria-label': ariaLabel,
|
|
38
|
-
}: PriceRangeProps) => {
|
|
39
|
-
const [edges, setEdges] = useState({ min: min.selected, max: max.selected })
|
|
28
|
+
type PriceRangeRefType = {
|
|
29
|
+
setPriceRangeValues: (values: { min: number; max: number }) => void
|
|
30
|
+
}
|
|
40
31
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
32
|
+
const PriceRange = forwardRef<PriceRangeRefType | undefined, PriceRangeProps>(
|
|
33
|
+
function PriceRange(
|
|
34
|
+
{
|
|
35
|
+
className,
|
|
36
|
+
formatter,
|
|
37
|
+
max,
|
|
38
|
+
min,
|
|
39
|
+
onChange,
|
|
40
|
+
onEnd,
|
|
41
|
+
testId = 'store-price-range',
|
|
42
|
+
variant,
|
|
43
|
+
'aria-label': ariaLabel,
|
|
44
|
+
},
|
|
45
|
+
ref
|
|
46
|
+
) {
|
|
47
|
+
const sliderRef = useRef<{
|
|
48
|
+
setSliderValues: (values: { min: number; max: number }) => void
|
|
49
|
+
}>()
|
|
50
|
+
const [edges, setEdges] = useState({ min: min.selected, max: max.selected })
|
|
51
|
+
|
|
52
|
+
useImperativeHandle(ref, () => ({
|
|
53
|
+
setPriceRangeValues: (values: { min: number; max: number }) => {
|
|
54
|
+
setEdges(values)
|
|
55
|
+
onChange?.(values)
|
|
56
|
+
sliderRef.current?.setSliderValues(values)
|
|
57
|
+
},
|
|
58
|
+
}))
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div data-store-price-range data-testid={testId} className={className}>
|
|
62
|
+
<Slider
|
|
63
|
+
ref={sliderRef}
|
|
64
|
+
min={min}
|
|
65
|
+
max={max}
|
|
66
|
+
onEnd={onEnd}
|
|
67
|
+
onChange={(value) => {
|
|
68
|
+
setEdges(value)
|
|
69
|
+
onChange?.(value)
|
|
70
|
+
}}
|
|
71
|
+
aria-label={ariaLabel}
|
|
65
72
|
/>
|
|
73
|
+
<div data-price-range-values>
|
|
74
|
+
<Price
|
|
75
|
+
formatter={formatter}
|
|
76
|
+
data-price-range-value="min"
|
|
77
|
+
value={edges.min}
|
|
78
|
+
variant={variant}
|
|
79
|
+
/>
|
|
80
|
+
<Price
|
|
81
|
+
formatter={formatter}
|
|
82
|
+
data-price-range-value="max"
|
|
83
|
+
value={edges.max}
|
|
84
|
+
variant={variant}
|
|
85
|
+
/>
|
|
86
|
+
</div>
|
|
66
87
|
</div>
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
)
|
|
70
91
|
|
|
71
92
|
export default PriceRange
|