Skip to content

Commit 6c8c345

Browse files
authored
Merge pull request #1438 from vtex/feature/slider-dynamic-min-max-support
Add resetOnMinMaxChange to the Slider component
2 parents a91b69a + a691044 commit 6c8c345

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Prevents `Slider` range from resetting when the min/max value changes.
13+
1014
## [9.146.3] - 2022-09-08
1115

1216
### Fix

react/components/Slider/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const handleInputChange = event => {
4848
}
4949
}
5050

51-
const handleSubmit = (e) => {
51+
const handleSubmit = e => {
5252
e.preventDefault()
5353

5454
const { left: leftValueInput, right: rightValueInput } = inputValues
@@ -89,7 +89,11 @@ const handleSubmit = (e) => {
8989
value={inputValues.right}
9090
/>
9191
</div>
92-
<Button type="submit" onClick={handleSubmit} variation="primary" size="small">
92+
<Button
93+
type="submit"
94+
onClick={handleSubmit}
95+
variation="primary"
96+
size="small">
9397
Submit
9498
</Button>
9599
</form>

react/components/Slider/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ export default class Slider extends Component {
6666
componentDidUpdate(prevProps) {
6767
if (prevProps.min !== this.props.min || prevProps.max !== this.props.max) {
6868
this.setState(
69-
{
69+
prev => ({
7070
translate: {
7171
left: 0,
7272
right: 0,
7373
},
7474
values: {
75-
left: this.props.min,
76-
right: this.props.max,
75+
left: prev.values.left ?? this.props.min,
76+
right: prev.values.right ?? this.props.max,
7777
},
78-
},
78+
}),
7979
this.updateLayout
8080
)
8181
}

react/playground/Playground.tsx

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,47 @@ import React from 'react'
22

33
import PageHeader from '../PageHeader'
44
import Layout from '../Layout'
5+
import Button from '../Button'
6+
import Slider from '../Slider'
7+
8+
const SliderTest = () => {
9+
const [max, setMax] = React.useState(10)
10+
const defaultValues = [2, 8]
11+
12+
const handleSubmit = e => {
13+
e.preventDefault()
14+
15+
setMax(value => value + 10)
16+
}
17+
18+
return (
19+
<>
20+
<form className="flex items-end mb5">
21+
<Button
22+
type="Increase max value +10"
23+
onClick={handleSubmit}
24+
variation="primary"
25+
size="small">
26+
Increase max value +10
27+
</Button>
28+
</form>
29+
30+
<div className="mt8">
31+
<Slider
32+
min={0}
33+
max={max}
34+
alwaysShowCurrentValue={false}
35+
range
36+
defaultValues={defaultValues}
37+
/>
38+
</div>
39+
</>
40+
)
41+
}
542

643
const Playground = () => (
744
<Layout fullWidth pageHeader={<PageHeader title="Playground" />}>
8-
{/* Add your code here, don't forget to delete after */}
45+
<SliderTest />
946
</Layout>
1047
)
1148

0 commit comments

Comments
 (0)