UNPKG

baseui

Version:

A React Component library implementing the Base design language

168 lines (166 loc) • 6.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = require("./styled-components"); var _overrides = require("../helpers/overrides"); var _utils = require("./utils"); var _focusVisible = require("../utils/focusVisible"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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 && Object.prototype.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 _extends() { _extends = Object.assign ? Object.assign.bind() : 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); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* Copyright (c) Uber Technologies, Inc. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree. */ class StarRating extends React.Component { constructor(...args) { super(...args); _defineProperty(this, "state", { isFocusVisible: false, previewIndex: undefined }); _defineProperty(this, "selectItem", value => { const { onChange } = this.props; onChange && onChange({ value }); this.setState({ previewIndex: undefined }); }); _defineProperty(this, "updatePreview", previewIndex => { this.setState({ previewIndex }); }); _defineProperty(this, "handleFocus", event => { if ((0, _focusVisible.isFocusVisible)(event)) { this.setState({ isFocusVisible: true }); } }); // eslint-disable-next-line @typescript-eslint/no-unused-vars _defineProperty(this, "handleBlur", event => { if (this.state.isFocusVisible !== false) { this.setState({ isFocusVisible: false }); } }); _defineProperty(this, "renderRatingContents", () => { const { overrides = {}, value = -1, numItems, size = 22, readOnly = false } = this.props; const { previewIndex } = this.state; const [Star, starProps] = (0, _overrides.getOverrides)(overrides.Item, _styledComponents.StyledStar); const ratings = []; // @ts-ignore const refs = [{ current: null }]; for (let x = 1; x <= numItems; x++) { const isFocusable = x === value || value < 1 && x === 1; const starRef = /*#__PURE__*/React.createRef(); // @ts-ignore refs.push(starRef); ratings.push( /*#__PURE__*/ // @ts-ignore React.createElement(Star, _extends({ key: x, role: "radio", title: "rating" // eslint-disable-next-line @typescript-eslint/no-explicit-any , ref: starRef, tabIndex: isFocusable ? '0' : '-1', "aria-setsize": numItems, "aria-checked": x <= value, "aria-posinset": x, "aria-disabled": readOnly, $size: size, $index: x, $isActive: previewIndex !== undefined ? x <= previewIndex : x <= value, $isPartialActive: previewIndex !== undefined ? false : x <= value + 0.5, $isSelected: x === previewIndex, $isFocusVisible: this.state.isFocusVisible && isFocusable, $isReadOnly: readOnly, onClick: () => { if (readOnly) { return; } this.selectItem(x); } // @ts-ignore , onKeyDown: e => { if (readOnly) { return; } if (e.keyCode === _utils.ARROW_UP || e.keyCode === _utils.ARROW_LEFT) { e.preventDefault && e.preventDefault(); const prevIndex = value - 1 < 1 ? numItems : value - 1; this.selectItem(prevIndex); // @ts-ignore refs[prevIndex].current && refs[prevIndex].current.focus(); } if (e.keyCode === _utils.ARROW_DOWN || e.keyCode === _utils.ARROW_RIGHT) { e.preventDefault && e.preventDefault(); const nextIndex = value + 1 > numItems ? 1 : value + 1; this.selectItem(nextIndex); // @ts-ignore refs[nextIndex].current && refs[nextIndex].current.focus(); } }, onMouseOver: () => { if (readOnly) { return; } this.updatePreview(x); } }, starProps, { onFocus: (0, _focusVisible.forkFocus)(starProps, this.handleFocus), onBlur: (0, _focusVisible.forkBlur)(starProps, this.handleBlur) }))); } return ratings; }); } render() { const { overrides = {} } = this.props; const [Root, rootProps] = (0, _overrides.getOverrides)(overrides.Root, _styledComponents.StyledRoot); return /*#__PURE__*/React.createElement(Root, _extends({ "data-baseweb": "star-rating", role: "radiogroup" // @ts-ignore , onBlur: e => { if (!e.currentTarget.contains(e.relatedTarget)) this.updatePreview(undefined); }, onMouseLeave: () => this.updatePreview(undefined) }, rootProps), this.renderRatingContents()); } } _defineProperty(StarRating, "defaultProps", { overrides: {}, numItems: 5, readOnly: false }); var _default = exports.default = StarRating;