@catho/quantum
Version:
Catho react components
123 lines (122 loc) • 7.6 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _theme = require("../shared/theme");
var _shared = require("../shared");
var _helper = require("../Stepper/helper");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } // Generated with scripts/create-component.js
var Wrapper = _styledComponents["default"].div.withConfig({
displayName: "Score__Wrapper",
componentId: "sc-ykmk1t-0"
})(["display:inline-flex;align-items:center;"]);
var Circle = _styledComponents["default"].div.withConfig({
displayName: "Score__Circle",
componentId: "sc-ykmk1t-1"
})(["display:flex;align-items:center;justify-content:center;width:24px;min-width:24px;height:24px;min-height:24px;", " border-radius:50%;", ""], function (_ref) {
var _ref$degrees = _ref.degrees,
reference = _ref$degrees[0],
position = _ref$degrees[1],
skin = _ref.skin,
_ref$theme$colors = _ref.theme.colors,
neutralColor = _ref$theme$colors.neutral[300],
color = _ref$theme$colors[skin][900];
return reference > 0 ? (0, _styledComponents.css)(["background-image:linear-gradient( ", "deg,", " 50%,transparent 50% ),linear-gradient(", "deg,", " 50%,", " 50%);"], reference, neutralColor, position, neutralColor, color) : (0, _styledComponents.css)(["background-image:linear-gradient( ", "deg,", " 50%,transparent 50% ),linear-gradient(", "deg,", " 50%,", " 50%);"], reference, color, position, color, neutralColor);
}, (0, _shared.shadow)(2));
var CircleOverlay = _styledComponents["default"].div.withConfig({
displayName: "Score__CircleOverlay",
componentId: "sc-ykmk1t-2"
})(["width:16px;height:16px;border-radius:50%;background-color:", ";"], function (_ref2) {
var neutralColor = _ref2.theme.colors.neutral[0];
return neutralColor;
});
var ProgressWrapper = _styledComponents["default"].span.withConfig({
displayName: "Score__ProgressWrapper",
componentId: "sc-ykmk1t-3"
})(["margin-left:", ";", ""], function (_ref3) {
var small = _ref3.theme.spacing.small;
return "".concat(small, "px");
}, function (_ref4) {
var _ref4$theme = _ref4.theme,
baseFontSize = _ref4$theme.baseFontSize,
primaryColor = _ref4$theme.colors.primary[900];
return "\n color: ".concat(primaryColor, ";\n font-size: ").concat(baseFontSize, "px;\n ");
});
var formatInPercentage = function formatInPercentage(number) {
return number.toLocaleString('pt-br', {
style: 'percent',
maximumFractionDigits: 2
});
};
var Score = function Score(_ref5) {
var _ref5$percentage = _ref5.percentage,
percentage = _ref5$percentage === void 0 ? 0 : _ref5$percentage,
_ref5$round = _ref5.round,
round = _ref5$round === void 0 ? true : _ref5$round,
_ref5$text = _ref5.text,
text = _ref5$text === void 0 ? 'compatível' : _ref5$text,
_ref5$skin = _ref5.skin,
skin = _ref5$skin === void 0 ? 'primary' : _ref5$skin,
_ref5$theme = _ref5.theme,
theme = _ref5$theme === void 0 ? {
colors: _theme.colors,
baseFontSize: _theme.baseFontSize,
spacing: _theme.spacing
} : _ref5$theme;
var _useState = (0, _react.useState)(formatInPercentage(0)),
_useState2 = _slicedToArray(_useState, 2),
compatibilityPercentage = _useState2[0],
setCompatibilityPercentage = _useState2[1];
var isPercentageValid = function isPercentageValid(percentageValue) {
return percentageValue >= 0 && percentageValue <= 100;
};
(0, _react.useEffect)(function () {
if (isPercentageValid(percentage)) {
var percentageValue = percentage;
if (round) {
percentageValue = Math.round(percentageValue);
}
var formattedPercentageValue = formatInPercentage(percentageValue / 100);
setCompatibilityPercentage(formattedPercentageValue);
}
}, [percentage]);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Wrapper, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Circle, {
degrees: (0, _helper.percentToDegrees)(percentage),
theme: theme,
skin: skin,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(CircleOverlay, {
theme: theme
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(ProgressWrapper, {
theme: theme,
children: "".concat(compatibilityPercentage, " ").concat(text)
})]
});
};
Score.propTypes = {
percentage: _propTypes["default"].number,
text: _propTypes["default"].string,
round: _propTypes["default"].bool,
skin: _propTypes["default"].oneOf(['primary', 'secondary', 'success', 'warning', 'error', 'neutral']),
theme: _propTypes["default"].shape({
colors: _propTypes["default"].object,
baseFontSize: _propTypes["default"].number,
spacing: _propTypes["default"].object
})
};
var _default = exports["default"] = Score;