UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

219 lines (217 loc) 12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.SliderBehaviorDecorator = void 0; var _handle = require("@enact/core/handle"); var _hoc = _interopRequireDefault(require("@enact/core/hoc")); var _Pause = _interopRequireDefault(require("@enact/spotlight/Pause")); var _IString = _interopRequireDefault(require("ilib/lib/IString")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = require("react"); var _$L = _interopRequireDefault(require("../internal/$L")); var _utils = require("./utils"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 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); } function _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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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 _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var toggleActive = function toggleActive(_ref) { var active = _ref.active; return { active: !active }; }; var defaultConfig = { // FIXME: This is a compromise to maintain a single decorator for Slider and IncrementSlider // that handles both a consolidated focus state and spotlight directional event mgmt. When this // is unset (for Slider), this decorator will listen to onKeyDown and fire spotlight events. // When set (for IncrementSlider), it specifies the event that is passed down to trigger // spotlight events and also doesn't remove the spotlight directional callbacks from the props // so the Wrapped component can fire them manually or use the callback for the default behavior. emitSpotlightEvents: null }; // Adds sandstone-specific slider behaviors // * aria-valuetext handling // * use aria-valuetext when set // * defaults to current value // * onActivate, set to hint text // * on value change, reset to value or aria-valuetext // * Spotlight // * Pause Spotlight when dragging to prevent spotlight from leaving when pointer enters another // component // * Forward directional spotlight events from slider // * Managing focused state to show/hide tooltip var SliderBehaviorDecorator = exports.SliderBehaviorDecorator = (0, _hoc["default"])(defaultConfig, function (config, Wrapped) { var _class; var emitSpotlightEvents = config.emitSpotlightEvents; return _class = /*#__PURE__*/function (_Component) { _inherits(_class, _Component); var _super = _createSuper(_class); function _class(props) { var _this; _classCallCheck(this, _class); _this = _super.call(this, props); _this.paused = new _Pause["default"](); _this.handleActivate = _this.handleActivate.bind(_assertThisInitialized(_this)); _this.handleBlur = _this.handleBlur.bind(_assertThisInitialized(_this)); _this.handleDragEnd = _this.handleDragEnd.bind(_assertThisInitialized(_this)); _this.handleDragStart = _this.handleDragStart.bind(_assertThisInitialized(_this)); _this.handleFocus = _this.handleFocus.bind(_assertThisInitialized(_this)); _this.handleSpotlightEvents = _this.handleSpotlightEvents.bind(_assertThisInitialized(_this)); _this.bounds = {}; _this.state = { active: false, dragging: false, focused: false, useHintText: true, prevValue: props.value }; return _this; } _createClass(_class, [{ key: "componentWillUnmount", value: function componentWillUnmount() { this.paused.resume(); } }, { key: "getValueText", value: function getValueText() { var _this$props = this.props, ariaValueText = _this$props['aria-valuetext'], max = _this$props.max, min = _this$props.min, orientation = _this$props.orientation, _this$props$value = _this$props.value, value = _this$props$value === void 0 ? min : _this$props$value; var useHintText = this.state.useHintText; var valueText = ariaValueText != null ? ariaValueText : value; var verticalHint = "".concat(new _IString["default"]((0, _$L["default"])('From {startValue} to {lastValue}')).format({ startValue: min, lastValue: max }), " ").concat(valueText, " ").concat((0, _$L["default"])('change a value with up down button')); var horizontalHint = "".concat(new _IString["default"]((0, _$L["default"])('From {startValue} to {lastValue}')).format({ startValue: min, lastValue: max }), " ").concat(valueText, " ").concat((0, _$L["default"])('change a value with left right button')); if (useHintText) { return orientation === 'horizontal' ? horizontalHint : verticalHint; } return valueText; } }, { key: "handleActivate", value: function handleActivate() { (0, _handle.forwardCustom)('onActivate')(null, this.props); this.setState(toggleActive); } }, { key: "handleBlur", value: function handleBlur(ev) { (0, _handle.forward)('onBlur', ev, this.props); this.setState({ focused: false, useHintText: true }); } }, { key: "handleDragStart", value: function handleDragStart() { this.paused.pause(); this.setState({ dragging: true }); } }, { key: "handleDragEnd", value: function handleDragEnd() { this.paused.resume(); this.setState({ dragging: false }); } }, { key: "handleFocus", value: function handleFocus(ev) { (0, _handle.forward)('onFocus', ev, this.props); if (!this.props.activateOnSelect) { this.handleActivate(); } this.setState({ focused: true }); } }, { key: "handleSpotlightEvents", value: function handleSpotlightEvents(ev) { if (!emitSpotlightEvents) { (0, _handle.forward)('onKeyDown', ev, this.props); } (0, _utils.forwardSpotlightEvents)(ev, this.props); } }, { key: "render", value: function render() { var props = Object.assign({}, this.props); if (!emitSpotlightEvents) { // Remove spotlight props before hitting spottable since we've handled them uniquely delete props.onSpotlightLeft; delete props.onSpotlightRight; delete props.onSpotlightUp; delete props.onSpotlightDown; props.onKeyDown = this.handleSpotlightEvents; } else { props[emitSpotlightEvents] = this.handleSpotlightEvents; } return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({ role: "slider" }, props), {}, { active: this.state.active, "aria-valuetext": this.getValueText(), focused: this.state.focused || this.state.dragging, onActivate: this.handleActivate, onBlur: this.handleBlur, onDragStart: this.handleDragStart, onDragEnd: this.handleDragEnd, onFocus: this.handleFocus })); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (props.value !== state.prevValue) { return { useHintText: false, prevValue: props.value }; } return null; } }]); return _class; }(_react.Component), _class.displayName = 'SliderBehaviorDecorator', _class.propTypes = { activateOnSelect: _propTypes["default"].bool, 'aria-valuetext': _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]), max: _propTypes["default"].number, min: _propTypes["default"].number, orientation: _propTypes["default"].string, value: _propTypes["default"].number }, _class.defaultProps = { max: 100, min: 0, orientation: 'horizontal' }, _class; }); var _default = exports["default"] = SliderBehaviorDecorator;