-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Slider] Allow disabling the left and right thumbs swap (#25547)
- Loading branch information
1 parent
fa475e2
commit 7729b92
Showing
12 changed files
with
277 additions
and
14 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
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
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,73 @@ | ||
import * as React from 'react'; | ||
import Box from '@material-ui/core/Box'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Slider from '@material-ui/core/Slider'; | ||
|
||
function valuetext(value) { | ||
return `${value}°C`; | ||
} | ||
|
||
const minDistance = 10; | ||
|
||
export default function MinimumDistanceSlider() { | ||
const [value1, setValue1] = React.useState([20, 37]); | ||
|
||
const handleChange1 = (event, newValue, activeThumb) => { | ||
if (!Array.isArray(newValue)) { | ||
return; | ||
} | ||
|
||
if (activeThumb === 0) { | ||
setValue1([Math.min(newValue[0], value1[1] - minDistance), value1[1]]); | ||
} else { | ||
setValue1([value1[0], Math.max(newValue[1], value1[0] + minDistance)]); | ||
} | ||
}; | ||
|
||
const [value2, setValue2] = React.useState([20, 37]); | ||
|
||
const handleChange2 = (event, newValue, activeThumb) => { | ||
if (!Array.isArray(newValue)) { | ||
return; | ||
} | ||
|
||
if (newValue[1] - newValue[0] < minDistance) { | ||
if (activeThumb === 0) { | ||
const clamped = Math.min(newValue[0], 100 - minDistance); | ||
setValue2([clamped, clamped + minDistance]); | ||
} else { | ||
const clamped = Math.max(newValue[1], minDistance); | ||
setValue2([clamped - minDistance, clamped]); | ||
} | ||
} else { | ||
setValue2(newValue); | ||
} | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: 300 }}> | ||
<Typography id="minimum-distance-demo" gutterBottom> | ||
Minimum distance | ||
</Typography> | ||
<Slider | ||
value={value1} | ||
onChange={handleChange1} | ||
valueLabelDisplay="auto" | ||
aria-labelledby="minimum-distance-demo" | ||
getAriaValueText={valuetext} | ||
disableSwap | ||
/> | ||
<Typography id="minimum-distance-shift-demo" gutterBottom> | ||
Minimum distance shift | ||
</Typography> | ||
<Slider | ||
value={value2} | ||
onChange={handleChange2} | ||
valueLabelDisplay="auto" | ||
aria-labelledby="minimum-distance-shift-demo" | ||
getAriaValueText={valuetext} | ||
disableSwap | ||
/> | ||
</Box> | ||
); | ||
} |
81 changes: 81 additions & 0 deletions
81
docs/src/pages/components/slider/MinimumDistanceSlider.tsx
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,81 @@ | ||
import * as React from 'react'; | ||
import Box from '@material-ui/core/Box'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Slider from '@material-ui/core/Slider'; | ||
|
||
function valuetext(value: number) { | ||
return `${value}°C`; | ||
} | ||
|
||
const minDistance = 10; | ||
|
||
export default function MinimumDistanceSlider() { | ||
const [value1, setValue1] = React.useState<number[]>([20, 37]); | ||
|
||
const handleChange1 = ( | ||
event: Event, | ||
newValue: number | number[], | ||
activeThumb: number, | ||
) => { | ||
if (!Array.isArray(newValue)) { | ||
return; | ||
} | ||
|
||
if (activeThumb === 0) { | ||
setValue1([Math.min(newValue[0], value1[1] - minDistance), value1[1]]); | ||
} else { | ||
setValue1([value1[0], Math.max(newValue[1], value1[0] + minDistance)]); | ||
} | ||
}; | ||
|
||
const [value2, setValue2] = React.useState<number[]>([20, 37]); | ||
|
||
const handleChange2 = ( | ||
event: Event, | ||
newValue: number | number[], | ||
activeThumb: number, | ||
) => { | ||
if (!Array.isArray(newValue)) { | ||
return; | ||
} | ||
|
||
if (newValue[1] - newValue[0] < minDistance) { | ||
if (activeThumb === 0) { | ||
const clamped = Math.min(newValue[0], 100 - minDistance); | ||
setValue2([clamped, clamped + minDistance]); | ||
} else { | ||
const clamped = Math.max(newValue[1], minDistance); | ||
setValue2([clamped - minDistance, clamped]); | ||
} | ||
} else { | ||
setValue2(newValue as number[]); | ||
} | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: 300 }}> | ||
<Typography id="minimum-distance-demo" gutterBottom> | ||
Minimum distance | ||
</Typography> | ||
<Slider | ||
value={value1} | ||
onChange={handleChange1} | ||
valueLabelDisplay="auto" | ||
aria-labelledby="minimum-distance-demo" | ||
getAriaValueText={valuetext} | ||
disableSwap | ||
/> | ||
<Typography id="minimum-distance-shift-demo" gutterBottom> | ||
Minimum distance shift | ||
</Typography> | ||
<Slider | ||
value={value2} | ||
onChange={handleChange2} | ||
valueLabelDisplay="auto" | ||
aria-labelledby="minimum-distance-shift-demo" | ||
getAriaValueText={valuetext} | ||
disableSwap | ||
/> | ||
</Box> | ||
); | ||
} |
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
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
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
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
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
Oops, something went wrong.