UNPKG

kitten-components

Version:
162 lines (126 loc) 5.84 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Slider = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _sliderBar = require('kitten/components/sliders/slider-bar'); var _sliderKeyDownHandler = require('kitten/handlers/sliders/slider-key-down-handler'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // TODO move to the "sliders" group (breaking change) // Slider input to choose an integer value between two bounds var Slider = exports.Slider = function (_React$Component) { _inherits(Slider, _React$Component); function Slider(props) { _classCallCheck(this, Slider); var _this = _possibleConstructorReturn(this, (Slider.__proto__ || Object.getPrototypeOf(Slider)).call(this, props)); _this.handleMove = _this.handleMove.bind(_this); _this.handleKeyDown = _sliderKeyDownHandler.sliderKeyDownHandler.bind(_this); return _this; } // Allow other components to focus _createClass(Slider, [{ key: 'focus', value: function focus() { this.refs.focus(); } }, { key: 'handleMove', value: function handleMove(ratio) { var _props = this.props, min = _props.min, max = _props.max; var value = Math.round(ratio * (max - min) + min); this.move(value); } }, { key: 'ratio', value: function ratio() { return this.ratioForValue(this.props.value); } }, { key: 'ratioForValue', value: function ratioForValue(value) { var _props2 = this.props, min = _props2.min, max = _props2.max; return value === null ? min : this.ratioInBounds((value - min) / (max - min)); } }, { key: 'move', value: function move(to) { var value = this.valueInBounds(to); this.props.onChange(value, this.ratioForValue(value)); } }, { key: 'ratioInBounds', value: function ratioInBounds(ratio) { return ratio > 1 ? 1 : ratio < 0 ? 0 : ratio; } }, { key: 'valueInBounds', value: function valueInBounds(value) { var _props3 = this.props, min = _props3.min, max = _props3.max, step = _props3.step; if (value === null) return min < max ? min : max; if (min < max) { if (value < min) return min;else if (value > max) return max; } else { if (value > min) return min;else if (value < max) return max; } return Math.round(value / step) * step; } }, { key: 'render', value: function render() { return _react2.default.createElement(_sliderBar.SliderBar, _extends({ ref: 'contents' }, this.props, this.state, { onMove: this.handleMove, onStart: this.handleStart, onClick: this.handleClick, onAction: this.props.onAction, ratio: this.ratio() })); } }]); return Slider; }(_react2.default.Component); Slider.propTypes = { // Starting value (e.g. 50) initialValue: _propTypes2.default.number, // Minimum possible value (e.g. 0) min: _propTypes2.default.number, // Maximum possible value (e.g. 100) max: _propTypes2.default.number, // Space between each possible value when updated by keyboard (e.g. 1) step: _propTypes2.default.number, // Input name, if needed (e.g. "amount") name: _propTypes2.default.string, // Callback when the value changes (clicked or while dragging) // passes the current value and percentage as an argument // // You should use the given value and pass it back to the Slider props // to re-render the Slider value at the correct position. onChange: _propTypes2.default.func, // Callback when we click, touch or focus onAction: _propTypes2.default.func }; Slider.defaultProps = { value: null, min: 0, max: 100, step: 1, onChange: function onChange() {}, onChangeEnd: function onChangeEnd() {} // DEPRECATED: do not use default export. };exports.default = Slider;