tuya-panel-kit
Version:
a functional component library for developing tuya device panels!
141 lines (111 loc) • 5.79 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _jsxFileName = 'src/components/battery/battery.js';
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 _reactNative = require('react-native');
var _reactNativeSvg = require('react-native-svg');
var _reactNativeSvg2 = _interopRequireDefault(_reactNativeSvg);
var _utils = require('../../utils');
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 cx = _utils.RatioUtils.convertX;
var wrapBatteryD = 'M6.5,0 C6.77614237,-5.07265313e-17 7,0.223857625 7,0.5 L7,1 L9.5,1 C10.3284271,1 11,1.67157288 11,2.5 L11,17.5 C11,18.3284271 10.3284271,19 9.5,19 L1.5,19 C0.671572875,19 0,18.3284271 0,17.5 L0,2.5 C0,1.67157288 0.671572875,1 1.5,1 L4,1 L4,0.5 C4,0.223857625 4.22385763,5.07265313e-17 4.5,0 L6.5,0 Z M9.5,2 L1.5,2 C1.22385763,2 1,2.22385763 1,2.5 L1,17.5 C1,17.7761424 1.22385763,18 1.5,18 L9.5,18 C9.77614237,18 10,17.7761424 10,17.5 L10,2.5 C10,2.22385763 9.77614237,2 9.5,2 Z';
var Battery = function (_PureComponent) {
_inherits(Battery, _PureComponent);
function Battery() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Battery);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Battery.__proto__ || Object.getPrototypeOf(Battery)).call.apply(_ref, [this].concat(args))), _this), _this.calcBattery = function () {
var value = _this.props.value;
var top = 17 - (17 - 3) * value / 100;
console.log('[battery]', top);
return top;
}, _this.calcColor = function () {
var _this$props = _this.props,
highColor = _this$props.highColor,
middleColor = _this$props.middleColor,
lowColor = _this$props.lowColor,
onCalcColor = _this$props.onCalcColor;
var top = _this.calcBattery();
var color = typeof onCalcColor === 'function' && onCalcColor(top, highColor, middleColor, lowColor);
if (color) {
return color;
}
if (top <= 14.2 && top >= 3) {
return highColor;
}
if (top <= 15.6 && top > 14.2) {
return middleColor;
}
return lowColor;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Battery, [{
key: 'render',
value: function render() {
var _props = this.props,
batteryColor = _props.batteryColor,
size = _props.size;
var top = this.calcBattery();
var points = '2 ' + top + ' 9 ' + top + ' 9 17 2 17';
var insideColor = this.calcColor();
return _react2.default.createElement(
_reactNative.View,
{ style: { flex: 1, alignItems: 'center', justifyContent: 'center' }, __source: {
fileName: _jsxFileName,
lineNumber: 66
}
},
_react2.default.createElement(
_reactNativeSvg2.default,
{ width: 1.1 * size, height: 1.9 * size, viewBox: '0 0 11 19', __source: {
fileName: _jsxFileName,
lineNumber: 67
}
},
_react2.default.createElement(_reactNativeSvg.Path, { d: wrapBatteryD, fill: batteryColor, __source: {
fileName: _jsxFileName,
lineNumber: 68
}
}),
_react2.default.createElement(_reactNativeSvg.Polygon, { points: points, fill: insideColor, __source: {
fileName: _jsxFileName,
lineNumber: 69
}
})
)
);
}
}]);
return Battery;
}(_react.PureComponent);
Battery.propTypes = {
size: _propTypes2.default.number,
batteryColor: _reactNative.ColorPropType,
value: _propTypes2.default.number,
highColor: _reactNative.ColorPropType,
middleColor: _reactNative.ColorPropType,
lowColor: _reactNative.ColorPropType,
onCalcColor: _propTypes2.default.func
};
Battery.defaultProps = {
size: cx(10),
batteryColor: 'rgba(0,0,0,.5)',
value: 80,
highColor: '#70CF98',
middleColor: '#F5A623',
lowColor: '#FF4444',
onCalcColor: undefined
};
exports.default = Battery;