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

[Slider]: add triggerOnDragging property for determine when to trigger onChange event on component. #1072

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 9 additions & 8 deletions site/docs/en-US/slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ Selecting a range of values is supported.
constructor(props) {
super(props);

this.state = {
value: [4, 8]
}
this.onChange = (value) => {
this.setState({ value });
};
}

render() {
return (
<div className="block">
<Slider value={this.state.value} max={10} range={true} showStops={true} />
<Slider value={this.state.value} onChange={this.onChange} max={10} range showStops />
</div>
)
}
Expand Down Expand Up @@ -165,11 +165,12 @@ render() {
| max | maximum value | number | — | 100 |
| disabled | whether Slider is disabled | boolean | — | false |
| step | step size | number | — | 1 |
| show-input | whether to display an input box, works when `range` is false | boolean | — | false |
| show-input-controls | whether to display control buttons when `show-input` is true | boolean | — | true |
| show-stops | whether to display breakpoints | boolean | — | false |
| show-tooltip | whether to display tooltip value | boolean | — | true |
| showInput | whether to display an input box, works when `range` is false | boolean | — | false |
| showInputControls | whether to display control buttons when `show-input` is true | boolean | — | true |
| showStops | whether to display breakpoints | boolean | — | false |
| showTooltip | whether to display tooltip value | boolean | — | true |
| range | whether to select a range | boolean | — | false |
| triggerOnDragging | trigger `onChange` when mouse dragging | boolean | — | false |

## Events
| Event Name | Description | Parameters |
Expand Down
8 changes: 7 additions & 1 deletion site/docs/zh-CN/slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ constructor(props) {
this.state = {
value: [4, 8]
}

this.onChange = (value) => {
this.setState({ value });
};
}

render() {
return (
<div className="block">
<Slider value={this.state.value} max={10} range={true} showStops={true} />
<Slider value={this.state.value} onChange={this.onChange} max={10} range showStops />
</div>
)
}
Expand Down Expand Up @@ -165,7 +169,9 @@ render() {
| showInput | 是否显示输入框,仅在非范围选择时有效 | boolean | — | false |
| showInputControls | 在显示输入框的情况下,是否显示输入框的控制按钮 | boolean | — | true|
| showStops | 是否显示间断点 | boolean | — | false |
| showTooltip | 是否显示值提示 | boolean | — | true |
| range | 是否为范围选择 | boolean | — | false |
| triggerOnDragging | 是否在拖动过程中触发 `onChange` | boolean | — | false |

### Events
| 事件名称 | 说明 | 回调参数 |
Expand Down
82 changes: 40 additions & 42 deletions src/slider/Button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ type State = {
dragging: boolean,
startX: number,
startY: number,
currentX: number,
currentY: number,
startPosition: number,
newPosition: number
}
Expand All @@ -31,8 +29,6 @@ export default class SliderButton extends Component {
dragging: false,
startX: 0,
startY: 0,
currentX: 0,
currentY: 0,
startPosition: 0,
newPosition: 0
}
Expand All @@ -42,58 +38,59 @@ export default class SliderButton extends Component {
return this.context.component;
}

handleMouseEnter(): void {
handleMouseEnter = (): void => {
this.setState({
hovering: true
});
}

handleMouseLeave(): void {
handleMouseLeave = (): void => {
this.setState({
hovering: false
});
}

onButtonDown(event: SyntheticMouseEvent<any>) {
onDragStart = (event: SyntheticMouseEvent<any>) => {
if (this.disabled()) return;

this.onDragStart(event);
const position = parseInt(this.currentPosition(), 10);

window.addEventListener('mousemove', this.onDragging.bind(this));
window.addEventListener('mouseup', this.onDragEnd.bind(this));
window.addEventListener('contextmenu', this.onDragEnd.bind(this));
}

onDragStart(event: SyntheticMouseEvent<any>) {
this.setState({
dragging: true,
startX: event.clientX,
startY: event.clientY,
startPosition: parseInt(this.currentPosition(), 10)
startPosition: position,
newPosition: position
});
}

onDragging(event: SyntheticMouseEvent<any>) {
const { dragging, startY, currentY, currentX, startX, startPosition, newPosition } = this.state;
window.addEventListener('mousemove', this.onDragging);
window.addEventListener('mouseup', this.onDragEnd);
window.addEventListener('contextmenu', this.onDragEnd);
};

onDragging = (event: SyntheticMouseEvent<any>) => {
const { dragging, startY, startX, startPosition } = this.state;
const { vertical } = this.props;
if (dragging) {
this.setState({
currentX: event.clientX,
currentY: event.clientY,
}, () => {
let diff;
if (vertical) {
diff = (startY - currentY) / this.parent().sliderSize() * 100;
} else {
diff = (currentX - startX) / this.parent().sliderSize() * 100;
}
this.state.newPosition = startPosition + diff;
this.setPosition(newPosition);
});
if (!dragging) {
return;
}
}
const diff = vertical
? (startY - event.clientY)
: (event.clientX - startX);

onDragEnd() {
if (!diff) {
return;
}

const newPosition = startPosition + diff / this.parent().sliderSize() * 100;
this.setState({
newPosition
});

this.setPosition(newPosition);
};

onDragEnd = () => {
const { dragging, newPosition } = this.state;
if (dragging) {
/*
Expand All @@ -108,13 +105,14 @@ export default class SliderButton extends Component {
});
}, 0);

window.removeEventListener('mousemove', this.onDragging.bind(this));
window.removeEventListener('mouseup', this.onDragEnd.bind(this));
window.removeEventListener('contextmenu', this.onDragEnd.bind(this));
window.removeEventListener('mousemove', this.onDragging);
window.removeEventListener('mouseup', this.onDragEnd);
window.removeEventListener('contextmenu', this.onDragEnd);
}
}
};

setPosition(newPosition: number) {
const { onChange } = this.props;
if (newPosition < 0) {
newPosition = 0;
} else if (newPosition > 100) {
Expand All @@ -125,7 +123,7 @@ export default class SliderButton extends Component {
const steps = Math.round(newPosition / lengthPerStep);
const value = steps * lengthPerStep * (this.max() - this.min()) * 0.01 + this.min();

this.props.onChange(parseFloat(value.toFixed(this.precision())));
onChange(parseFloat(value.toFixed(this.precision())));
}

/* Computed Methods */
Expand Down Expand Up @@ -178,9 +176,9 @@ export default class SliderButton extends Component {
'dragging': dragging
})}
style={this.wrapperStyle()}
onMouseEnter={this.handleMouseEnter.bind(this)}
onMouseLeave={this.handleMouseLeave.bind(this)}
onMouseDown={this.onButtonDown.bind(this)}>
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
onMouseDown={this.onDragStart}>
<Tooltip
placement="top"
content={<span>{this.formatValue()}</span>}
Expand Down
Loading