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

refactor: trackStyle & handleStyle & railStyle #281

Merged
merged 1 commit into from
Jun 9, 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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ 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. |
| minimumTrackStyle | Object |`{}` | The style used for the track to the left of the button. |
| maximumTrackStyle | Object | `{}` | The style used for the track to the right of the button. |
| handleStyle | Object | `{}` | The style used for handle. |
| minimumTrackStyle | Object | | please use `trackStyle[0]` instead. (`only used for slider, just for compatibility , will be deprecate at rc-slider@9.x `) |
| maximumTrackStyle | Object | | please use `railStyle` instead (`only used for slider, just for compatibility , will be deprecate at rc-slider@9.x`) |
| handleStyle | Array[Object] \| Object | `[{}]` | The style used for handle. (`both for slider and range, the array will be used for mutli handle follow element order`) |
| trackStyle | Array[Object] | `[{}]` | The style used for track. (`both for slider and range, the array will be used for mutli track follow element order`)|
| railStyle | Object | `{}` | The style used for the track base color. |


### Slider

Expand Down
8 changes: 8 additions & 0 deletions examples/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ ReactDOM.render(
<p>Multi Range</p>
<Range count={3} defaultValue={[20, 40, 60, 80]} pushable />
</div>
<div style={style}>
<p>Multi Range with custom track and handle style</p>
<Range count={3} defaultValue={[20, 40, 60, 80]} pushable
trackStyle={[{ backgroundColor: 'red' }, { backgroundColor: 'green' }]}
handleStyle={[{ backgroundColor: 'yellow' }, { backgroundColor: 'gray' }]}
railStyle={{ backgroundColor: 'black' }}
/>
</div>
<div style={style}>
<p>Customized Range</p>
<CustomizedRange />
Expand Down
24 changes: 20 additions & 4 deletions examples/slider.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint react/no-multi-comp: 0 */
/* eslint react/no-multi-comp: 0, max-len: 0 */
import 'rc-slider/assets/index.less';

import React from 'react';
import ReactDOM from 'react-dom';
import Slider from 'rc-slider';

const style = { width: 400, margin: 50 };
const style = { width: 600, margin: 50 };

function log(value) {
console.log(value); //eslint-disable-line
Expand Down Expand Up @@ -104,7 +104,7 @@ ReactDOM.render(
<Slider tipFormatter={null} onChange={log} />
</div>
<div style={style}>
<p>Slider with custom handle and track style</p>
<p>Slider with custom handle and track style.<strong>(old api, will be deperacete)</strong></p>
<Slider
defaultValue={30}
maximumTrackStyle={{ backgroundColor: 'red', height: 10 }}
Expand All @@ -115,10 +115,26 @@ ReactDOM.render(
width: 28,
marginLeft: -14,
marginTop: -9,
backgroundColor: 'blue',
backgroundColor: 'black',
}}
/>
</div>
<div style={style}>
<p>Slider with custom handle and track style.<strong>(The recommended new api)</strong></p>
<Slider
defaultValue={30}
trackStyle={[{ backgroundColor: 'blue', height: 10 }]}
handleStyle={[{
borderColor: 'blue',
height: 28,
width: 28,
marginLeft: -14,
marginTop: -9,
backgroundColor: 'black',
}]}
railStyle={{ backgroundColor: 'red', height: 10 }}
/>
</div>
<div style={style}>
<p>Basic Slider, disabled</p>
<Slider tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log} disabled />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-slider",
"version": "8.0.2",
"version": "8.1.0",
"description": "Slider UI component for React",
"keywords": [
"react",
Expand Down
11 changes: 7 additions & 4 deletions src/Handle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import PropTypes from 'prop-types';
export default class Handle extends React.Component {
render() {
const {
className, vertical, offset, handleStyle, disabled, min, max, value, ...restProps,
className, vertical, offset, style, disabled, min, max, value, ...restProps,
} = this.props;

const style = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` };
const elStyle = { ...style, ...handleStyle };
const postionStyle = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` };
const elStyle = {
...style,
...postionStyle,
};
let ariaProps = {};
if (value !== undefined) {
ariaProps = {
Expand All @@ -35,7 +38,7 @@ Handle.propTypes = {
className: PropTypes.string,
vertical: PropTypes.bool,
offset: PropTypes.number,
handleStyle: PropTypes.object,
style: PropTypes.object,
disabled: PropTypes.bool,
min: PropTypes.number,
max: PropTypes.number,
Expand Down
4 changes: 4 additions & 0 deletions src/Range.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ class Range extends React.Component {
min,
max,
handle: handleGenerator,
trackStyle,
handleStyle,
} = this.props;

const offsets = bounds.map(v => this.calcOffset(v));
Expand All @@ -303,6 +305,7 @@ class Range extends React.Component {
min,
max,
disabled,
style: handleStyle[i],
ref: h => this.saveHandle(i, h),
}));

Expand All @@ -319,6 +322,7 @@ class Range extends React.Component {
included={included}
offset={offsets[i - 1]}
length={offsets[i] - offsets[i - 1]}
style={trackStyle[index]}
key={i}
/>
);
Expand Down
12 changes: 7 additions & 5 deletions src/Slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import createSlider from './common/createSlider';
import * as utils from './utils';

class Slider extends React.Component {
static displayName = 'Slider';
static propTypes = {
defaultValue: PropTypes.number,
value: PropTypes.number,
disabled: PropTypes.bool,
};

static defaultProps = {};

constructor(props) {
super(props);

Expand Down Expand Up @@ -111,6 +108,7 @@ class Slider extends React.Component {
included,
disabled,
minimumTrackStyle,
trackStyle,
handleStyle,
min,
max,
Expand All @@ -127,17 +125,21 @@ class Slider extends React.Component {
disabled,
min,
max,
handleStyle,
style: handleStyle[0] || handleStyle,
ref: h => this.saveHandle(0, h),
});

const track = (
<Track
className={`${prefixCls}-track`}
vertical={vertical}
included={included}
offset={0}
length={offset}
minimumTrackStyle={minimumTrackStyle}
style={{
...minimumTrackStyle,
...trackStyle[0],
}}
/>
);

Expand Down
26 changes: 13 additions & 13 deletions src/common/Track.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* eslint-disable react/prop-types */
import React from 'react';

const Track = ({
className, included, vertical, offset, length, minimumTrackStyle,
}) => {
const style = {
visibility: included ? 'visible' : 'hidden',
const Track = (props) => {
const { className, included, vertical, offset, length, style } = props;

const positonStyle = vertical ? {
bottom: `${offset}%`,
height: `${length}%`,
} : {
left: `${offset}%`,
width: `${length}%`,
};
if (vertical) {
style.bottom = `${offset}%`;
style.height = `${length}%`;
} else {
style.left = `${offset}%`;
style.width = `${length}%`;
}

const elStyle = {
visibility: included ? 'visible' : 'hidden',
...style,
...minimumTrackStyle,
...positonStyle,
};
return <div className={className} style={elStyle} />;
};
Expand Down
36 changes: 25 additions & 11 deletions src/common/createSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export default function createSlider(Component) {
dots: PropTypes.bool,
vertical: PropTypes.bool,
style: PropTypes.object,
minimumTrackStyle: PropTypes.object,
maximumTrackStyle: PropTypes.object,
handleStyle: PropTypes.object,
minimumTrackStyle: PropTypes.object, // just for compatibility, will be deperecate
maximumTrackStyle: PropTypes.object, // just for compatibility, will be deperecate
handleStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]),
trackStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]),
railStyle: PropTypes.object,
};

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

constructor(props) {
Expand All @@ -71,8 +73,15 @@ export default function createSlider(Component) {
max - min,
step
);
warning(
!('minimumTrackStyle' in props),
'minimumTrackStyle will be deprecate, please use trackStyle[0] instead.'
);
warning(
!('maximumTrackStyle' in props),
'maximumTrackStyle will be deprecate, please use railStyle instead.'
);
}

this.handlesRefs = {};
}

Expand Down Expand Up @@ -214,17 +223,16 @@ export default function createSlider(Component) {
children,
maximumTrackStyle,
style,
railStyle,
} = this.props;
const { tracks, handles } = super.render();

const sliderClassName = classNames({
[prefixCls]: true,
const sliderClassName = classNames(prefixCls, {
[`${prefixCls}-with-marks`]: Object.keys(marks).length,
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-vertical`]: vertical,
[className]: className,
});

return (
<div
ref={this.saveSlider}
Expand All @@ -233,7 +241,13 @@ export default function createSlider(Component) {
onMouseDown={disabled ? noop : this.onMouseDown}
style={style}
>
<div className={`${prefixCls}-rail`} style={maximumTrackStyle} />
<div
className={`${prefixCls}-rail`}
style={{
...maximumTrackStyle,
...railStyle,
}}
/>
{tracks}
<Steps
prefixCls={prefixCls}
Expand Down
7 changes: 6 additions & 1 deletion src/createSliderWithTooltip.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export default function createSliderWithTooltip(Component) {
return class ComponentWrapper extends React.Component {
static propTypes = {
tipFormatter: PropTypes.func,
handleStyle: PropTypes.arrayOf(PropTypes.object),
};
static defaultProps = {
tipFormatter(value) { return value; },
handleStyle: [{}],
};
constructor(props) {
super(props);
Expand All @@ -26,7 +28,7 @@ export default function createSliderWithTooltip(Component) {
});
}
handleWithTooltip = ({ value, dragging, index, disabled, ...restProps }) => {
const { tipFormatter } = this.props;
const { tipFormatter, handleStyle } = this.props;
return (
<Tooltip
prefixCls="rc-slider-tooltip"
Expand All @@ -37,6 +39,9 @@ export default function createSliderWithTooltip(Component) {
>
<Handle
{...restProps}
style={{
...handleStyle[0],
}}
value={value}
onMouseEnter={() => this.handleTooltipVisibleChange(index, true)}
onMouseLeave={() => this.handleTooltipVisibleChange(index, false)}
Expand Down