-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[Slider] Feature Custom Icon #12600
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
[Slider] Feature Custom Icon #12600
Changes from 10 commits
979c0cd
cac07cd
49b716f
201177e
1efcbf3
5933838
b30e4b5
a3c4f77
4fa5ffa
6b91465
fad0478
abe9c9c
3889145
3a858e8
06ba37f
c4639d0
46750f3
cd6f752
617bf64
ba8c1e4
fea9ed5
9c6d598
5f29eb3
ab55c29
185aa2e
d19e900
8e503c4
385b56c
7a92eaa
507e0e2
7f7611e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { withStyles } from '@material-ui/core/styles'; | ||
| import Typography from '@material-ui/core/Typography'; | ||
| import Slider from '@material-ui/lab/Slider'; | ||
| import StarsIcon from '@material-ui/icons/Gamepad'; | ||
|
|
||
| const styles = { | ||
| root: { | ||
| width: 300, | ||
| }, | ||
| thumbIcon: { | ||
| borderRadius: '50%', | ||
| }, | ||
| }; | ||
|
|
||
| class CustomIconSlider extends React.Component { | ||
| state = { | ||
| value: 50, | ||
| }; | ||
|
|
||
| handleChange = (event, value) => { | ||
| this.setState({ value }); | ||
| }; | ||
|
|
||
| render() { | ||
| const { classes } = this.props; | ||
| const { value } = this.state; | ||
|
|
||
| return ( | ||
| <div className={classes.root}> | ||
| <Typography id="label">Slider Image</Typography> | ||
|
||
| <Slider | ||
| value={value} | ||
| aria-labelledby="label" | ||
| onChange={this.handleChange} | ||
| thumb={ | ||
| <img | ||
| alt="slider thumb icon" | ||
| src="/static/images/cards/live-from-space.jpg" | ||
|
||
| className={classes.thumbIcon} | ||
| /> | ||
| } | ||
| /> | ||
| <Typography id="label">Slider Icon</Typography> | ||
|
||
| <Slider | ||
| value={value} | ||
| aria-labelledby="label" | ||
| onChange={this.handleChange} | ||
| thumb={<StarsIcon style={{ color: '#2196f3' }} />} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| CustomIconSlider.propTypes = { | ||
| classes: PropTypes.object.isRequired, | ||
| }; | ||
|
|
||
| export default withStyles(styles)(CustomIconSlider); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,17 @@ export const styles = theme => { | |
| height: 17, | ||
| }, | ||
| }, | ||
| /* Class applied to the thumb element if custom thumb icon provided` . */ | ||
|
||
| thumbIconWrapper: { | ||
| backgroundColor: 'transparent', | ||
| }, | ||
| thumbIcon: { | ||
| position: 'relative', | ||
| top: 0, | ||
| height: 26, | ||
| width: 'auto', | ||
| zIndex: 2, | ||
| }, | ||
| /* Class applied to the root element to trigger JSS nested styles if `reverse={true}` . */ | ||
| reverse: {}, | ||
| /* Class applied to the track and thumb elements to trigger JSS nested styles if `disabled`. */ | ||
|
|
@@ -375,6 +386,7 @@ class Slider extends React.Component { | |
| const { currentState } = this.state; | ||
| const { | ||
| component: Component, | ||
| thumb: Thumb, | ||
| classes, | ||
| className: classNameProp, | ||
| disabled, | ||
|
|
@@ -418,14 +430,30 @@ class Slider extends React.Component { | |
| [classes.vertical]: vertical, | ||
| }); | ||
|
|
||
| const thumbClasses = classNames(classes.thumb, commonClasses); | ||
|
|
||
| const trackProperty = vertical ? 'height' : 'width'; | ||
| const thumbProperty = vertical ? 'top' : 'left'; | ||
| const inlineTrackBeforeStyles = { [trackProperty]: this.calculateTrackBeforeStyles(percent) }; | ||
| const inlineTrackAfterStyles = { [trackProperty]: this.calculateTrackAfterStyles(percent) }; | ||
| const inlineThumbStyles = { [thumbProperty]: `${percent}%` }; | ||
|
|
||
| /** Start Thumbnail Icon Logic Here */ | ||
|
||
| const withIcon = !!Thumb; | ||
| const Thumbnail = withIcon | ||
| ? React.cloneElement(Thumb, { | ||
| ...Thumb.props, | ||
| className: `${classes.thumbIcon} ${Thumb.props.className || ''}`, | ||
| }) | ||
| : null; | ||
| /** End Thumbnail Icon Logic Here */ | ||
|
|
||
| const thumbClasses = classNames( | ||
| { | ||
| [classes.thumb]: !withIcon, | ||
| [classes.thumbIconWrapper]: withIcon, | ||
| }, | ||
| commonClasses, | ||
| ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can leverage Why not apply
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Done, I have made the changes. :)
Regarding why I am not using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I can not use the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would it look if you overwrite the conflicting styles? It might be confusing to users that this class is not always applied to the thumb.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood, i'll do something about it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have enabled
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I just wanted to say thank you for taking the time out of your busy schedule, to review my code. This really means a lot. Thank you. |
||
|
|
||
| return ( | ||
| <Component | ||
| role="slider" | ||
|
|
@@ -454,7 +482,9 @@ class Slider extends React.Component { | |
| onTouchStartCapture={this.handleTouchStart} | ||
| onTouchMove={this.handleMouseMove} | ||
| onFocusVisible={this.handleFocus} | ||
| /> | ||
| > | ||
| {Thumbnail} | ||
| </ButtonBase> | ||
| <div className={trackAfterClasses} style={inlineTrackAfterStyles} /> | ||
| </div> | ||
| </Component> | ||
|
|
@@ -515,6 +545,11 @@ Slider.propTypes = { | |
| * @ignore | ||
| */ | ||
| theme: PropTypes.object.isRequired, | ||
| /** | ||
| * The component used for the slider icon. | ||
| * This is optional, if provided should be a react element. | ||
| */ | ||
| thumb: PropTypes.element, | ||
| /** | ||
| * The value of the slider. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GamepadIcon?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Updating it :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated it to Lens icon, which is basically a filled circle.