react-konva-grid
Version:
Canvas grid to render large set of tabular data with virtualization.
68 lines • 3.01 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const defaultTooltipComponent = ({ content, x, y }) => {
const offset = 10;
return (react_1.default.createElement("div", { style: {
position: "absolute",
left: 0,
top: 0,
transform: `translate(${x + offset}px, ${y + offset}px)`,
maxWidth: 200,
background: "rgba(0,0,0,0.8)",
padding: 5,
borderRadius: 5,
color: "white",
} }, content));
};
const getDefaultTooltipComponent = () => defaultTooltipComponent;
const useTooltip = ({ getValue, gridRef, getTooltipComponent = getDefaultTooltipComponent, }) => {
const [activeCell, setActiveCell] = react_1.useState(null);
const [tooltipPosition, setTooltipPosition] = react_1.useState({ x: 0, y: 0 });
const content = react_1.useMemo(() => { var _a; return (activeCell ? (_a = getValue(activeCell)) !== null && _a !== void 0 ? _a : null : null); }, [activeCell]);
const showTooltip = activeCell && content !== null;
const Tooltip = react_1.useMemo(() => getTooltipComponent(activeCell), [activeCell]);
const tooltipComponent = showTooltip ? (react_1.default.createElement(Tooltip, { content: content, x: tooltipPosition.x, y: tooltipPosition.y })) : null;
const handleMouseMove = react_1.useCallback((e) => {
const { rowIndex, columnIndex } = gridRef.current.getCellCoordsFromOffset(e.clientX, e.clientY);
setTooltipPosition({
x: e.clientX,
y: e.clientY,
});
/* Exit if its the same cell */
if (activeCell &&
activeCell.rowIndex === rowIndex &&
activeCell.columnIndex === columnIndex)
return;
setActiveCell({ rowIndex, columnIndex });
}, [activeCell]);
const handleMouseLeave = react_1.useCallback((e) => {
setActiveCell(null);
}, []);
return {
tooltipComponent,
onMouseMove: handleMouseMove,
onMouseLeave: handleMouseLeave,
};
};
exports.default = useTooltip;
//# sourceMappingURL=useTooltip.js.map