Skip to content

Commit 591b7ef

Browse files
committed
Add configurable strict mode for TreeSelectionState - defaults to true.
Also release version patch
1 parent d515f24 commit 591b7ef

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

examples/src/pages/tests/table/treeview/TreeSelectionState.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,35 @@ export default test.describe.parallel('TreeSelectionState', () => {
175175
['3', '31', '311', '3111'],
176176
]);
177177
});
178+
179+
test('strictCheckPaths should work properly', async () => {
180+
let treeSelectionState = new TreeSelectionState({
181+
defaultSelection: false,
182+
selectedPaths: [
183+
['media', 'videos'],
184+
['media', 'pictures'],
185+
['documents'],
186+
],
187+
});
188+
189+
expect(treeSelectionState.isNodeSelected(['media', 'videos'])).toBe(false);
190+
191+
treeSelectionState = new TreeSelectionState(
192+
{
193+
defaultSelection: false,
194+
selectedPaths: [
195+
['media', 'videos'],
196+
['media', 'pictures'],
197+
['documents'],
198+
],
199+
},
200+
{
201+
treePaths: [],
202+
strictCheckPaths: false,
203+
},
204+
);
205+
206+
expect(treeSelectionState.isNodeSelected(['media', 'videos'])).toBe(true);
207+
expect(treeSelectionState.isNodeSelected(['media', 'other'])).toBe(false);
208+
});
178209
});

source/src/components/DataSource/TreeSelectionState.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@ export type TreeSelectionStateObject =
2121

2222
type TreeSelectionStateConfigObject = {
2323
treePaths: DeepMap<any, true>;
24+
strictCheckPaths: boolean;
2425
};
2526

2627
export type TreeSelectionStateConfigParam<_T = any> = {
27-
treePaths: TreeSelectionStateConfigObject['treePaths'] | [any[]];
28+
treePaths: TreeSelectionStateConfigObject['treePaths'] | any[][];
29+
/**
30+
* Defaults to true. If false, when checking if a given path is selected,
31+
* it will not check if the path is in the treePaths, but will only check
32+
* in the selectionMap.
33+
*
34+
* If true, will check for the path both in the treePaths and the selectionMap.
35+
*/
36+
strictCheckPaths?: boolean;
2837
};
2938
export type GetTreeSelectionStateConfig<T> =
3039
| TreeSelectionStateConfigParam<T>
@@ -210,9 +219,12 @@ export class TreeSelectionState<T = any> {
210219
}),
211220
);
212221

213-
this.disableCorrectnessChecks();
222+
this.disableStrictMode();
214223
this.update(stateObject);
215-
this.enableCorrectnessChecks();
224+
225+
if (this.config.strictCheckPaths) {
226+
this.enableStrictMode();
227+
}
216228
}
217229
setConfig(getConfig: GetTreeSelectionStateConfig<T>) {
218230
const config = typeof getConfig === 'function' ? getConfig() : getConfig;
@@ -221,6 +233,7 @@ export class TreeSelectionState<T = any> {
221233
treePaths: Array.isArray(config.treePaths)
222234
? new DeepMap(config.treePaths.map((path) => [path, true]))
223235
: config.treePaths,
236+
strictCheckPaths: config.strictCheckPaths ?? true,
224237
};
225238
this.xcache();
226239
}
@@ -291,18 +304,18 @@ export class TreeSelectionState<T = any> {
291304
});
292305
}
293306

294-
private correctnessChecks = false;
307+
private strictMode = false;
295308

296-
enableCorrectnessChecks() {
297-
this.correctnessChecks = true;
309+
enableStrictMode() {
310+
this.strictMode = true;
298311
}
299312

300-
disableCorrectnessChecks() {
301-
this.correctnessChecks = false;
313+
disableStrictMode() {
314+
this.strictMode = false;
302315
}
303316

304317
isPathAvailable(nodePath: NodePath) {
305-
if (!this.correctnessChecks) {
318+
if (!this.strictMode) {
306319
return true;
307320
}
308321

@@ -357,7 +370,7 @@ export class TreeSelectionState<T = any> {
357370
})
358371
.sort(shortestToLongest);
359372

360-
if (this.correctnessChecks) {
373+
if (this.strictMode) {
361374
childPaths = childPaths.filter((path) => this.isPathAvailable(path));
362375
}
363376

www/content/docs/releases/index.page.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ title: Releases
33
description: All releases | Infinite Table DataGrid for React
44
---
55

6+
## 7.3.1
7+
8+
Improve TreeSelectionState standalone usage by adding config for strict mode.
9+
610
## 7.3.0
711

812
Fix DataGrid virtualization issues in React 18 and above, caused by batched updates

0 commit comments

Comments
 (0)