UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

201 lines (198 loc) 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.PositionDecorator = void 0; var _hoc = _interopRequireDefault(require("@enact/core/hoc")); var _handle = require("@enact/core/handle"); var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _validators = require("../internal/validators"); var _utils = require("./utils"); var _SliderModule = _interopRequireDefault(require("./Slider.module.css")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } 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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : 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 _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var validateRange = (0, _validators.validateRangeOnce)(function (props) { return props; }, { 'component': 'PositionDecorator' }); var validateStepValue = (0, _validators.validateSteppedOnce)(function (props) { return props; }, { 'component': 'PositionDecorator' }); var validateStepMax = (0, _validators.validateSteppedOnce)(function (props) { return props; }, { 'component': 'PositionDecorator', valueName: 'max' }); var PositionDecorator = exports.PositionDecorator = (0, _hoc["default"])(function (config, Wrapped) { var _Class; return _Class = /*#__PURE__*/function (_Component) { function _Class(props) { var _this; _classCallCheck(this, _Class); _this = _callSuper(this, _Class, [props]); _this.handleDown = _this.handleDown.bind(_this); _this.handleDrag = _this.handleDrag.bind(_this); _this.handleDragStart = _this.handleDragStart.bind(_this); _this.bounds = {}; _this.dragConfig = { global: true }; return _this; } _inherits(_Class, _Component); return _createClass(_Class, [{ key: "emitChangeForPosition", value: function emitChangeForPosition(x, y) { var _this$props = this.props, max = _this$props.max, min = _this$props.min, orientation = _this$props.orientation, step = _this$props.step; var position = x; var offset = this.bounds.offsetX; if (orientation === 'vertical') { position = y; offset = this.bounds.offsetY; } var proportion = (0, _utils.calcProportion)(this.bounds.min, this.bounds.max, position - offset); if (orientation === 'vertical') { proportion = 1 - proportion; } var value = (max - min) * proportion + min; // adjust value for stepping if (step) { var delta = (value - min) % step; if (delta < step / 2) { value -= delta; } else { value += step - delta; } // recalculate the proportion based on the stepped value proportion = (0, _utils.calcProportion)(min, max, value); } if (value !== this.props.value) { (0, _handle.forward)('onChange', { type: 'onChange', value: value, proportion: proportion }, this.props); } } }, { key: "updateBounds", value: function updateBounds(node) { var orientation = this.props.orientation; var bounds = node.getBoundingClientRect(); var computedStyle = window.getComputedStyle(node); if (orientation === 'horizontal') { this.bounds.min = bounds.left + parseInt(computedStyle.paddingLeft); this.bounds.max = bounds.right - parseInt(computedStyle.paddingRight); } else { this.bounds.min = bounds.top + parseInt(computedStyle.paddingTop); this.bounds.max = bounds.bottom - parseInt(computedStyle.paddingBottom); } } }, { key: "updateOffset", value: function updateOffset(clientX, clientY, target) { this.bounds.offsetX = 0; this.bounds.offsetY = 0; var knob = target.closest(".".concat(_SliderModule["default"].knob)); if (knob) { var rect = knob.getBoundingClientRect(); var centerX = rect.left + rect.width / 2; var centerY = rect.top + rect.height / 2; this.bounds.offsetX = clientX - centerX; this.bounds.offsetY = clientY - centerY; } } }, { key: "handleDown", value: function handleDown(_ref) { var clientX = _ref.clientX, clientY = _ref.clientY, currentTarget = _ref.currentTarget, target = _ref.target; // bail early for emulated mousedown events from key presses if (typeof clientX === 'undefined' || typeof clientY === 'undefined') return; this.updateBounds(currentTarget); this.updateOffset(clientX, clientY, target); this.emitChangeForPosition(clientX, clientY); } }, { key: "handleDragStart", value: function handleDragStart(ev) { (0, _handle.forward)('onDragStart', ev, this.props); this.emitChangeForPosition(ev.x, ev.y); } }, { key: "handleDrag", value: function handleDrag(ev) { (0, _handle.forward)('onDrag', ev, this.props); this.emitChangeForPosition(ev.x, ev.y); } }, { key: "render", value: function render() { if (process.env.NODE_ENV !== "production") { var _this$props2 = this.props, min = _this$props2.min, _this$props2$value = _this$props2.value, value = _this$props2$value === void 0 ? min : _this$props2$value, max = _this$props2.max, step = _this$props2.step; var props = { min: min, value: value || min, max: max, step: step }; validateRange(props); validateStepValue(props); validateStepMax(props); } return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, this.props), {}, { dragConfig: this.dragConfig, onDown: this.handleDown, onDragStart: this.handleDragStart, onDrag: this.handleDrag })); } }]); }(_react.Component), _Class.displayName = 'PositionDecorator', _Class.propTypes = { disabled: _propTypes["default"].bool, max: _propTypes["default"].number, min: _propTypes["default"].number, onChange: _propTypes["default"].func, orientation: _propTypes["default"].string, step: _propTypes["default"].number, value: _propTypes["default"].number }, _Class.defaultProps = { disabled: false, max: 100, min: 0, orientation: 'horizontal', step: 1 }, _Class; }); var _default = exports["default"] = PositionDecorator;