Skip to content

Commit 0cc6dc3

Browse files
committed
fix(compat): remove Object.fromEntries dependency
1 parent 8b6c073 commit 0cc6dc3

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/DoubleTree.stories.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react';
55
import { Tree, TreeContainer, TreeNode, TreeState, useTreeNodeController } from 'index';
66
import { TreeSource } from 'types';
77
import { useTreeController } from 'use-tree-controller';
8+
import { objectFromEntries } from './util';
89

910
/* tslint:disable:no-console */
1011

@@ -81,7 +82,7 @@ const LeftListItem: React.FC<IListItemProps> = React.memo(({ item }) => {
8182
const newExpandedIds: { [k: string]: boolean } = {
8283
...expandedIds,
8384
// Collapse all children at this same depth.
84-
...Object.fromEntries(
85+
...objectFromEntries(
8586
Object.values(rootTree.allNodes) // For all nodes in the tree.
8687
.filter(({ depth }) => depth === item.depth) // At the same level.
8788
.map(({ id }) => [id, false]), // Create a [id]: false element in expandedIds.
@@ -177,8 +178,8 @@ function useSubTreeSource<T>(source: TreeSource<T>, rootId: string | null): Tree
177178
return [];
178179
}
179180
return fullTrail.slice(0, rootItemIndex + 1);
180-
}
181-
}
181+
},
182+
};
182183
}, [source, rootId]);
183184
}
184185

src/use-tree-loader.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
22
import { LoadableArray, RootTree, TreeNode, TreeSource, TreeSourceNode, TreeState } from './types';
3+
import { objectFromEntries } from './util';
34

45
interface StringMap<V> {
56
[k: string]: V;
@@ -63,13 +64,15 @@ export function useTreeLoader<T>(
6364
// Get active trail IDs from active ID.
6465
const activeTrailIds = useMemo(
6566
() => (activeId && trails[activeId]) ? trails[activeId].map((node) => node.id) : []
66-
, [activeId, trails]);
67+
, [activeId, trails]);
6768

6869
// Add new trails and their sub trails.
6970
const addTrails = useCallback((newTrails: Array<Array<TreeSourceNode<T>>>) => {
7071
setTrails((currentTrails) => {
71-
const newEntries = newTrails.map((trail) => [trail[0].id, trail]);
72-
return newEntries.length > 0 ? { ...currentTrails, ...Object.fromEntries(newEntries) } : currentTrails;
72+
const newEntries: Array<[string, Array<TreeSourceNode<T>>]> = newTrails.map(
73+
(trail) => [trail[0].id, trail],
74+
);
75+
return newEntries.length > 0 ? { ...currentTrails, ...objectFromEntries(newEntries) } : currentTrails;
7376
});
7477
}, [setTrails]);
7578

@@ -110,8 +113,8 @@ export function useTreeLoader<T>(
110113
}
111114

112115
const enableChildrenLoadingState = () => {
113-
setChildren((currentChildren) => ({
114-
...currentChildren, ...Object.fromEntries(idsToLoad.map((id) => [id, { isLoading: true, items: [] }])),
116+
setChildren((currentChildren) => ({
117+
...currentChildren, ...objectFromEntries(idsToLoad.map((id) => [id, { isLoading: true, items: [] }])),
115118
}));
116119
};
117120
let loadingTransitionTimeout: unknown | null = null;
@@ -125,8 +128,10 @@ export function useTreeLoader<T>(
125128

126129
// Load them from the source.
127130
Promise.all(
128-
idsToLoad.map((id) => source.children(id).then((items) => [id, { loading: false, items }])),
129-
).then((results) => {
131+
idsToLoad.map(
132+
(id) => source.children(id).then((items) => [id, { isLoading: false, items }]),
133+
) as Array<Promise<[string, LoadableArray<TreeSourceNode<T>>]>>,
134+
).then((results: Array<[string, LoadableArray<TreeSourceNode<T>>]>) => {
130135
if (loadingTransitionTimeout !== null) {
131136
clearTimeout(loadingTransitionTimeout as any);
132137
}
@@ -137,7 +142,7 @@ export function useTreeLoader<T>(
137142
}
138143

139144
// Add the children to state.
140-
const loadedChildren: StringMap<LoadableArray<TreeSourceNode<T>>> = Object.fromEntries(results);
145+
const loadedChildren: StringMap<LoadableArray<TreeSourceNode<T>>> = objectFromEntries(results);
141146
setChildren((currentChildren) => ({ ...currentChildren, ...loadedChildren }));
142147

143148
// Add trails for the new children so we can make them active.
@@ -148,7 +153,7 @@ export function useTreeLoader<T>(
148153
}, [expandedIds, children, trails, activeTrailIds, source, addTrails, setChildren, loadingTransitionMs]);
149154

150155
return useMemo(() => {
151-
const activeTrailIdsIndex = Object.fromEntries(activeTrailIds.map((id) => [id, true]));
156+
const activeTrailIdsIndex = objectFromEntries(activeTrailIds.map((id) => [id, true]));
152157
const expandedIdsIndex = expandedIds || {};
153158

154159
function buildOutputNode(node: TreeSourceNode<T>, depth: number): TreeNode<T> {

src/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function objectFromEntries<T = any>(entries: Array<[string | number, T]>): { [k: string]: T } {
2+
return entries.reduce<{ [k: string]: T }>((obj, [k, v]) => { obj[k] = v; return obj; }, {});
3+
}

0 commit comments

Comments
 (0)