Skip to content

Commit 4f904e5

Browse files
committed
Add DeepMap.getCountOfLeafNodesStartingWith method
1 parent 55fec92 commit 4f904e5

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

examples/src/pages/tests/table/treegrid/expand-collapse.page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default function DataTestPage() {
107107
treeExpandState={treeExpandState}
108108
onTreeExpandStateChange={(expandState) => {
109109
console.log('expandState', expandState);
110-
debugger;
110+
// console.log(expandState.collapsedPaths)
111111
setTreeExpandState(expandState);
112112
}}
113113
>

examples/src/pages/tests/table/utils/DeepMap.spec.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ export default test.describe('DeepMap', () => {
645645
['3', '31'],
646646
['1', '10'],
647647
]);
648+
expect(
649+
map.getCountOfLeafNodesStartingWith([], {
650+
excludeSelf: true,
651+
}),
652+
).toEqual(2);
648653

649654
map = new DeepMap<string | number, boolean>();
650655
map.set(['3', '31'], true);
@@ -660,6 +665,11 @@ export default test.describe('DeepMap', () => {
660665
['3', '31'],
661666
['1', '10'],
662667
]);
668+
expect(
669+
map.getCountOfLeafNodesStartingWith([], {
670+
excludeSelf: true,
671+
}),
672+
).toEqual(2);
663673

664674
map = new DeepMap<string | number, boolean>();
665675
map.set(['1', '10'], true);
@@ -673,11 +683,15 @@ export default test.describe('DeepMap', () => {
673683
}),
674684
).toEqual([]);
675685
expect(
676-
map.getKeysForLeafNodesStartingWith(['1', '10'], {
686+
map.getCountOfLeafNodesStartingWith(['1', '10'], {
687+
excludeSelf: true,
688+
}),
689+
).toEqual(0);
690+
expect(
691+
map.getCountOfLeafNodesStartingWith(['1', '10'], {
677692
excludeSelf: false,
678-
respectOrder: true,
679693
}),
680-
).toEqual([['1', '10']]);
694+
).toEqual(1);
681695
});
682696

683697
test('getKeysForLeafNodesStartingWith should work correctly - with depthLimit', () => {
@@ -694,13 +708,25 @@ export default test.describe('DeepMap', () => {
694708
depthLimit: 1,
695709
}),
696710
).toEqual([['4']]);
711+
expect(
712+
map.getCountOfLeafNodesStartingWith([], {
713+
excludeSelf: true,
714+
depthLimit: 1,
715+
}),
716+
).toEqual(1);
697717
expect(
698718
map.getKeysForLeafNodesStartingWith([], {
699719
excludeSelf: true,
700720
depthLimit: 2,
701721
respectOrder: true,
702722
}),
703723
).toEqual([['3', '31'], ['1', '10'], ['4']]);
724+
expect(
725+
map.getCountOfLeafNodesStartingWith([], {
726+
excludeSelf: true,
727+
depthLimit: 2,
728+
}),
729+
).toEqual(3);
704730

705731
map = new DeepMap<string | number, boolean>();
706732
expect(
@@ -712,9 +738,16 @@ export default test.describe('DeepMap', () => {
712738
expect(
713739
map.getKeysForLeafNodesStartingWith([], { excludeSelf: false }),
714740
).toEqual([[]]);
741+
expect(
742+
map.getCountOfLeafNodesStartingWith([], { excludeSelf: false }),
743+
).toEqual(1);
744+
715745
expect(
716746
map.getKeysForLeafNodesStartingWith([], { excludeSelf: true }),
717747
).toEqual([]);
748+
expect(
749+
map.getCountOfLeafNodesStartingWith([], { excludeSelf: true }),
750+
).toEqual(0);
718751
});
719752

720753
test('getLeafNodesStartingWith should work correctly', () => {
@@ -728,6 +761,7 @@ export default test.describe('DeepMap', () => {
728761

729762
const result = map.getLeafNodesStartingWith([], (pair) => pair.keys);
730763
expect(result).toEqual([['1', '10'], ['1', '20'], ['3', '31'], ['4']]);
764+
expect(map.getCountOfLeafNodesStartingWith([])).toEqual(4);
731765
});
732766

733767
test('getLeafNodesStartingWith should work correctly - second scenario', () => {
@@ -757,6 +791,7 @@ export default test.describe('DeepMap', () => {
757791
['3', '31', '311', '3110'],
758792
['3', '31', '311', '3111'],
759793
]);
794+
expect(map.getCountOfLeafNodesStartingWith([])).toEqual(8);
760795
});
761796

762797
test('visit depth first, with index', () => {

source/src/components/DataSource/TreeSelectionState.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,12 @@ export class TreeSelectionState<T = any> {
229229
setConfig(getConfig: GetTreeSelectionStateConfig<T>) {
230230
const config = typeof getConfig === 'function' ? getConfig() : getConfig;
231231

232+
const treePaths = Array.isArray(config.treePaths)
233+
? new DeepMap(config.treePaths.map((path) => [path, true as const]))
234+
: config.treePaths;
235+
232236
this.config = {
233-
treePaths: Array.isArray(config.treePaths)
234-
? new DeepMap(config.treePaths.map((path) => [path, true]))
235-
: config.treePaths,
237+
treePaths,
236238
strictCheckPaths: config.strictCheckPaths ?? true,
237239
};
238240
this.xcache();

source/src/utils/DeepMap/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ export class DeepMap<KeyType, ValueType> {
125125
return result;
126126
}
127127

128+
getCountOfLeafNodesStartingWith(
129+
keys: KeyType[],
130+
options?: StartingWithMethodOptions,
131+
) {
132+
let count = 0;
133+
this.getStartingWith(
134+
keys,
135+
(_keys, _value, pair) => {
136+
if (!pair.map && pair.hasOwnProperty('value')) {
137+
count++;
138+
}
139+
},
140+
options,
141+
);
142+
143+
return count;
144+
}
145+
128146
getKeysForLeafNodesStartingWith(
129147
keys: KeyType[],
130148
options?: StartingWithMethodOptionsWithOrder,

0 commit comments

Comments
 (0)