wix-style-react
Version:
187 lines (151 loc) • 6.97 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React from 'react';
import PropTypes from 'prop-types';
import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC";
import { st, classes } from './InteractiveModeStar.st.css';
import { dataHooks, starRatingBarSizesInPx } from '../constants';
import StarFilledIcon from 'wix-ui-icons-common/StarFilled';
import StarIcon from 'wix-ui-icons-common/Star';
var InteractiveModeStar = /*#__PURE__*/function (_React$PureComponent) {
_inherits(InteractiveModeStar, _React$PureComponent);
var _super = _createSuper(InteractiveModeStar);
function InteractiveModeStar() {
var _this;
_classCallCheck(this, InteractiveModeStar);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "_onMouseEnter", function () {
var index = _this.props.index;
_this.props.onMouseEnter(index);
});
_defineProperty(_assertThisInitialized(_this), "_onMouseLeave", function () {
_this.props.onMouseLeave();
});
_defineProperty(_assertThisInitialized(_this), "_onClick", function () {
var index = _this.props.index;
_this.props.onClick(index);
});
_defineProperty(_assertThisInitialized(_this), "_onFocus", function () {
var _this$props = _this.props,
focusableOnFocus = _this$props.focusableOnFocus,
index = _this$props.index; // We would like to change the rate caption label when focus / hover
_this.props.handleFocus(index);
focusableOnFocus();
});
_defineProperty(_assertThisInitialized(_this), "_onBlur", function () {
var focusableOnBlur = _this.props.focusableOnBlur; // We would like to change the rate caption label when focus / hover
_this.props.handleBlur();
focusableOnBlur();
});
return _this;
}
_createClass(InteractiveModeStar, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
dataHook = _this$props2.dataHook,
selectedStarIndex = _this$props2.selectedStarIndex,
index = _this$props2.index,
starsRatingBarSize = _this$props2.starsRatingBarSize,
hoveredStarIndex = _this$props2.hoveredStarIndex;
var isStarsHovered = hoveredStarIndex !== 0;
var isCurrentStarHovered = hoveredStarIndex === index; // If the user hovers on a star the value should be compatible to the value of the hovered star
// otherwise the value should be compatible to the selected value.
var isFilledStar = isStarsHovered ? index <= hoveredStarIndex : index <= selectedStarIndex;
var commonProps = {
size: starRatingBarSizesInPx[starsRatingBarSize]
};
return /*#__PURE__*/React.createElement("button", {
role: "radio",
"aria-checked": isFilledStar,
"aria-label": index,
"data-hook": dataHook,
"data-index": index,
className: st(classes.root, this.props.className),
onClick: function onClick() {
return _this2._onClick(index);
},
onMouseEnter: function onMouseEnter() {
return _this2._onMouseEnter(index);
},
onMouseLeave: function onMouseLeave() {
return _this2._onMouseLeave();
},
onFocus: this._onFocus,
onBlur: this._onBlur
}, isFilledStar ? /*#__PURE__*/React.createElement(StarFilledIcon, _extends({
"aria-hidden": true
}, commonProps, {
"data-hook": dataHooks.filledStar,
className: st(classes.star, {
filled: true,
hovered: isCurrentStarHovered
})
})) : /*#__PURE__*/React.createElement(StarIcon, _extends({
"aria-hidden": true
}, commonProps, {
"data-hook": dataHooks.emptyStar,
className: st(classes.star, {
empty: true,
hovered: isCurrentStarHovered
})
})));
}
}]);
return InteractiveModeStar;
}(React.PureComponent);
InteractiveModeStar.displayName = 'InteractiveModeStar';
InteractiveModeStar.propTypes = {
/** Applied as data-hook HTML attribute that can be used in the tests */
dataHook: PropTypes.string,
/** A css class to be applied to the component's root element */
className: PropTypes.string,
/** Specifies the size of the star rating bar. Interactive mode must be 'large'. The default value for the read only mode is 'medium'. */
starsRatingBarSize: PropTypes.oneOf(['large']),
/** The star index. */
index: PropTypes.oneOf([0, 1, 2, 3, 4, 5]).isRequired,
/** The star rating bar’s selected rate. */
selectedStarIndex: PropTypes.oneOf([0, 1, 2, 3, 4, 5]).isRequired,
/** The star rating bar’s hovered star index. */
hoveredStarIndex: PropTypes.oneOf([0, 1, 2, 3, 4, 5]).isRequired,
/** A Handler for clicking on the star
* ##### Signature:
* function(rating: number) => void
* * `rating`: 1 | 2 | 3 | 4 | 5
*/
onClick: PropTypes.func,
/** A Handler for mouse enter
* ##### Signature:
* function(rating: number) => void
* * `rating`: 1 | 2 | 3 | 4 | 5
*/
onMouseEnter: PropTypes.func,
/** A Handler for mouse leave
* ##### Signature:
* function() => void
*/
onMouseLeave: PropTypes.func,
/** A Handler for focus
* ##### Signature:
* function() => void
*/
handleFocus: PropTypes.func,
/** A Handler for blur
* ##### Signature:
* function() => void
*/
handleBlur: PropTypes.func
};
export default withFocusable(InteractiveModeStar);