mirror of
https://github.com/wassname/slider.git
synced 2026-09-09 11:35:22 +08:00
Merge pull request #102 from elboman/custom-handle
Allow to pass custom handle components
This commit is contained in:
@@ -130,6 +130,12 @@ ReactDOM.render(<Rcslider />, container);
|
||||
<td></td>
|
||||
<td>Set current positions of handles. If range is `false`, the type of `defaultValue` should be `number`. Otherwise, `[number, number]`</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>handle</td>
|
||||
<td>Component</td>
|
||||
<td></td>
|
||||
<td>Provide a custom Handle to use in the slider by passing a component. This component will have a `value` and `offset` props used to define custom styling/content.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>included</td>
|
||||
<td>boolean</td>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
placeholder
|
||||
@@ -0,0 +1,44 @@
|
||||
require('rc-slider/assets/index.less');
|
||||
|
||||
const React = require('react');
|
||||
const ReactDOM = require('react-dom');
|
||||
const Slider = require('rc-slider');
|
||||
|
||||
const wrapperStyle = {width: 400, margin: 50};
|
||||
|
||||
const handleStyle = {
|
||||
position: 'absolute',
|
||||
transform: 'translate(-50%, -50%)',
|
||||
cursor: 'pointer',
|
||||
padding: '2px',
|
||||
border: '2px solid #abe2fb',
|
||||
borderRadius: '3px',
|
||||
background: '#fff',
|
||||
fontSize: '14px',
|
||||
textAlign: 'center',
|
||||
zIndex: 3,
|
||||
};
|
||||
|
||||
const CustomHandle = props => {
|
||||
const style = Object.assign({left: props.offset + '%'}, handleStyle);
|
||||
return (
|
||||
<div style={style}>val: {props.value}</div>
|
||||
);
|
||||
};
|
||||
CustomHandle.propTypes = {
|
||||
value: React.PropTypes.any,
|
||||
offset: React.PropTypes.number,
|
||||
};
|
||||
|
||||
ReactDOM.render(
|
||||
<div>
|
||||
<div style={wrapperStyle}>
|
||||
<p>Default slider</p>
|
||||
<Slider min={0} max={20} defaultValue={3} />
|
||||
</div>
|
||||
<div style={wrapperStyle}>
|
||||
<p>Slider with custom handle</p>
|
||||
<Slider min={0} max={20} defaultValue={3} handle={<CustomHandle />} />
|
||||
</div>
|
||||
</div>
|
||||
, document.getElementById('__react-content'));
|
||||
+12
-8
@@ -1,8 +1,8 @@
|
||||
import React from 'react';
|
||||
import React, { cloneElement } from 'react';
|
||||
import {Dom as DomUtils} from 'rc-util';
|
||||
import classNames from 'classnames';
|
||||
import Track from './Track';
|
||||
import Handle from './Handle';
|
||||
import DefaultHandle from './Handle';
|
||||
import Steps from './Steps';
|
||||
import Marks from './Marks';
|
||||
|
||||
@@ -330,21 +330,23 @@ class Slider extends React.Component {
|
||||
const {className, prefixCls, disabled, vertical, dots, included, range, step,
|
||||
marks, max, min, tipTransitionName, tipFormatter, children} = this.props;
|
||||
|
||||
const customHandle = this.props.handle;
|
||||
|
||||
const upperOffset = this.calcOffset(upperBound);
|
||||
const lowerOffset = this.calcOffset(lowerBound);
|
||||
|
||||
const handleClassName = prefixCls + '-handle';
|
||||
const isNoTip = (step === null) || (tipFormatter === null);
|
||||
|
||||
const upper = (<Handle className={handleClassName}
|
||||
noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter}
|
||||
vertical = {vertical} offset={upperOffset} value={upperBound} dragging={handle === 'upperBound'}/>);
|
||||
const upper = cloneElement(customHandle, { className: handleClassName,
|
||||
noTip: isNoTip, tipTransitionName: tipTransitionName, tipFormatter: tipFormatter,
|
||||
vertical: vertical, offset: upperOffset, value: upperBound, dragging: handle === 'upperBound' });
|
||||
|
||||
let lower = null;
|
||||
if (range) {
|
||||
lower = (<Handle className={handleClassName}
|
||||
noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter}
|
||||
vertical = {vertical} offset={lowerOffset} value={lowerBound} dragging={handle === 'lowerBound'}/>);
|
||||
lower = cloneElement(customHandle, { className: handleClassName,
|
||||
noTip: isNoTip, tipTransitionName: tipTransitionName, tipFormatter: tipFormatter,
|
||||
vertical: vertical, offset: lowerOffset, value: lowerBound, dragging: handle === 'lowerBound' });
|
||||
}
|
||||
|
||||
const sliderClassName = classNames({
|
||||
@@ -395,6 +397,7 @@ Slider.propTypes = {
|
||||
onBeforeChange: React.PropTypes.func,
|
||||
onChange: React.PropTypes.func,
|
||||
onAfterChange: React.PropTypes.func,
|
||||
handle: React.PropTypes.element,
|
||||
tipTransitionName: React.PropTypes.string,
|
||||
tipFormatter: React.PropTypes.func,
|
||||
dots: React.PropTypes.bool,
|
||||
@@ -411,6 +414,7 @@ Slider.defaultProps = {
|
||||
max: 100,
|
||||
step: 1,
|
||||
marks: {},
|
||||
handle: <DefaultHandle />,
|
||||
onBeforeChange: noop,
|
||||
onChange: noop,
|
||||
onAfterChange: noop,
|
||||
|
||||
Reference in New Issue
Block a user