UNPKG

react-dipswitch

Version:

An interactive & customizable Dipswitch component for react.

154 lines (124 loc) 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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 _Switch = require("./Switch"); var _Switch2 = _interopRequireDefault(_Switch); 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; } var Dipswitch = function (_Component) { _inherits(Dipswitch, _Component); function Dipswitch(props) { _classCallCheck(this, Dipswitch); var _this = _possibleConstructorReturn(this, (Dipswitch.__proto__ || Object.getPrototypeOf(Dipswitch)).call(this)); _this.onSwitchClick = _this.onSwitchClick.bind(_this); _this.onValueChange = _this.onValueChange.bind(_this); return _this; } _createClass(Dipswitch, [{ key: "onSwitchClick", value: function onSwitchClick(index) { var _this2 = this; return function () { _this2.onValueChange(index); var onClick = _this2.props.onClick; if (onClick) { _this2.props.onClick(index); } }; } }, { key: "onValueChange", value: function onValueChange(index) { var _props = this.props, onValueChange = _props.onValueChange, value = _props.value; if (onValueChange) { var newVal = value ^ Math.pow(2, index); onValueChange(newVal); } } // Used to determine the order of the switches }, { key: "msbRightIterator", value: function msbRightIterator(count, fn) { for (var i = 0; i < count; i++) { fn(i); } } }, { key: "msbLeftIterator", value: function msbLeftIterator(count, fn) { for (var i = count - 1; i >= 0; i--) { fn(i); } } }, { key: "toBool", value: function toBool(val) { return !!val; } }, { key: "render", value: function render() { var _this3 = this; var switches = []; var _props2 = this.props, _props2$switchCount = _props2.switchCount, switchCount = _props2$switchCount === undefined ? 8 : _props2$switchCount, _props2$value = _props2.value, value = _props2$value === undefined ? 0 : _props2$value, _props2$mostSignifica = _props2.mostSignificantBit, mostSignificantBit = _props2$mostSignifica === undefined ? "right" : _props2$mostSignifica, _props2$width = _props2.width, width = _props2$width === undefined ? 128 : _props2$width, switchColor = _props2.switchColor, channelColor = _props2.channelColor, bodyColor = _props2.bodyColor, labelColor = _props2.labelColor; var iterator = mostSignificantBit.toLowerCase() === "right" ? this.msbRightIterator : this.msbLeftIterator; var switchWidth = width / switchCount; iterator(switchCount, function (i) { var switchState = _this3.toBool(Math.pow(2, i) & value); var singleSwitch = _react2.default.createElement(_Switch2.default, { key: i, label: i + 1, onClick: _this3.onSwitchClick(i), on: switchState, width: switchWidth, switchColor: switchColor, channelColor: channelColor, bodyColor: bodyColor, labelColor: labelColor }); switches.push(singleSwitch); }); return _react2.default.createElement( "div", null, switches ); } }]); return Dipswitch; }(_react.Component); Dipswitch.PropTypes = { switchCount: _propTypes2.default.number.isRequired, value: _propTypes2.default.number, width: _propTypes2.default.number, mostSignificantBit: _propTypes2.default.oneOf(["left", "right"]), onValueChange: _propTypes2.default.func, onSwitchClick: _propTypes2.default.func, switchColor: _propTypes2.default.string, channelColor: _propTypes2.default.string, bodyColor: _propTypes2.default.string, labelColor: _propTypes2.default.string }; exports.default = Dipswitch;