@@ -4,6 +4,7 @@ import arrayToTree, { Tree } from "array-to-tree";
44import { ValidationMessage , getObject } from "@jeltemx/mendix-react-widget-utils" ;
55import { TreeColumnProps , getTreeTableColumns , TableRecord } from "../util/columns" ;
66import { RowObject , TreeRowObject } from "./objects/row" ;
7+ import { SelectionMode } from "../../typings/MxTreeTableProps" ;
78
89configure ( { enforceActions : "observed" } ) ;
910
@@ -52,6 +53,7 @@ export interface NodeStoreConstructorOptions {
5253 expandFirstLevel : boolean ;
5354 resetState : boolean ;
5455 rowObjectMxProperties : RowObjectMxProperties ;
56+ selectionMode : SelectionMode ;
5557
5658 childLoader : ( guids : string [ ] , parentKey : string , loadFromRef : boolean ) => Promise < void > ;
5759 convertMxObjectToRow : ( mxObject : mendix . lib . MxObject , parentKey ?: string | null ) => Promise < TreeRowObject > ;
@@ -89,6 +91,7 @@ export class NodeStore {
8991 private writeTableState : ( state : TableState ) => void ;
9092 private onSelect : ( ids : string [ ] ) => void ;
9193
94+ private selectionMode : SelectionMode ;
9295 private dataResetOnContextChange : boolean ;
9396 private needToCalculateInitialParents : boolean ;
9497 private needToRestoreStateOnContextChange : boolean ;
@@ -111,6 +114,7 @@ export class NodeStore {
111114 rowObjectMxProperties,
112115 validationMessages,
113116 getInitialTableState,
117+ selectionMode,
114118 onSelect,
115119 writeTableState,
116120 resetState,
@@ -130,6 +134,7 @@ export class NodeStore {
130134 this . needToCalculateInitialParents = calculateInitialParents ;
131135 this . validationMessages = validationMessages ;
132136 this . rowObjectMxProperties = rowObjectMxProperties ;
137+ this . selectionMode = selectionMode ;
133138 this . getInitialTableState = getInitialTableState ;
134139 this . writeTableState = writeTableState ;
135140 this . resetState = resetState ;
@@ -521,13 +526,41 @@ export class NodeStore {
521526 return this . rowObjects . filter ( findRow => findRow . _parent && findRow . _parent === row . key ) . length > 0 ;
522527 }
523528
524- private _setSelectedFromExternal ( guid : string ) : void {
525- const object = this . findRowObject ( guid ) ;
526- if ( object ) {
527- const parents = this . findParents ( object ) ;
528- this . setSelected ( [ object . key ] ) ;
529- this . setExpanded ( parents . map ( p => p . key ) ) ;
530- this . onSelect ( [ object . key ] ) ;
529+ private _setSelectedFromExternal ( guids : string | string [ ] ) : void {
530+ if ( this . selectionMode === "none" ) {
531+ return ;
532+ }
533+
534+ let selected : RowObject [ ] = [ ] ;
535+ if ( Array . isArray ( guids ) ) {
536+ selected = guids . map ( guid => this . findRowObject ( guid ) ) . filter ( obj => obj !== null ) as RowObject [ ] ;
537+ } else {
538+ const object = this . findRowObject ( guids ) ;
539+ if ( object ) {
540+ selected . push ( object ) ;
541+ }
542+ }
543+
544+ if ( selected . length > 0 ) {
545+ if ( this . selectionMode === "single" ) {
546+ selected = [ selected [ 0 ] ] ;
547+ }
548+
549+ const parentGuids : string [ ] = [ ] ;
550+ const selectedGuids = selected . map ( obj => obj . key ) ;
551+
552+ selected . forEach ( obj => {
553+ const parents = this . findParents ( obj ) ;
554+ parents . forEach ( parent => {
555+ if ( parentGuids . indexOf ( parent . key ) === - 1 ) {
556+ parentGuids . push ( parent . key ) ;
557+ }
558+ } ) ;
559+ } ) ;
560+
561+ this . setSelected ( selectedGuids ) ;
562+ this . setExpanded ( parentGuids ) ;
563+ this . onSelect ( selectedGuids ) ;
531564 }
532565 }
533566
0 commit comments