Compare commits

..
16 Commits
Author SHA1 Message Date
Benjy Cui daaea139b9 bump 3.4.0 2016-03-17 15:48:33 +08:00
Boris Serdiuk 2641880783 Merge pull request #82 from react-component/feat-mark-style
feat: support set style of specific mark point
2016-03-17 10:30:45 +03:00
Benjy Cui d6e81a692c feat: support set style of specific mark point 2016-03-17 09:45:55 +08:00
Benjy Cui 0512ee6ee0 Merge pull request #71 from kalinichenko/master
put marks in correct order so one can style it
2016-02-02 20:53:13 +08:00
Andrey Kalinichenko 613a2ae213 put marks in correct order so one can style it 2016-02-02 11:35:10 +01:00
yiminghe 2cb0dbce6f fix: work with rc-form 2016-01-30 18:52:12 +08:00
Benjy Cui f850dab6c4 bump 3.3.1 2015-12-21 09:59:02 +08:00
Benjy Cui f3c1d4bd3d fix: the key of dots should be unique #64 2015-12-21 09:57:30 +08:00
Benjy Cui 4d795b01cc chore: update unit test 2015-12-18 17:30:34 +08:00
Benjy Cui 959007ed30 bump 3.3.0 2015-12-18 10:11:50 +08:00
Benjy Cui 079469b765 refactor: the default value of tipFormatter 2015-12-18 10:10:52 +08:00
Benjy Cui ddb2bf670b Merge pull request #61 from frank-weindel/hide-the-tip
If tipFormatter is null, don't show the tip
2015-12-18 09:18:41 +08:00
Frank Weindel 4c57f3ecca Refactor isNoTip expression 2015-12-17 10:49:29 -05:00
Frank Weindel 24f3b755b2 Add example for hiding the tooltip 2015-12-17 10:48:54 -05:00
Frank Weindel 867e252631 Add documentation on how to hide the tip 2015-12-16 16:42:13 -05:00
Frank Weindel 39de81dfa9 If tipFormatter is null, don't show the tip 2015-12-16 16:38:15 -05:00
9 changed files with 88 additions and 55 deletions
+4 -4
View File
@@ -90,9 +90,9 @@ ReactDOM.render(<Rcslider />, container);
</tr> </tr>
<tr> <tr>
<td>marks</td> <td>marks</td>
<td>object {number: string}</td> <td>object{number: string} or object{number: object{ style, label }}</td>
<td>{}</td> <td>{}</td>
<td>Mark on the slider. The key determines the position, and the value determines what will show.</td> <td>Mark on the slider. The key determines the position, and the value determines what will show. If you want to set the style of a specific mark point, the value should be an object which contains `style` and `label` properties.</td>
</tr> </tr>
<tr> <tr>
<td>step</td> <td>step</td>
@@ -144,9 +144,9 @@ ReactDOM.render(<Rcslider />, container);
</tr> </tr>
<tr> <tr>
<td>tipFormatter</td> <td>tipFormatter</td>
<td>function</td> <td>function or `null`</td>
<td></td> <td></td>
<td>Format the value of the tooltip if it shows.</td> <td>Format the value of the tooltip if it shows. If `null` the tooltip will always be hidden.</td>
</tr> </tr>
<tr> <tr>
<td>dots</td> <td>dots</td>
+8 -2
View File
@@ -10,7 +10,13 @@ const marks = {
0: '0°C', 0: '0°C',
26: '26°C', 26: '26°C',
37: '37°C', 37: '37°C',
100: '100°C', 50: '50°C',
100: {
style: {
color: 'red',
},
label: '100°C',
},
}; };
function log(value) { function log(value) {
@@ -25,7 +31,7 @@ ReactDOM.render(
</div> </div>
<div style={style}> <div style={style}>
<p>Slider with marks and steps</p> <p>Slider with marks and steps</p>
<Slider min={-10} marks={marks} step={10} onChange={log} defaultValue={20} /> <Slider dots min={-10} marks={marks} step={10} onChange={log} defaultValue={20} />
</div> </div>
<div style={style}> <div style={style}>
+4
View File
@@ -86,6 +86,10 @@ ReactDOM.render(
<p>Basic Slider with `tipFormatter`</p> <p>Basic Slider with `tipFormatter`</p>
<Slider tipFormatter={percentFormatter} tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log} /> <Slider tipFormatter={percentFormatter} tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log} />
</div> </div>
<div style={style}>
<p>Basic Slider without tooltip</p>
<Slider tipFormatter={null} onChange={log} />
</div>
<div style={style}> <div style={style}>
<p>Controlled Slider</p> <p>Controlled Slider</p>
<Slider value={50} /> <Slider value={50} />
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "rc-slider", "name": "rc-slider",
"version": "3.2.0", "version": "3.4.0",
"description": "slider ui component for react", "description": "slider ui component for react",
"keywords": [ "keywords": [
"react", "react",
+1
View File
@@ -5,6 +5,7 @@ function calcPoints(marks, dots, step, min, max) {
const points = Object.keys(marks).map(parseFloat); const points = Object.keys(marks).map(parseFloat);
if (dots) { if (dots) {
for (let i = min; i <= max; i = i + step) { for (let i = min; i <= max; i = i + step) {
if (points.indexOf(i) >= 0) continue;
points.push(i); points.push(i);
} }
} }
+1 -1
View File
@@ -42,7 +42,7 @@ export default class Handle extends React.Component {
prefixCls={className.replace('slider-handle', 'tooltip')} prefixCls={className.replace('slider-handle', 'tooltip')}
placement="top" placement="top"
visible={isTooltipVisible} visible={isTooltipVisible}
overlay={<span>{tipFormatter ? tipFormatter(value) : value}</span>} overlay={<span>{tipFormatter(value)}</span>}
delay={0} delay={0}
transitionName={tipTransitionName}> transitionName={tipTransitionName}>
{handle} {handle}
+8 -3
View File
@@ -8,7 +8,7 @@ const Marks = ({className, marks, included, upperBound, lowerBound, max, min}) =
const markWidth = unit * 0.9; const markWidth = unit * 0.9;
const range = max - min; const range = max - min;
const elements = marksKeys.map(parseFloat).map((point) => { const elements = marksKeys.map(parseFloat).sort((a, b) => a - b).map((point) => {
const isActived = (!included && point === upperBound) || const isActived = (!included && point === upperBound) ||
(included && point <= upperBound && point >= lowerBound); (included && point <= upperBound && point >= lowerBound);
const markClassName = classNames({ const markClassName = classNames({
@@ -19,8 +19,13 @@ const Marks = ({className, marks, included, upperBound, lowerBound, max, min}) =
const style = { width: markWidth + '%' }; const style = { width: markWidth + '%' };
style.left = (point - min) / range * 100 - markWidth / 2 + '%'; style.left = (point - min) / range * 100 - markWidth / 2 + '%';
return (<span className={markClassName} style={style} key={point}> const markPoint = marks[point];
{marks[point]} const markPointIsObject = typeof markPoint === 'object';
const markLabel = markPointIsObject ? markPoint.label : markPoint;
const markStyle = markPointIsObject ?
{ ...style, ...markPoint.style } : style;
return (<span className={markClassName} style={markStyle} key={point}>
{markLabel}
</span>); </span>);
}); });
+10 -9
View File
@@ -34,7 +34,7 @@ class Slider extends React.Component {
const {range, min, max} = props; const {range, min, max} = props;
const initialValue = range ? [min, min] : min; const initialValue = range ? [min, min] : min;
const defaultValue = ('defaultValue' in props ? props.defaultValue : initialValue); const defaultValue = ('defaultValue' in props ? props.defaultValue : initialValue);
const value = ('value' in props ? props.value : defaultValue); const value = (props.value !== undefined ? props.value : defaultValue);
let upperBound; let upperBound;
let lowerBound; let lowerBound;
@@ -81,11 +81,11 @@ class Slider extends React.Component {
lowerBound: nextLowerBound, lowerBound: nextLowerBound,
}); });
if (this.isValueOutOfBounds(upperBound, nextProps) || if (this.isValueOutOfBounds(upperBound, nextProps) ||
this.isValueOutOfBounds(lowerBound, nextProps)) { this.isValueOutOfBounds(lowerBound, nextProps)) {
this.props.onChange([nextLowerBound, nextUpperBound]); this.props.onChange([nextLowerBound, nextUpperBound]);
} }
} else { } else {
const value = 'value' in nextProps ? nextProps.value : upperBound; const value = nextProps.value !== undefined ? nextProps.value : upperBound;
const nextValue = this.trimAlignValue(value, nextProps); const nextValue = this.trimAlignValue(value, nextProps);
if (nextValue === upperBound && lowerBound === nextProps.min) return; if (nextValue === upperBound && lowerBound === nextProps.min) return;
@@ -333,23 +333,23 @@ class Slider extends React.Component {
render() { render() {
const {handle, upperBound, lowerBound} = this.state; const {handle, upperBound, lowerBound} = this.state;
const {className, prefixCls, disabled, dots, included, range, step, const {className, prefixCls, disabled, dots, included, range, step,
marks, max, min, tipTransitionName, tipFormatter, children} = this.props; marks, max, min, tipTransitionName, tipFormatter, children} = this.props;
const upperOffset = this.calcOffset(upperBound); const upperOffset = this.calcOffset(upperBound);
const lowerOffset = this.calcOffset(lowerBound); const lowerOffset = this.calcOffset(lowerBound);
const handleClassName = prefixCls + '-handle'; const handleClassName = prefixCls + '-handle';
const isNoTip = (step === null) && !tipFormatter; const isNoTip = (step === null) || (tipFormatter === null);
const upper = (<Handle className={handleClassName} const upper = (<Handle className={handleClassName}
noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter} noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter}
offset={upperOffset} value={upperBound} dragging={handle === 'upperBound'} />); offset={upperOffset} value={upperBound} dragging={handle === 'upperBound'}/>);
let lower = null; let lower = null;
if (range) { if (range) {
lower = (<Handle className={handleClassName} lower = (<Handle className={handleClassName}
noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter} noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter}
offset={lowerOffset} value={lowerBound} dragging={handle === 'lowerBound'} />); offset={lowerOffset} value={lowerBound} dragging={handle === 'lowerBound'}/>);
} }
const sliderClassName = classNames({ const sliderClassName = classNames({
@@ -368,10 +368,10 @@ class Slider extends React.Component {
offset={lowerOffset} length={upperOffset - lowerOffset}/> offset={lowerOffset} length={upperOffset - lowerOffset}/>
<Dots prefixCls={prefixCls} marks={marks} dots={dots} step={step} <Dots prefixCls={prefixCls} marks={marks} dots={dots} step={step}
included={isIncluded} lowerBound={lowerBound} included={isIncluded} lowerBound={lowerBound}
upperBound={upperBound} max={max} min={min} /> upperBound={upperBound} max={max} min={min}/>
<Marks className={prefixCls + '-mark'} marks={marks} <Marks className={prefixCls + '-mark'} marks={marks}
included={isIncluded} lowerBound={lowerBound} included={isIncluded} lowerBound={lowerBound}
upperBound={upperBound} max={max} min={min} /> upperBound={upperBound} max={max} min={min}/>
{children} {children}
</div> </div>
); );
@@ -417,6 +417,7 @@ Slider.defaultProps = {
onBeforeChange: noop, onBeforeChange: noop,
onChange: noop, onChange: noop,
onAfterChange: noop, onAfterChange: noop,
tipFormatter: value => value,
included: true, included: true,
disabled: false, disabled: false,
dots: false, dots: false,
+51 -35
View File
@@ -22,28 +22,36 @@ describe('rc-slider', function() {
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-track').length).to.be(1); expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-track').length).to.be(1);
}); });
it('should render a Slider with correct value and CSS style', () => { it('should render a Slider with default value correctly', () => {
const sliderWithDefaultValue = ReactDOM.render(<Slider defaultValue={50} />, div); const sliderWithDefaultValue = ReactDOM.render(<Slider defaultValue={50} />, div);
expect(sliderWithDefaultValue.state.upperBound).to.be(50); expect(sliderWithDefaultValue.state.upperBound).to.be(50);
expect(ReactTestUtils expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0] .scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0]
.style.cssText) .style.cssText)
.to.be('left: 50%; '); .to.match(/left: 50%;/);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText)
.to.be('left: 0%; width: 50%; visibility: visible; ');
const trackStyle = ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText;
expect(trackStyle).to.match(/left: 0%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
});
it('should render a Slider with value corrently', () => {
const sliderWithValue = ReactDOM.render(<Slider value={50} />, div); const sliderWithValue = ReactDOM.render(<Slider value={50} />, div);
expect(sliderWithValue.state.upperBound).to.be(50); expect(sliderWithValue.state.upperBound).to.be(50);
expect(ReactTestUtils expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0] .scryRenderedDOMComponentsWithClass(sliderWithValue, 'rc-slider-handle')[0]
.style.cssText) .style.cssText)
.to.be('left: 50%; '); .to.match(/left: 50%;/);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0] const trackStyle = ReactTestUtils
.style.cssText) .scryRenderedDOMComponentsWithClass(sliderWithValue, 'rc-slider-track')[0]
.to.be('left: 0%; width: 50%; visibility: visible; '); .style.cssText;
expect(trackStyle).to.match(/left: 0%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
}); });
it('should render a Range with correct DOM structure', () => { it('should render a Range with correct DOM structure', () => {
@@ -53,38 +61,46 @@ describe('rc-slider', function() {
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(range, 'rc-slider-track').length).to.be(1); expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(range, 'rc-slider-track').length).to.be(1);
}); });
it('should render a Range with correct value and CSS style', () => { it('should render a Range with default value correctly', () => {
const sliderWithDefaultValue = ReactDOM.render(<Slider range defaultValue={[0, 50]} />, div); const rangeWithDefaultValue = ReactDOM.render(<Slider range defaultValue={[0, 50]} />, div);
expect(sliderWithDefaultValue.state.lowerBound).to.be(0); expect(rangeWithDefaultValue.state.lowerBound).to.be(0);
expect(sliderWithDefaultValue.state.upperBound).to.be(50); expect(rangeWithDefaultValue.state.upperBound).to.be(50);
expect(ReactTestUtils expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0] .scryRenderedDOMComponentsWithClass(rangeWithDefaultValue, 'rc-slider-handle')[0]
.style.cssText) .style.cssText)
.to.be('left: 50%; '); .to.match(/left: 50%;/);
expect(ReactTestUtils expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[1] .scryRenderedDOMComponentsWithClass(rangeWithDefaultValue, 'rc-slider-handle')[1]
.style.cssText) .style.cssText)
.to.be('left: 0%; '); .to.match(/left: 0%;/);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText)
.to.be('left: 0%; width: 50%; visibility: visible; ');
const sliderWithValue = ReactDOM.render(<Slider range value={[50, 100]} />, div); const trackStyle = ReactTestUtils
expect(sliderWithValue.state.lowerBound).to.be(50); .scryRenderedDOMComponentsWithClass(rangeWithDefaultValue, 'rc-slider-track')[0]
expect(sliderWithValue.state.upperBound).to.be(100); .style.cssText;
expect(trackStyle).to.match(/left: 0%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
});
it('should render a Range with value correctly', () => {
const rangeWithValue = ReactDOM.render(<Slider range value={[50, 100]} />, div);
expect(rangeWithValue.state.lowerBound).to.be(50);
expect(rangeWithValue.state.upperBound).to.be(100);
expect(ReactTestUtils expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0] .scryRenderedDOMComponentsWithClass(rangeWithValue, 'rc-slider-handle')[0]
.style.cssText) .style.cssText)
.to.be('left: 100%; '); .to.match(/left: 100%;/);
expect(ReactTestUtils expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[1] .scryRenderedDOMComponentsWithClass(rangeWithValue, 'rc-slider-handle')[1]
.style.cssText) .style.cssText)
.to.be('left: 50%; '); .to.match(/left: 50%;/);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0] const trackStyle = ReactTestUtils
.style.cssText) .scryRenderedDOMComponentsWithClass(rangeWithValue, 'rc-slider-track')[0]
.to.be('width: 50%; visibility: visible; left: 50%; '); .style.cssText;
expect(trackStyle).to.match(/left: 50%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
}); });
it('should render dots correctly when `dots=true`', () => { it('should render dots correctly when `dots=true`', () => {