@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
203 lines (200 loc) • 10.9 kB
JavaScript
;
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(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 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) {
_inherits(_class, _Component);
var _super = _createSuper(_class);
function _class(props) {
var _this;
_classCallCheck(this, _class);
_this = _super.call(this, props);
_this.handleDown = _this.handleDown.bind(_assertThisInitialized(_this));
_this.handleDrag = _this.handleDrag.bind(_assertThisInitialized(_this));
_this.handleDragStart = _this.handleDragStart.bind(_assertThisInitialized(_this));
_this.bounds = {};
_this.dragConfig = {
global: true
};
return _this;
}
_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
}));
}
}]);
return _class;
}(_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;