UNPKG

@nodeject/ui-components

Version:

UI library for non-trivial components

35 lines (34 loc) 1.29 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { produce } from 'immer'; import { arrayToTree } from '../arrayToTree'; /** * Transform a flat array of nodes into a hierarchy structure * Also, as all nodes' props are into a data object, spreads it. * @param treeNodes */ export var convertTreeDataToDataSource = function (treeNodes) { // step 1: add keys to props, and spreads the data into each node var treeDataWithkeys = treeNodes === null || treeNodes === void 0 ? void 0 : treeNodes.map(function (n) { var newNode = produce(n, function (nodeDraft) { return __assign(__assign({}, n), { id: n.data.id, key: n.data.id }); }); return newNode; }); // step 2: returns the tree hierarchy return arrayToTree(treeDataWithkeys, { id: 'id', parentId: 'data.parent', dataField: null, throwChildrenFieldIfEmpty: true, }); };