UNPKG

10.4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _styledComponents = require("./styled-components.js");
11
12var _overrides = require("../helpers/overrides.js");
13
14var _utils = require("./utils.js");
15
16var _focusVisible = require("../utils/focusVisible.js");
17
18function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
19
20function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
22function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
23
24function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
25
26function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
27
28function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
29
30function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
31
32function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
33
34function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
35
36function _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); } }
37
38function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
39
40function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
41
42function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
43
44function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
45
46function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
47
48function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
49
50function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
51
52var StarRating =
53/*#__PURE__*/
54function (_React$Component) {
55 _inherits(StarRating, _React$Component);
56
57 function StarRating() {
58 var _getPrototypeOf2;
59
60 var _this;
61
62 _classCallCheck(this, StarRating);
63
64 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
65 args[_key] = arguments[_key];
66 }
67
68 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(StarRating)).call.apply(_getPrototypeOf2, [this].concat(args)));
69
70 _defineProperty(_assertThisInitialized(_this), "state", {
71 isFocusVisible: false,
72 previewIndex: undefined
73 });
74
75 _defineProperty(_assertThisInitialized(_this), "selectItem", function (value) {
76 var onChange = _this.props.onChange;
77 onChange && onChange({
78 value: value
79 });
80
81 _this.setState({
82 previewIndex: undefined
83 });
84 });
85
86 _defineProperty(_assertThisInitialized(_this), "updatePreview", function (previewIndex) {
87 _this.setState({
88 previewIndex: previewIndex
89 });
90 });
91
92 _defineProperty(_assertThisInitialized(_this), "handleFocus", function (event) {
93 if ((0, _focusVisible.isFocusVisible)(event)) {
94 _this.setState({
95 isFocusVisible: true
96 });
97 }
98 });
99
100 _defineProperty(_assertThisInitialized(_this), "handleBlur", function (event) {
101 if (_this.state.isFocusVisible !== false) {
102 _this.setState({
103 isFocusVisible: false
104 });
105 }
106 });
107
108 _defineProperty(_assertThisInitialized(_this), "renderRatingContents", function () {
109 var _this$props = _this.props,
110 _this$props$overrides = _this$props.overrides,
111 overrides = _this$props$overrides === void 0 ? {} : _this$props$overrides,
112 _this$props$value = _this$props.value,
113 value = _this$props$value === void 0 ? -1 : _this$props$value,
114 numItems = _this$props.numItems,
115 _this$props$size = _this$props.size,
116 size = _this$props$size === void 0 ? 22 : _this$props$size;
117 var previewIndex = _this.state.previewIndex;
118
119 var _getOverrides = (0, _overrides.getOverrides)(overrides.Item, _styledComponents.StyledStar),
120 _getOverrides2 = _slicedToArray(_getOverrides, 2),
121 Star = _getOverrides2[0],
122 starProps = _getOverrides2[1];
123
124 var ratings = [];
125 var refs = [{
126 current: null
127 }];
128
129 var _loop = function _loop(x) {
130 var isFocusable = x === value || value < 1 && x === 1;
131 var starRef = React.createRef();
132 refs.push(starRef);
133 ratings.push(React.createElement(Star, _extends({
134 key: x,
135 role: "radio" // eslint-disable-next-line flowtype/no-weak-types
136 ,
137 ref: starRef,
138 tabIndex: isFocusable ? '0' : '-1',
139 "aria-setsize": numItems,
140 "aria-checked": x <= value,
141 "aria-posinset": x,
142 $size: size,
143 $index: x,
144 $isActive: previewIndex !== undefined ? x <= previewIndex : x <= value,
145 $isSelected: x === previewIndex,
146 $isFocusVisible: _this.state.isFocusVisible && isFocusable,
147 onClick: function onClick() {
148 return _this.selectItem(x);
149 },
150 onKeyDown: function onKeyDown(e) {
151 if (e.keyCode === _utils.ARROW_UP || e.keyCode === _utils.ARROW_LEFT) {
152 e.preventDefault && e.preventDefault();
153 var prevIndex = value - 1 < 1 ? numItems : value - 1;
154
155 _this.selectItem(prevIndex);
156
157 refs[prevIndex].current && refs[prevIndex].current.focus();
158 }
159
160 if (e.keyCode === _utils.ARROW_DOWN || e.keyCode === _utils.ARROW_RIGHT) {
161 e.preventDefault && e.preventDefault();
162 var nextIndex = value + 1 > numItems ? 1 : value + 1;
163
164 _this.selectItem(nextIndex);
165
166 refs[nextIndex].current && refs[nextIndex].current.focus();
167 }
168 },
169 onMouseOver: function onMouseOver() {
170 return _this.updatePreview(x);
171 }
172 }, starProps, {
173 onFocus: (0, _focusVisible.forkFocus)(starProps, _this.handleFocus),
174 onBlur: (0, _focusVisible.forkBlur)(starProps, _this.handleBlur)
175 })));
176 };
177
178 for (var x = 1; x <= numItems; x++) {
179 _loop(x);
180 }
181
182 return ratings;
183 });
184
185 return _this;
186 }
187
188 _createClass(StarRating, [{
189 key: "render",
190 value: function render() {
191 var _this2 = this;
192
193 var _this$props$overrides2 = this.props.overrides,
194 overrides = _this$props$overrides2 === void 0 ? {} : _this$props$overrides2;
195
196 var _getOverrides3 = (0, _overrides.getOverrides)(overrides.Root, _styledComponents.StyledRoot),
197 _getOverrides4 = _slicedToArray(_getOverrides3, 2),
198 Root = _getOverrides4[0],
199 rootProps = _getOverrides4[1];
200
201 return React.createElement(Root, _extends({
202 "data-baseweb": "star-rating",
203 role: "radiogroup",
204 onBlur: function onBlur() {
205 return _this2.updatePreview(undefined);
206 },
207 onMouseLeave: function onMouseLeave() {
208 return _this2.updatePreview(undefined);
209 }
210 }, rootProps), this.renderRatingContents());
211 }
212 }]);
213
214 return StarRating;
215}(React.Component);
216
217_defineProperty(StarRating, "defaultProps", {
218 overrides: {},
219 numItems: 5
220});
221
222var _default = StarRating;
223exports.default = _default;
\No newline at end of file