UNPKG

react-virtualized-sticky-tree

Version:

A React component for efficiently rendering tree like structures with support for position: sticky

55 lines (54 loc) 2.81 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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 (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = __importStar(require("react")); const countries_json_1 = __importDefault(require("./countries.json")); const src_1 = require("../src"); const backgroundColors = ['#45b3e0', '#5bbce4', '#71c5e7', '#87ceeb']; const countryNodes = countries_json_1.default.map((country) => (Object.assign(Object.assign({}, country), { id: country.index }))); const rootNode = countryNodes[0]; const CountriesTree = () => { const rowRenderer = (0, react_1.useCallback)(({ node, style }) => { style = Object.assign(Object.assign({}, style), { backgroundColor: backgroundColors[node.depth] }); return (react_1.default.createElement("div", { className: "node-row", key: node.id, style: style }, node.name)); }, []); const getChildren = (0, react_1.useCallback)((node, nodeInfo) => { var _a; if (node.children) { return (_a = node.children) === null || _a === void 0 ? void 0 : _a.map((childId) => ({ isSticky: !!countryNodes[childId].children, stickyTop: 30 * countryNodes[childId].depth, zIndex: 4 - countryNodes[childId].depth, node: countryNodes[childId], })); } }, []); const root = (0, react_1.useMemo)(() => ({ isSticky: true, stickyTop: 0, zIndex: 4, node: rootNode }), []); return (react_1.default.createElement(src_1.AutoSizedStickyTree, { className: "sticky-tree-wrapper", root: root, renderRoot: true, rowHeight: 30, rowRenderer: rowRenderer, getChildren: getChildren })); }; exports.default = CountriesTree;