UNPKG

@blueprintjs/table

Version:

Scalable interactive table component

95 lines 3.79 kB
"use strict"; /* * 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; const react_1 = require("react"); const core_1 = require("@blueprintjs/core"); const dragEvents_1 = require("./dragEvents"); const 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. */ class Draggable extends react_1.PureComponent { static defaultProps = { preventDefault: true, stopPropagation: false, }; events = new dragEvents_1.DragEvents(); targetRef = this.props.targetRef ?? (0, react_1.createRef)(); render() { const onlyChild = react_1.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 (0, react_1.cloneElement)(onlyChild, { ref: this.targetRef }); } componentDidUpdate(prevProps) { const 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); } } componentDidMount() { if (isValidTarget(this.targetRef.current)) { this.events.attach(this.targetRef.current, this.props); } } componentWillUnmount() { if (isValidTarget(this.targetRef.current)) { this.events?.detach(); } } } 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 `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