Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resetOnMinMaxChange to the Slider component #1438

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- Prevents `Slider` range from resetting when the min/max value changes.

## [9.146.3] - 2022-09-08

### Fix
Expand Down
8 changes: 6 additions & 2 deletions react/components/Slider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const handleInputChange = event => {
}
}

const handleSubmit = (e) => {
const handleSubmit = e => {
e.preventDefault()

const { left: leftValueInput, right: rightValueInput } = inputValues
Expand Down Expand Up @@ -89,7 +89,11 @@ const handleSubmit = (e) => {
value={inputValues.right}
/>
</div>
<Button type="submit" onClick={handleSubmit} variation="primary" size="small">
<Button
type="submit"
onClick={handleSubmit}
variation="primary"
size="small">
Submit
</Button>
</form>
Expand Down
8 changes: 4 additions & 4 deletions react/components/Slider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ export default class Slider extends Component {
componentDidUpdate(prevProps) {
if (prevProps.min !== this.props.min || prevProps.max !== this.props.max) {
this.setState(
{
prev => ({
translate: {
left: 0,
right: 0,
},
values: {
left: this.props.min,
right: this.props.max,
left: prev.values.left ?? this.props.min,
right: prev.values.right ?? this.props.max,
},
},
}),
this.updateLayout
)
}
Expand Down
39 changes: 38 additions & 1 deletion react/playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,47 @@ import React from 'react'

import PageHeader from '../PageHeader'
import Layout from '../Layout'
import Button from '../Button'
import Slider from '../Slider'

const SliderTest = () => {
const [max, setMax] = React.useState(10)
const defaultValues = [2, 8]

const handleSubmit = e => {
e.preventDefault()

setMax(value => value + 10)
}

return (
<>
<form className="flex items-end mb5">
<Button
type="Increase max value +10"
onClick={handleSubmit}
variation="primary"
size="small">
Increase max value +10
</Button>
</form>

<div className="mt8">
<Slider
min={0}
max={max}
alwaysShowCurrentValue={false}
range
defaultValues={defaultValues}
/>
</div>
</>
)
}

const Playground = () => (
<Layout fullWidth pageHeader={<PageHeader title="Playground" />}>
{/* Add your code here, don't forget to delete after */}
<SliderTest />
</Layout>
)

Expand Down