UNPKG

@arche-mc2/arche-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

77 lines 3.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = require("react"); var _ = require("lodash"); var typestyle_1 = require("typestyle"); var classnames = require("classnames"); var theming_1 = require("../../../Common/theming"); var SvgIcon_1 = require("../../Display/SvgIcon"); var eventListener_1 = require("../../../Common/utils/eventListener"); var styles_1 = require("./styles"); exports.getStarFill = function (current, number, rating, max) { var numberByStar = (1 / number) * 100; var valeurMin = (current - 1) * numberByStar; var valeurMax = current * numberByStar; var ratingLevel = Math.ceil((rating / max) * 100); if (ratingLevel >= valeurMax) { return 'full'; } else if (ratingLevel > valeurMin) { return 'half'; } return 'empty'; }; var UpRating = (function (_super) { tslib_1.__extends(UpRating, _super); function UpRating(props, context) { var _this = _super.call(this, props, context) || this; _this.isControlled = function () { return _this.props.value !== undefined; }; _this.getValue = function () { return _this.isControlled() ? _this.props.value : _this.state.value; }; _this.setEditedValue = function (index) { if (!_this.props.disabled) { _this.setState({ editedValue: _this.computeValue(index) }); } }; _this.unsetEditedValue = function () { if (!_this.props.disabled) { _.throttle(function () { return _this.setState({ editedValue: null }); }, 500); } }; _this.validEditedValue = function (index) { if (!_this.props.disabled) { var newValue_1 = _this.computeValue(index); _this.setState({ value: newValue_1 }, function () { if (_this.props.onChange) { _this.props.onChange(eventListener_1.eventFactory(_this.props.name, newValue_1), newValue_1); } }); } }; _this.computeValue = function (index) { return index * Math.floor(_this.props.max / _this.props.numberOfStars); }; _this.state = { editedValue: null, value: props.value, }; return _this; } UpRating.prototype.render = function () { var _this = this; var _a = this.props, className = _a.className, value = _a.value, numberOfStars = _a.numberOfStars, max = _a.max, dataFor = _a.dataFor, theme = _a.theme, onChange = _a.onChange, rest = tslib_1.__rest(_a, ["className", "value", "numberOfStars", "max", "dataFor", "theme", "onChange"]); return (React.createElement("div", tslib_1.__assign({ className: classnames(styles_1.RatingWrapperStyle(this.props), className, 'up-rating'), "data-for": dataFor, "data-tip": "tooltip" }, rest), Array(numberOfStars).fill(0).map(function (element, index) { var fillMode = exports.getStarFill(index + 1, numberOfStars, _this.state.editedValue != null ? _this.state.editedValue : value, max); return (React.createElement("span", { key: index, onMouseOver: _this.setEditedValue.bind(_this, index + 1), onMouseOut: _this.unsetEditedValue.bind(_this, index + 1), onClick: _this.validEditedValue.bind(_this, index + 1) }, React.createElement(SvgIcon_1.default, { iconName: fillMode == 'empty' ? 'star' : 'star-full', key: index, className: classnames('up-star', typestyle_1.style(styles_1.CommonStartCSS(_this.props))) }))); }))); }; UpRating.defaultProps = { theme: theming_1.default, }; return UpRating; }(React.PureComponent)); exports.UpRating = UpRating; ; exports.default = theming_1.withTheme(UpRating); //# sourceMappingURL=UpRating.js.map