react-toolbox/components/slider/Slider.js

338 lines
9.7 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
2016-05-22 23:35:32 +03:00
import classnames from 'classnames';
2017-01-26 20:05:32 +03:00
import styleShape from 'react-style-proptype';
2016-05-22 23:35:32 +03:00
import { themr } from 'react-css-themr';
Migrate styles to PostCSS (#666) * Add postcss-next postcss-include and reporter * Add stylelint * Add CSS colors * Add CSS custom media queries * Use dashes for CSS colors * Add base CSS variables * Remove AppBar SASS dependency from spec page * Migrate AppBar style to PostCSS * Migrate Avatar style to PostCSS * Migrate Ripple style to PostCSS * Remove unneeded media CSS import in Avatar * Add shadows to CSS variables * Migrate Button style to PostCSS * Update webpack test config and linting from npm * Migrate Input style to PostCSS * Add missing input config variables for Dropdown and Autocomplete * Migrate Chip style to PostCSS * Migrate Autocomplete style to PostCSS * Migrate Dropdown style to PostCSS * Migrate animations to PostCSS * Migrate Card style to PostCSS * Migrate Checkbox style to PostCSS * Migrate DataPicker style to PostCSS * Migrate Dialog style to PostCSS * Migrate Drawer style to PostCSS * Add postcss-mixins and postcss-each * Migrate Layout style to PostCSS * Fix bug in button theme * Bugfix in avatar css * Add some missing nesting notations * Migrate Link style to PostCSS * Migrate List style to PostCSS * Migrate Menu style to PostCSS * Migrate Navigation style to PostCSS * Migrate Overlay style to PostCSS * Migrate ProgressBar style to PostCSS * Migrate Radio style to PostCSS * Migrate Slider style to PostCSS * Migrate Snackbar style to PostCSS * Migrate Switch style to PostCSS * Migrate Table style to PostCSS * Migrate Tabs style to PostCSS * Migrate TimePicker to PostCSS * Migrate Tooltip styles to PostCSS * Update webpack config for testing and tests * Migrate commons to PostCSS * Remove sass from main project * Bye from docs to sass * Build with CSS * Remove unneded deps for sass in docs subproject * Fix tests * use 4p shadow in AppBar as spec indicates * Fixed typo in list/config.css * Fix tests * Fix linter errors * Latest build * Release 2.0.0-beta.0 * Remove sass lint * fixes old sass var in css config * Update linter * New Table implementation * Fix old sass var in list/config.css See da0c47041ecf2d8b118b06fba9f53cc4d7e1e79f. * Remove normalize.css from commons.css * Update dependencies * Latest build * Input ready to accept visible hint * Prepare slider and progress to be disabled * Render Snackbar using Portal * Refactor Dialog, Drawer and Overlay to be used in Layout * Add inner layer to AppBar * New layout * Use Layout in spec * Latest build * remove layout playground example * add smTablet and lgTablet to NavDrawer in Layout readme * add default prop className to Layout, NavDrawer and Sidebar * fix css linter errors in card and slider * Typings for Table component * Add missing Drawer identifier * Update lib build * Adds onQueryChange callback property to Autocomplete The onQueryChange callback is called when the value of the query changes in Autocomplete. It is called with the new query value. * Fix #966 * Fix #965 * Fixes #976 * Updated css-related dependencies Removed usages of `addDependencyTo` since it's deprecated and not needed anymore. See https://github.com/postcss/postcss-import#adddependencyto * Enabled HMR for styles by disabling extracting them to a CSS file * Remove lib from repo * Remove lib * Fixes #1021 * Build using Gulp * Export ThemeProvider from react-css-themr * Add CHANGELOG to release command * Release 2.0.0-beta.1 * Remove immutability helper * Update dependencies * Fix Tooltip trying to render after it's been unmounted * Fixes #1038 * Release 2.0.0-beta.2 * Add ramda, refactor utils and remove slide animation modules * Remove separate slide animations modules * Remove box-sizing reset and body rule * Remove commons.css * Release 2.0.0-beta.4 * Update dependencies * Fixes #1061 * More aggresive guard condition for multiple autocomplete * Fix typeof check in isBrowser function * Fixes an issue when specs are opened with browsers that do not support Object.entries(). * Import from ramda using the 'import XXX from "ramda/src/XXX"' pattern so that bundle sizes will be smaller for not including the whole ramda package. * Fix #1032 * Fix tests * Update yarn.lock * Fixes #1064
2017-01-05 04:42:18 +03:00
import { round, range } from '../utils/utils';
2017-01-26 20:05:32 +03:00
import { SLIDER } from '../identifiers';
import events from '../utils/events';
import InjectProgressBar from '../progress_bar/ProgressBar';
import InjectInput from '../input/Input';
2016-05-28 20:35:00 +03:00
const factory = (ProgressBar, Input) => {
2016-05-30 00:57:34 +03:00
class Slider extends Component {
2016-05-28 20:35:00 +03:00
static propTypes = {
buffer: PropTypes.number,
2016-05-30 00:57:34 +03:00
className: PropTypes.string,
disabled: PropTypes.bool,
2016-05-30 00:57:34 +03:00
editable: PropTypes.bool,
max: PropTypes.number,
min: PropTypes.number,
onChange: PropTypes.func,
onDragStart: PropTypes.func,
onDragStop: PropTypes.func,
2016-05-30 00:57:34 +03:00
pinned: PropTypes.bool,
snaps: PropTypes.bool,
step: PropTypes.number,
2017-01-26 20:05:32 +03:00
style: styleShape,
2016-05-30 00:57:34 +03:00
theme: PropTypes.shape({
container: PropTypes.string,
editable: PropTypes.string,
innerknob: PropTypes.string,
innerprogress: PropTypes.string,
input: PropTypes.string,
knob: PropTypes.string,
pinned: PropTypes.string,
pressed: PropTypes.string,
progress: PropTypes.string,
ring: PropTypes.string,
slider: PropTypes.string,
snap: PropTypes.string,
2017-01-26 20:05:32 +03:00
snaps: PropTypes.string,
2016-05-28 20:35:00 +03:00
}),
2017-01-26 20:05:32 +03:00
value: PropTypes.number,
2016-05-28 20:35:00 +03:00
};
static defaultProps = {
buffer: 0,
2016-05-28 20:35:00 +03:00
className: '',
editable: false,
max: 100,
min: 0,
onDragStart: () => {},
onDragStop: () => {},
2016-05-28 20:35:00 +03:00
pinned: false,
snaps: false,
step: 0.01,
2017-01-26 20:05:32 +03:00
value: 0,
2016-05-28 20:35:00 +03:00
};
state = {
inputFocused: false,
inputValue: null,
sliderLength: 0,
2017-01-26 20:05:32 +03:00
sliderStart: 0,
2016-05-28 20:35:00 +03:00
};
2017-01-26 20:05:32 +03:00
componentDidMount() {
2016-05-28 20:35:00 +03:00
window.addEventListener('resize', this.handleResize);
this.handleResize();
}
2017-01-26 20:05:32 +03:00
componentWillReceiveProps(nextProps) {
2016-05-28 20:35:00 +03:00
if (this.state.inputFocused && this.props.value !== nextProps.value) {
2017-01-26 20:05:32 +03:00
this.setState({ inputValue: this.valueForInput(nextProps.value) });
2016-05-28 20:35:00 +03:00
}
2016-08-02 22:57:00 +03:00
}
2017-01-26 20:05:32 +03:00
shouldComponentUpdate(nextProps, nextState) {
2016-08-02 22:57:00 +03:00
return this.state.inputFocused || !nextState.inputFocused;
2016-05-28 20:35:00 +03:00
}
componentWillUpdate(nextProps, nextState) {
if (nextState.pressed !== this.state.pressed) {
if (nextState.pressed) {
this.props.onDragStart();
} else {
this.props.onDragStop();
}
}
}
2017-01-26 20:05:32 +03:00
componentWillUnmount() {
2016-05-28 20:35:00 +03:00
window.removeEventListener('resize', this.handleResize);
events.removeEventsFromDocument(this.getMouseEventMap());
events.removeEventsFromDocument(this.getTouchEventMap());
events.removeEventsFromDocument(this.getKeyboardEvents());
}
2017-01-26 20:05:32 +03:00
getInput() {
return this.inputNode && this.inputNode.getWrappedInstance
? this.inputNode.getWrappedInstance()
: this.inputNode;
}
getKeyboardEvents() {
return {
keydown: this.handleKeyDown,
};
}
getMouseEventMap() {
return {
mousemove: this.handleMouseMove,
mouseup: this.handleMouseUp,
};
}
getTouchEventMap() {
return {
touchmove: this.handleTouchMove,
touchend: this.handleTouchEnd,
};
}
addToValue(increment) {
let value = this.state.inputFocused ? parseFloat(this.state.inputValue) : this.props.value;
value = this.trimValue(value + increment);
if (value !== this.props.value) this.props.onChange(value);
}
2016-05-28 20:35:00 +03:00
handleInputFocus = () => {
this.setState({
inputFocused: true,
2017-01-26 20:05:32 +03:00
inputValue: this.valueForInput(this.props.value),
2016-05-28 20:35:00 +03:00
});
};
2016-05-28 20:35:00 +03:00
handleInputChange = (value) => {
2017-01-26 20:05:32 +03:00
this.setState({ inputValue: value });
};
2016-05-28 20:35:00 +03:00
handleInputBlur = (event) => {
const value = this.state.inputValue || 0;
2017-01-26 20:05:32 +03:00
this.setState({ inputFocused: false, inputValue: null }, () => {
2016-05-28 20:35:00 +03:00
this.props.onChange(this.trimValue(value), event);
});
};
2016-05-28 20:35:00 +03:00
handleKeyDown = (event) => {
2016-08-02 22:57:00 +03:00
if ([13, 27].indexOf(event.keyCode) !== -1) this.getInput().blur();
2016-05-28 20:35:00 +03:00
if (event.keyCode === 38) this.addToValue(this.props.step);
if (event.keyCode === 40) this.addToValue(-this.props.step);
};
2016-05-28 20:35:00 +03:00
handleMouseDown = (event) => {
2016-08-02 22:57:00 +03:00
if (this.state.inputFocused) this.getInput().blur();
2016-05-28 20:35:00 +03:00
events.addEventsToDocument(this.getMouseEventMap());
this.start(events.getMousePosition(event));
events.pauseEvent(event);
};
2016-05-28 20:35:00 +03:00
handleMouseMove = (event) => {
events.pauseEvent(event);
this.move(events.getMousePosition(event));
};
2016-05-28 20:35:00 +03:00
handleMouseUp = () => {
this.end(this.getMouseEventMap());
};
2016-05-28 20:35:00 +03:00
handleResize = (event, callback) => {
2017-01-26 20:05:32 +03:00
const { left, right } = ReactDOM.findDOMNode(this.progressbarNode).getBoundingClientRect();
2016-05-28 20:35:00 +03:00
const cb = (callback) || (() => {});
2017-01-26 20:05:32 +03:00
this.setState({ sliderStart: left, sliderLength: right - left }, cb);
2016-05-28 20:35:00 +03:00
};
2016-05-28 20:35:00 +03:00
handleSliderBlur = () => {
events.removeEventsFromDocument(this.getKeyboardEvents());
};
2016-05-28 20:35:00 +03:00
handleSliderFocus = () => {
events.addEventsToDocument(this.getKeyboardEvents());
};
2016-05-28 20:35:00 +03:00
handleTouchEnd = () => {
this.end(this.getTouchEventMap());
};
2016-05-28 20:35:00 +03:00
handleTouchMove = (event) => {
this.move(events.getTouchPosition(event));
};
handleTouchStart = (event) => {
2016-08-02 22:57:00 +03:00
if (this.state.inputFocused) this.getInput().blur();
2016-05-28 20:35:00 +03:00
this.start(events.getTouchPosition(event));
events.addEventsToDocument(this.getTouchEventMap());
events.pauseEvent(event);
};
2017-01-26 20:05:32 +03:00
end(revents) {
2016-05-28 20:35:00 +03:00
events.removeEventsFromDocument(revents);
this.setState({ pressed: false });
}
2017-01-26 20:05:32 +03:00
knobOffset() {
const { max, min, value } = this.props;
return 100 * ((value - min) / (max - min));
2016-05-28 20:35:00 +03:00
}
2017-01-26 20:05:32 +03:00
move(position) {
2016-05-28 20:35:00 +03:00
const newValue = this.positionToValue(position);
if (newValue !== this.props.value) this.props.onChange(newValue);
}
2017-01-26 20:05:32 +03:00
positionToValue(position) {
2016-05-28 20:35:00 +03:00
const { sliderStart: start, sliderLength: length } = this.state;
const { max, min, step } = this.props;
2017-01-26 20:05:32 +03:00
const pos = ((position.x - start) / length) * (max - min);
return this.trimValue((Math.round(pos / step) * step) + min);
2016-05-28 20:35:00 +03:00
}
2017-01-26 20:05:32 +03:00
start(position) {
2016-05-28 20:35:00 +03:00
this.handleResize(null, () => {
2017-01-26 20:05:32 +03:00
this.setState({ pressed: true });
2016-05-28 20:35:00 +03:00
this.props.onChange(this.positionToValue(position));
});
}
2017-01-26 20:05:32 +03:00
stepDecimals() {
2016-05-28 20:35:00 +03:00
return (this.props.step.toString().split('.')[1] || []).length;
}
2017-01-26 20:05:32 +03:00
trimValue(value) {
2016-05-28 20:35:00 +03:00
if (value < this.props.min) return this.props.min;
if (value > this.props.max) return this.props.max;
Migrate styles to PostCSS (#666) * Add postcss-next postcss-include and reporter * Add stylelint * Add CSS colors * Add CSS custom media queries * Use dashes for CSS colors * Add base CSS variables * Remove AppBar SASS dependency from spec page * Migrate AppBar style to PostCSS * Migrate Avatar style to PostCSS * Migrate Ripple style to PostCSS * Remove unneeded media CSS import in Avatar * Add shadows to CSS variables * Migrate Button style to PostCSS * Update webpack test config and linting from npm * Migrate Input style to PostCSS * Add missing input config variables for Dropdown and Autocomplete * Migrate Chip style to PostCSS * Migrate Autocomplete style to PostCSS * Migrate Dropdown style to PostCSS * Migrate animations to PostCSS * Migrate Card style to PostCSS * Migrate Checkbox style to PostCSS * Migrate DataPicker style to PostCSS * Migrate Dialog style to PostCSS * Migrate Drawer style to PostCSS * Add postcss-mixins and postcss-each * Migrate Layout style to PostCSS * Fix bug in button theme * Bugfix in avatar css * Add some missing nesting notations * Migrate Link style to PostCSS * Migrate List style to PostCSS * Migrate Menu style to PostCSS * Migrate Navigation style to PostCSS * Migrate Overlay style to PostCSS * Migrate ProgressBar style to PostCSS * Migrate Radio style to PostCSS * Migrate Slider style to PostCSS * Migrate Snackbar style to PostCSS * Migrate Switch style to PostCSS * Migrate Table style to PostCSS * Migrate Tabs style to PostCSS * Migrate TimePicker to PostCSS * Migrate Tooltip styles to PostCSS * Update webpack config for testing and tests * Migrate commons to PostCSS * Remove sass from main project * Bye from docs to sass * Build with CSS * Remove unneded deps for sass in docs subproject * Fix tests * use 4p shadow in AppBar as spec indicates * Fixed typo in list/config.css * Fix tests * Fix linter errors * Latest build * Release 2.0.0-beta.0 * Remove sass lint * fixes old sass var in css config * Update linter * New Table implementation * Fix old sass var in list/config.css See da0c47041ecf2d8b118b06fba9f53cc4d7e1e79f. * Remove normalize.css from commons.css * Update dependencies * Latest build * Input ready to accept visible hint * Prepare slider and progress to be disabled * Render Snackbar using Portal * Refactor Dialog, Drawer and Overlay to be used in Layout * Add inner layer to AppBar * New layout * Use Layout in spec * Latest build * remove layout playground example * add smTablet and lgTablet to NavDrawer in Layout readme * add default prop className to Layout, NavDrawer and Sidebar * fix css linter errors in card and slider * Typings for Table component * Add missing Drawer identifier * Update lib build * Adds onQueryChange callback property to Autocomplete The onQueryChange callback is called when the value of the query changes in Autocomplete. It is called with the new query value. * Fix #966 * Fix #965 * Fixes #976 * Updated css-related dependencies Removed usages of `addDependencyTo` since it's deprecated and not needed anymore. See https://github.com/postcss/postcss-import#adddependencyto * Enabled HMR for styles by disabling extracting them to a CSS file * Remove lib from repo * Remove lib * Fixes #1021 * Build using Gulp * Export ThemeProvider from react-css-themr * Add CHANGELOG to release command * Release 2.0.0-beta.1 * Remove immutability helper * Update dependencies * Fix Tooltip trying to render after it's been unmounted * Fixes #1038 * Release 2.0.0-beta.2 * Add ramda, refactor utils and remove slide animation modules * Remove separate slide animations modules * Remove box-sizing reset and body rule * Remove commons.css * Release 2.0.0-beta.4 * Update dependencies * Fixes #1061 * More aggresive guard condition for multiple autocomplete * Fix typeof check in isBrowser function * Fixes an issue when specs are opened with browsers that do not support Object.entries(). * Import from ramda using the 'import XXX from "ramda/src/XXX"' pattern so that bundle sizes will be smaller for not including the whole ramda package. * Fix #1032 * Fix tests * Update yarn.lock * Fixes #1064
2017-01-05 04:42:18 +03:00
return round(value, this.stepDecimals());
2016-05-28 20:35:00 +03:00
}
2017-01-26 20:05:32 +03:00
valueForInput(value) {
2016-05-28 20:35:00 +03:00
const decimals = this.stepDecimals();
return decimals > 0 ? value.toFixed(decimals) : value.toString();
}
2017-01-26 20:05:32 +03:00
renderSnaps() {
if (!this.props.snaps) return undefined;
return (
<div className={this.props.theme.snaps}>
{range(0, (this.props.max - this.props.min) / this.props.step).map(i =>
<div key={`span-${i}`} className={this.props.theme.snap} />,
)}
</div>
);
}
2017-01-26 20:05:32 +03:00
renderInput() {
if (!this.props.editable) return undefined;
return (
<Input
ref={(node) => { this.inputNode = node; }}
className={this.props.theme.input}
disabled={this.props.disabled}
onFocus={this.handleInputFocus}
onChange={this.handleInputChange}
onBlur={this.handleInputBlur}
value={this.state.inputFocused
? this.state.inputValue
: this.valueForInput(this.props.value)}
/>
);
}
2017-01-26 20:05:32 +03:00
render() {
2016-05-28 20:35:00 +03:00
const { theme } = this.props;
2017-01-26 20:05:32 +03:00
const knobStyles = { left: `${this.knobOffset()}%` };
2016-05-28 20:35:00 +03:00
const className = classnames(theme.slider, {
[theme.editable]: this.props.editable,
[theme.disabled]: this.props.disabled,
2016-05-28 20:35:00 +03:00
[theme.pinned]: this.props.pinned,
[theme.pressed]: this.state.pressed,
2017-01-26 20:05:32 +03:00
[theme.ring]: this.props.value === this.props.min,
2016-05-28 20:35:00 +03:00
}, this.props.className);
return (
<div
2016-05-28 20:35:00 +03:00
className={className}
disabled={this.props.disabled}
2017-01-26 20:05:32 +03:00
data-react-toolbox="slider"
2016-05-28 20:35:00 +03:00
onBlur={this.handleSliderBlur}
onFocus={this.handleSliderFocus}
2017-01-19 23:07:31 +03:00
style={this.props.style}
2017-01-26 20:05:32 +03:00
tabIndex="0"
>
<div
2017-01-26 20:05:32 +03:00
ref={(node) => { this.sliderNode = node; }}
2016-05-28 20:35:00 +03:00
className={theme.container}
onMouseDown={this.handleMouseDown}
onTouchStart={this.handleTouchStart}
2017-01-26 20:05:32 +03:00
>
2016-05-28 20:35:00 +03:00
<div
2017-01-26 20:05:32 +03:00
ref={(node) => { this.knobNode = node; }}
2016-05-28 20:35:00 +03:00
className={theme.knob}
onMouseDown={this.handleMouseDown}
onTouchStart={this.handleTouchStart}
style={knobStyles}
2017-01-26 20:05:32 +03:00
>
<div className={theme.innerknob} data-value={parseInt(this.props.value, 10)} />
2016-05-28 20:35:00 +03:00
</div>
<div className={theme.progress}>
<ProgressBar
disabled={this.props.disabled}
2017-01-26 20:05:32 +03:00
ref={(node) => { this.progressbarNode = node; }}
2016-05-28 20:35:00 +03:00
className={theme.innerprogress}
max={this.props.max}
min={this.props.min}
2017-01-26 20:05:32 +03:00
mode="determinate"
2016-05-28 20:35:00 +03:00
value={this.props.value}
buffer={this.props.buffer}
2017-01-26 20:05:32 +03:00
/>
2016-05-28 20:35:00 +03:00
{this.renderSnaps()}
</div>
</div>
2016-05-28 20:35:00 +03:00
{this.renderInput()}
</div>
2016-05-28 20:35:00 +03:00
);
}
}
2016-05-28 20:35:00 +03:00
return Slider;
};
const Slider = factory(InjectProgressBar, InjectInput);
2016-05-30 00:57:34 +03:00
export default themr(SLIDER)(Slider);
export { factory as sliderFactory };
2016-05-25 01:25:43 +03:00
export { Slider };