react-selectable-box
Version:
A React component used hooks that allows you to select elements in the drag area using the mouse
130 lines (128 loc) • 4.34 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/hooks/useSelectable.ts
var useSelectable_exports = {};
__export(useSelectable_exports, {
default: () => useSelectable
});
module.exports = __toCommonJS(useSelectable_exports);
var import_react = require("react");
var import_context = require("../context");
var import_utils = require("../utils");
var import_useUpdateEffect = __toESM(require("./useUpdateEffect"));
function useSelectable({
value,
disabled,
rule = "collision"
}) {
const {
mode,
scrollContainer,
boxPosition,
isDragging,
value: contextValue = [],
startInside,
startTarget,
selectingValue,
unmountItemsInfo,
virtual,
boxRef,
compareFn
} = (0, import_context.useSelectableContext)();
const node = (0, import_react.useRef)(null);
const [isInRange, setIsInRange] = (0, import_react.useState)(false);
(0, import_react.useEffect)(() => {
var _a;
setIsInRange(
(0, import_utils.checkInRange)(
rule,
(_a = node.current) == null ? void 0 : _a.getBoundingClientRect(),
scrollContainer,
boxPosition,
boxRef
)
);
}, [rule, scrollContainer, boxPosition]);
const isSelected = contextValue.some((i) => compareFn(i, value));
const isSelecting = isDragging && !disabled && isInRange;
const isRemoving = isSelecting && isSelected && (mode === "remove" || mode === "reverse");
const isAdding = isSelecting && !isSelected && (mode === "add" || mode === "reverse");
const setNodeRef = (0, import_react.useCallback)((ref) => {
node.current = ref;
}, []);
(0, import_react.useEffect)(() => {
var _a;
if (startTarget && !startInside.current) {
const contain = (_a = node.current) == null ? void 0 : _a.contains(startTarget);
if (contain) {
startInside.current = true;
}
}
}, [startTarget]);
(0, import_react.useEffect)(() => {
if (selectingValue) {
if (isSelecting) {
selectingValue.current.push(value);
} else {
selectingValue.current = selectingValue.current.filter((i) => !compareFn(i, value));
}
}
}, [isSelecting]);
(0, import_react.useLayoutEffect)(() => {
if (virtual) {
unmountItemsInfo.current.delete(value);
return () => {
if (node.current && scrollContainer) {
unmountItemsInfo.current.set(value, {
rule,
rect: node.current.getBoundingClientRect(),
disabled,
scrollLeft: scrollContainer.scrollLeft,
scrollTop: scrollContainer.scrollTop
});
}
};
}
}, [virtual]);
(0, import_useUpdateEffect.default)(() => {
if (virtual) {
const info = unmountItemsInfo.current.get(value);
if (info) {
unmountItemsInfo.current.set(value, { ...info, disabled, rule });
}
}
}, [virtual, disabled, rule]);
return {
setNodeRef,
isSelecting,
isRemoving,
isSelected,
isAdding,
isDragging
};
}