1- import { Component , ReactNode , createElement , ReactElement } from "react" ;
1+ import { Component , ReactNode , createElement , MouseEvent } from "react" ;
22import { observer } from "mobx-react" ;
33import { Tree as ArrayTree } from "array-to-tree" ;
4- import Tree , { AntTreeNode } from "antd/es/tree" ;
5- import Spin from "antd/es/spin" ;
6- import Input from "antd/es/input" ;
7- import Empty from "antd/es/empty" ;
4+ import classNames from "classnames" ;
5+
6+ import { Key } from "antd/es/table/interface" ;
7+ import { EventDataNode , DataNode } from "antd/es/tree" ;
8+ import { Tree , Spin , Input , Empty } from "antd" ;
9+ import { CaretDownFilled } from "@ant-design/icons" ;
10+
811import debounce from "debounce" ;
912
10- const { TreeNode } = Tree ;
1113const { Search } = Input ;
1214
1315import { NodeStore } from "../store/index" ;
1416import { TreeObject } from "../store/objects/entry" ;
15- import { AntTreeNodeDropEvent , AntTreeNodeExpandedEvent } from "antd/es/tree/Tree" ;
1617import { ClickCellType } from "../utils/titlehelper" ;
1718import { Alerts } from "./Alerts" ;
18- import classNames from "classnames" ;
1919
2020export interface TreeViewComponentProps {
2121 store : NodeStore ;
@@ -27,6 +27,7 @@ export interface TreeViewComponentProps {
2727 iconIsGlyphicon : boolean ;
2828 onClickHandler : ( _obj : mendix . lib . MxObject , _clickType : ClickCellType ) => Promise < void > ;
2929 className : string ;
30+ switcherBg : string ;
3031}
3132
3233@observer
@@ -47,6 +48,16 @@ export class TreeViewComponent extends Component<TreeViewComponentProps> {
4748 ) ;
4849 }
4950
51+ componentWillMount ( ) : void {
52+ if ( this . props . switcherBg ) {
53+ document . documentElement . style . setProperty ( "--switcher-icon-bg" , this . props . switcherBg ) ;
54+ }
55+ }
56+
57+ componentWillUnmount ( ) : void {
58+ document . documentElement . style . removeProperty ( "--switcher-icon-bg" ) ;
59+ }
60+
5061 private renderControl ( ) : ReactNode {
5162 const { store, searchEnabled } = this . props ;
5263
@@ -106,19 +117,19 @@ export class TreeViewComponent extends Component<TreeViewComponentProps> {
106117 expandedKeys = { expandedKeys }
107118 showIcon = { showIcon }
108119 showLine = { showLine }
120+ switcherIcon = { showLine ? < CaretDownFilled style = { { backgroundColor : "#F5F8FD" } } /> : undefined }
109121 selectable = { false }
110122 draggable = { draggable }
111123 onDrop = { this . onDrop . bind ( this ) }
112124 onExpand = { this . onExpand . bind ( this ) }
113125 onClick = { this . handleClick ( "single" ) }
114126 onDoubleClick = { this . handleClick ( "double" ) }
115- >
116- { this . renderTreeNodes ( store . entryTree ) }
117- </ Tree >
127+ treeData = { this . getTreeNodes ( store . entryTree ) }
128+ />
118129 ) ;
119130 }
120131
121- private renderTreeNodes ( data : ArrayTree < TreeObject > [ ] ) : ReactElement < any > [ ] {
132+ private getTreeNodes ( data : ArrayTree < TreeObject > [ ] ) : DataNode [ ] {
122133 const { iconIsGlyphicon } = this . props ;
123134 return data . map ( item => {
124135 let icon : ReactNode | boolean = false ;
@@ -133,32 +144,46 @@ export class TreeViewComponent extends Component<TreeViewComponentProps> {
133144 icon = < span className = { iconIsGlyphicon ? "glyphicon glyphicon-" + item . icon : item . icon } /> ;
134145 }
135146
136- if ( item . children && item . children . length > 0 ) {
137- const children = this . renderTreeNodes ( item . children ) ;
138- return (
139- < TreeNode key = { item . guid } title = { item . title } icon = { icon } isLeaf = { isLeaf } className = { extraClass } >
140- { children }
141- </ TreeNode >
142- ) ;
147+ const dataNode : DataNode = {
148+ key : item . guid ,
149+ icon,
150+ title : item . title ,
151+ isLeaf,
152+ className : extraClass
153+ } ;
154+
155+ if ( item . children && item . children . length > 0 && typeof item . children [ 0 ] !== "string" ) {
156+ const children = this . getTreeNodes ( item . children ) ;
157+ dataNode . children = children ;
143158 }
144- return < TreeNode key = { item . guid } icon = { icon } title = { item . title } isLeaf = { isLeaf } className = { extraClass } /> ;
159+
160+ return dataNode ;
145161 } ) ;
146162 }
147163
148- private onDrop ( opts : AntTreeNodeDropEvent ) : void {
149- if ( ! opts . dragNode . props . eventKey ) {
164+ private onDrop ( info : {
165+ event : React . MouseEvent ;
166+ node : EventDataNode ;
167+ dragNode : EventDataNode ;
168+ dragNodesKeys : Key [ ] ;
169+ dropPosition : number ;
170+ dropToGap : boolean ;
171+ } ) : void {
172+ if ( ! info . dragNode . key ) {
150173 return ;
151174 }
152- // console.log(`Dragged : ${opts.dragNode.props.eventKey} to ${opts.node.props.eventKey}`);
153- this . props . store . switchEntryParent ( opts . dragNode . props . eventKey , opts . node . props . eventKey ) ;
175+ this . props . store . switchEntryParent ( info . dragNode . key as string , info . node . key as string ) ;
154176 }
155177
156- private handleClick ( clickType : ClickCellType ) : ( _evt : unknown , _node : AntTreeNode ) => void {
157- return ( _evt : unknown , node : AntTreeNode ) => {
158- if ( ! node . props . eventKey || ! this . props . onClickHandler ) {
178+ private handleClick (
179+ clickType : ClickCellType
180+ ) : ( _evt : MouseEvent < Element , globalThis . MouseEvent > , node : EventDataNode ) => void {
181+ return ( _evt : MouseEvent < Element , globalThis . MouseEvent > , node : EventDataNode ) : void => {
182+ if ( ! node . key || ! this . props . onClickHandler ) {
159183 return ;
160184 }
161- const entryObject = this . props . store . findEntry ( node . props . eventKey ) ;
185+ const key = node . key as string ;
186+ const entryObject = this . props . store . findEntry ( key ) ;
162187 if ( entryObject && entryObject . mxObject ) {
163188 this . props . onClickHandler ( entryObject . mxObject , clickType ) ;
164189 if ( this . props . holdSelection ) {
@@ -168,10 +193,17 @@ export class TreeViewComponent extends Component<TreeViewComponentProps> {
168193 } ;
169194 }
170195
171- private onExpand ( _expandedKeys : string [ ] , info : AntTreeNodeExpandedEvent ) : void {
196+ private onExpand (
197+ _expandedKeys : React . ReactText [ ] ,
198+ info : {
199+ node : EventDataNode ;
200+ expanded : boolean ;
201+ nativeEvent : globalThis . MouseEvent ;
202+ }
203+ ) : void {
172204 const { expanded, node } = info ;
173- if ( node && node . props . eventKey && typeof expanded !== "undefined" ) {
174- this . props . store . expandKey ( node . props . eventKey , expanded ) ;
205+ if ( node && node . key && typeof expanded !== "undefined" ) {
206+ this . props . store . expandKey ( node . key as string , expanded ) ;
175207 }
176208 }
177209}
0 commit comments