@blueprintjs/table
Version:
Scalable interactive table component
104 lines • 4.29 kB
JavaScript
;
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Draggable = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var core_1 = require("@blueprintjs/core");
var dragEvents_1 = require("./dragEvents");
var REATTACH_PROPS_KEYS = ["stopPropagation", "preventDefault"];
/**
* This component provides a simple interface for combined drag and/or click events.
*
* Like ResizeSensor, this component expects a single child element so that it can
* clone it and attach a ref to it.
*
* Since the mouse interactions for drag and click are overloaded, here are
* the events that will fire in these cases:
*
* A Click Interaction
* 1. The user presses down on the render element, triggering the onActivate
* callback.
* 2. The user releases the mouse button without moving it, triggering the
* onClick callback.
*
* A Drag Interaction
* 1. The user presses down on the render element, triggering the onActivate
* callback.
* 2. The user moves the mouse, triggering the onDragMove callback.
* 3. The user moves the mouse, triggering the onDragMove callback.
* 4. The user moves the mouse, triggering the onDragMove callback.
* 5. The user releases the mouse button, triggering a final onDragMove
* callback as well as an onDragEnd callback.
*
* If `false` is returned from the onActivate callback, no further events
* will be fired until the next activation.
*/
var Draggable = /** @class */ (function (_super) {
tslib_1.__extends(Draggable, _super);
function Draggable() {
var _a;
var _this = _super.apply(this, arguments) || this;
_this.events = new dragEvents_1.DragEvents();
_this.targetRef = (_a = _this.props.targetRef) !== null && _a !== void 0 ? _a : React.createRef();
return _this;
}
Draggable.prototype.render = function () {
var onlyChild = React.Children.only(this.props.children);
// if we're provided a ref to the child already, we don't need to attach one ourselves
if (this.props.targetRef !== undefined) {
return onlyChild;
}
return React.cloneElement(onlyChild, { ref: this.targetRef });
};
Draggable.prototype.componentDidUpdate = function (prevProps) {
var didEventHandlerPropsChange = !core_1.Utils.shallowCompareKeys(prevProps, this.props, {
include: REATTACH_PROPS_KEYS,
});
if (didEventHandlerPropsChange && isValidTarget(this.targetRef.current)) {
this.events.attach(this.targetRef.current, this.props);
}
};
Draggable.prototype.componentDidMount = function () {
if (isValidTarget(this.targetRef.current)) {
this.events.attach(this.targetRef.current, this.props);
}
};
Draggable.prototype.componentWillUnmount = function () {
var _a;
if (isValidTarget(this.targetRef.current)) {
(_a = this.events) === null || _a === void 0 ? void 0 : _a.detach();
}
};
Draggable.defaultProps = {
preventDefault: true,
stopPropagation: false,
};
return Draggable;
}(React.PureComponent));
exports.Draggable = Draggable;
/**
* Used to ensure that target elements are valid at runtime for use by DragEvents.
* This check is done at runtime instead of compile time because of our use of `React.cloneElement<any>()`
* and the associated untyped `ref` injection.
*
* @see https://github.com/palantir/blueprint/issues/6248
*/
function isValidTarget(refElement) {
return refElement != null && refElement instanceof HTMLElement;
}
//# sourceMappingURL=draggable.js.map