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

support custom track & handle style, for react-tiny #249

Merged
merged 1 commit into from
Apr 7, 2017
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ The following APIs are shared by Slider and Range.
| onBeforeChange | Function | NOOP | `onBeforeChange` will be triggered when `ontouchstart` or `onmousedown` is triggered. |
| onChange | Function | NOOP | `onChange` will be triggered while the value of Slider changing. |
| onAfterChange | Function | NOOP | `onAfterChange` will be triggered when `ontouchend` or `onmouseup` is triggered. |
| minimumTrackTintColor | String | | The color used for the track to the left of the button. |
| maximumTrackTintColor | String | | The color used for the track to the right of the button. |
| minimumTrackStyle | Object |`{}` | The style used for the track to the left of the button. |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了源码和内网 issue 才知道这个 API 是干嘛用的 😢

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于单滑块,这个命名是没问题的,但对于多滑块,就含义不清了。。不过算了,先用着看看吧。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩多滑块貌似是不适合,当时也没想到好名字,就跟着 RN 来了

| maximumTrackStyle | Object | `{}` | The style used for the track to the right of the button. |
| handleStyle | Object | `{}` | The style used for handle. |

### Slider

Expand Down
3 changes: 2 additions & 1 deletion assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
width: 100%;
background-color: #e9e9e9;
height: 4px;
border-radius: @border-radius-base;
}

&-track {
Expand Down Expand Up @@ -286,4 +287,4 @@
border-width: @tooltip-arrow-width @tooltip-arrow-width 0;
border-top-color: @tooltip-arrow-color;
}
}
}
16 changes: 16 additions & 0 deletions examples/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ ReactDOM.render(
<p>Basic Slider without tooltip</p>
<Slider tipFormatter={null} onChange={log} />
</div>
<div style={style}>
<p>Slider with custom handle and track style</p>
<Slider
defaultValue={30}
maximumTrackStyle={{ backgroundColor: 'red', height: 10 }}
minimumTrackStyle={{ backgroundColor: 'blue', height: 10 }}
handleStyle={{
borderColor: 'blue',
height: 28,
width: 28,
marginLeft: -14,
marginTop: -9,
backgroundColor: 'blue',
}}
/>
</div>
<div style={style}>
<p>Basic Slider, disabled</p>
<Slider tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log} disabled />
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "rc-slider",

"version": "6.3.1",
"description": "Slider UI component for React",
"keywords": [
Expand All @@ -26,7 +25,10 @@
"license": "MIT",
"main": "./lib/index.js",
"entry": {
"rc-slider": ["./assets/index.less", "./src/index.js"]
"rc-slider": [
"./assets/index.less",
"./src/index.js"
]
},
"style": "./assets/index.css",
"config": {
Expand Down Expand Up @@ -66,6 +68,7 @@
"dependencies": {
"babel-runtime": "6.x",
"classnames": "^2.2.5",
"object-assign": "^4.1.1",
"rc-tooltip": "^3.4.2",
"rc-util": "^4.0.0",
"warning": "^3.0.0"
Expand Down
11 changes: 4 additions & 7 deletions src/Handle.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import React, { PropTypes } from 'react';
import assign from 'object-assign';

export default class Handle extends React.Component {
render() {
const {
className, vertical, offset, minimumTrackTintColor, disabled, ...restProps,
className, vertical, offset, handleStyle, ...restProps,
} = this.props;
const style = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` };
if (minimumTrackTintColor && !disabled) {
style.borderColor = minimumTrackTintColor;
}
return <div {...restProps} className={className} style={style} />;
return <div {...restProps} className={className} style={assign({}, style, handleStyle)} />;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disabled 的背景色逻辑不需要了?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why import object-assign here when you're literally spreading objects all over?

This should be style={{...style, ...handleStyle}}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@STRML fixed in 441779c

}
}

Handle.propTypes = {
className: PropTypes.string,
vertical: PropTypes.bool,
offset: PropTypes.number,
minimumTrackTintColor: PropTypes.string,
disabled: PropTypes.bool,
handleStyle: PropTypes.object,
};
8 changes: 4 additions & 4 deletions src/Slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class Slider extends React.Component {
vertical,
included,
disabled,
minimumTrackTintColor,
minimumTrackStyle,
handleStyle,
handle: handleGenerator,
} = this.props;
const { value, dragging } = this.state;
Expand All @@ -121,7 +122,7 @@ class Slider extends React.Component {
value,
dragging,
disabled,
minimumTrackTintColor,
handleStyle,
ref: h => this.saveHandle(0, h),
});
const track = (
Expand All @@ -130,9 +131,8 @@ class Slider extends React.Component {
vertical={vertical}
included={included}
offset={0}
disabled={disabled}
length={offset}
minimumTrackTintColor={minimumTrackTintColor}
minimumTrackStyle={minimumTrackStyle}
/>
);

Expand Down
9 changes: 4 additions & 5 deletions src/common/Track.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import assign from 'object-assign';

const Track = ({
className, included, vertical, offset, length, minimumTrackTintColor, disabled,
className, included, vertical, offset, length, minimumTrackStyle,
}) => {
const style = {
visibility: included ? 'visible' : 'hidden',
Expand All @@ -13,10 +14,8 @@ const Track = ({
style.left = `${offset}%`;
style.width = `${length}%`;
}
if (minimumTrackTintColor && !disabled) {
style.backgroundColor = minimumTrackTintColor;
}
return <div className={className} style={style} />;

return <div className={className} style={assign({}, style, minimumTrackStyle)} />;
};

export default Track;
15 changes: 8 additions & 7 deletions src/common/createSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export default function createSlider(Component) {
dots: PropTypes.bool,
vertical: PropTypes.bool,
style: PropTypes.object,
maximumTrackTintColor: PropTypes.string,
minimumTrackStyle: PropTypes.object,
maximumTrackStyle: PropTypes.object,
handleStyle: PropTypes.object,
};

static defaultProps = {
Expand All @@ -53,6 +55,9 @@ export default function createSlider(Component) {
disabled: false,
dots: false,
vertical: false,
minimumTrackStyle: {},
maximumTrackStyle: {},
handleStyle: {},
};

constructor(props) {
Expand Down Expand Up @@ -207,7 +212,7 @@ export default function createSlider(Component) {
min,
max,
children,
maximumTrackTintColor,
maximumTrackStyle,
style,
} = this.props;
const { tracks, handles } = super.render();
Expand All @@ -220,10 +225,6 @@ export default function createSlider(Component) {
[className]: className,
});

const trackStyle = maximumTrackTintColor && !disabled ? {
backgroundColor: maximumTrackTintColor,
} : {};

return (
<div
ref={this.saveSlider}
Expand All @@ -232,7 +233,7 @@ export default function createSlider(Component) {
onMouseDown={disabled ? noop : this.onMouseDown}
style={style}
>
<div className={`${prefixCls}-rail`} style={trackStyle} />
<div className={`${prefixCls}-rail`} style={maximumTrackStyle} />
{tracks}
<Steps
prefixCls={prefixCls}
Expand Down