UNPKG

office-ui-fabric-react

Version:

Reusable React components for building experiences for Office 365.

82 lines (80 loc) 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = require("react"); var Utilities_1 = require("../../Utilities"); var stylesImport = require("./ColorPicker.scss"); var styles = stylesImport; var ColorSlider = (function (_super) { tslib_1.__extends(ColorSlider, _super); function ColorSlider(props) { var _this = _super.call(this, props) || this; var value = _this.props.value; _this.state = { isAdjusting: false, origin: null, currentValue: value }; return _this; } ColorSlider.prototype.componentWillReceiveProps = function (newProps) { if (newProps && newProps.value) { this.setState({ currentValue: newProps.value }); } }; ColorSlider.prototype.render = function () { var _a = this.props, className = _a.className, minValue = _a.minValue, maxValue = _a.maxValue, overlayStyle = _a.overlayStyle; var _b = this.state, currentValue = _b.currentValue, isAdjusting = _b.isAdjusting; var currentPercentage = 100 * (currentValue - minValue) / (maxValue - minValue); return (React.createElement("div", { ref: 'root', className: Utilities_1.css('ms-ColorPicker-slider', styles.slider, className, { 'is-adjusting': isAdjusting }), onMouseDown: this._onMouseDown }, React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-sliderOverlay', styles.sliderOverlay), style: overlayStyle }), React.createElement("div", { className: Utilities_1.css('ms-ColorPicker-thumb is-slider', styles.thumb, styles.thumbIsSlider), style: { left: currentPercentage + '%' } }))); }; ColorSlider.prototype._onMouseDown = function (ev) { this._events.on(window, 'mousemove', this._onMouseMove, true); this._events.on(window, 'mouseup', this._onMouseUp, true); this._onMouseMove(ev); }; ColorSlider.prototype._onMouseMove = function (ev) { var _a = this.props, onChanged = _a.onChanged, minValue = _a.minValue, maxValue = _a.maxValue; var rectSize = this.refs.root.getBoundingClientRect(); var currentPercentage = (ev.clientX - rectSize.left) / rectSize.width; var newValue = Math.min(maxValue, Math.max(minValue, currentPercentage * maxValue)); this.setState({ isAdjusting: true, currentValue: newValue }); if (onChanged) { onChanged(newValue); } ev.preventDefault(); ev.stopPropagation(); }; ColorSlider.prototype._onMouseUp = function (ev) { this._events.off(); this.setState({ isAdjusting: false, origin: null }); }; return ColorSlider; }(Utilities_1.BaseComponent)); ColorSlider.defaultProps = { minValue: 0, maxValue: 100, thumbColor: 'inherit', value: 0 }; tslib_1.__decorate([ Utilities_1.autobind ], ColorSlider.prototype, "_onMouseDown", null); tslib_1.__decorate([ Utilities_1.autobind ], ColorSlider.prototype, "_onMouseMove", null); tslib_1.__decorate([ Utilities_1.autobind ], ColorSlider.prototype, "_onMouseUp", null); exports.ColorSlider = ColorSlider; //# sourceMappingURL=ColorSlider.js.map