Skip to content

Commit a989392

Browse files
authored
Merge pull request #1 from JelteMX/feature/selection-indicator
Add Selection indicator
2 parents c06162c + 1134f82 commit a989392

File tree

7 files changed

+46
-4
lines changed

7 files changed

+46
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "treeview",
33
"widgetName": "TreeView",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"description": "TreeView for Mendix",
66
"copyright": "Jelte Lagendijk 2019",
77
"author": "Jelte Lagendijk <jelte.lagendijk@mendix.com>",

src/TreeView.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class TreeView extends Component<TreeViewContainerProps> {
5757
const validationMessages = validateProps(props);
5858

5959
const storeOpts: NodeStoreConstructorOptions = {
60+
holdSelection: props.selectionSelectOnClick,
6061
loadFull,
6162
entryObjectAttributes: {
6263
childRef,
@@ -100,12 +101,13 @@ class TreeView extends Component<TreeViewContainerProps> {
100101
}
101102

102103
render(): ReactNode {
103-
const { dragIsDraggable, uiNodeIconIsGlyphicon, uiNodeIconAttr } = this.props;
104+
const { dragIsDraggable, uiNodeIconIsGlyphicon, uiNodeIconAttr, selectionSelectOnClick } = this.props;
104105
const showIcon = uiNodeIconAttr !== "";
105106
return (
106107
<TreeViewComponent
107108
className={this.props.class}
108109
searchEnabled={this.searchEnabled}
110+
holdSelection={selectionSelectOnClick}
109111
store={this.store}
110112
showIcon={showIcon}
111113
iconIsGlyphicon={uiNodeIconIsGlyphicon}

src/TreeView.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ Note:
262262
</enumerationValues>
263263
</property>
264264
</propertyGroup>
265+
<propertyGroup caption="Selection">
266+
<property key="selectionSelectOnClick" type="boolean" defaultValue="true">
267+
<caption>Enable selection class</caption>
268+
<description>When clicking a Node, you might want to indicate that this is the selected Node. The widget will add a .selected class to the selected node.</description>
269+
</property>
270+
</propertyGroup>
265271
</propertyGroup>
266272
</properties>
267273
</widget>

src/components/TreeViewComponent.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import classNames from "classnames";
2020
export interface TreeViewComponentProps {
2121
store: NodeStore;
2222
draggable: boolean;
23+
holdSelection: boolean;
2324
searchEnabled: boolean;
2425
showIcon: boolean;
2526
iconIsGlyphicon: boolean;
@@ -120,7 +121,10 @@ export class TreeViewComponent extends Component<TreeViewComponentProps> {
120121
return data.map(item => {
121122
let icon: ReactNode | boolean = false;
122123
const isLeaf = !((item.children && item.children.length > 0) || item.hasChildren);
123-
const extraClass = classNames(item.highlight ? "highlight" : "");
124+
const extraClass = classNames(
125+
item.highlight ? "highlight" : "",
126+
item.selected && this.props.holdSelection ? "selected" : ""
127+
);
124128

125129
if (item.icon) {
126130
icon = <span className={iconIsGlyphicon ? "glyphicon glyphicon-" + item.icon : item.icon} />;
@@ -153,6 +157,9 @@ export class TreeViewComponent extends Component<TreeViewComponentProps> {
153157
const entryObject = this.props.store.findEntry(node.props.eventKey);
154158
if (entryObject && entryObject.mxObject) {
155159
this.props.onClickHandler(entryObject.mxObject, clickType);
160+
if (this.props.holdSelection) {
161+
this.props.store.selectEntry(entryObject.guid);
162+
}
156163
}
157164
};
158165
}

src/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<package xmlns="http://www.mendix.com/package/1.0/">
3-
<clientModule name="TreeView" version="1.0.0" xmlns="http://www.mendix.com/clientModule/1.0/">
3+
<clientModule name="TreeView" version="1.1.0" xmlns="http://www.mendix.com/clientModule/1.0/">
44
<widgetFiles>
55
<widgetFile path="TreeView.xml"/>
66
</widgetFiles>

src/store/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface TreeGuids {
1717
export interface NodeStoreConstructorOptions {
1818
contextObject?: mendix.lib.MxObject;
1919
loadFull: boolean;
20+
holdSelection?: boolean;
2021
subscriptionHandler?: (guids: TreeGuids) => void;
2122
onSelectionChange?: (guids: TreeGuids) => void;
2223
validationMessages: ValidationMessage[];
@@ -61,12 +62,14 @@ export class NodeStore {
6162
@observable public validationMessages: ValidationMessage[] = [];
6263

6364
private loadFull = false;
65+
private holdSelection = false;
6466
private expandedMapping: { [key: string]: string[] } = {};
6567

6668
constructor(opts: NodeStoreConstructorOptions) {
6769
const {
6870
contextObject,
6971
loadFull,
72+
holdSelection,
7073
subscriptionHandler,
7174
onSelectionChange,
7275
validationMessages,
@@ -78,6 +81,7 @@ export class NodeStore {
7881

7982
this.isLoading = false;
8083
this.loadFull = typeof loadFull !== "undefined" ? loadFull : false;
84+
this.holdSelection = typeof holdSelection !== "undefined" ? holdSelection : false;
8185
this.contextObject = contextObject || null;
8286
this.subscriptionHandler = subscriptionHandler || ((): void => {});
8387
this.onSelectionChangeHandler = onSelectionChange || ((): void => {});
@@ -240,6 +244,27 @@ export class NodeStore {
240244
return this.entries.filter(entry => entry.selected).map(entry => entry.guid);
241245
}
242246

247+
@action
248+
selectEntry(guid: string) {
249+
if (!this.holdSelection) {
250+
return;
251+
}
252+
let selectedFound = false;
253+
this.selectedEntries.forEach(entry => {
254+
if (entry.guid !== guid) {
255+
entry.setSelected(false);
256+
} else {
257+
selectedFound = true;
258+
}
259+
});
260+
if (!selectedFound) {
261+
const entry = this.findEntry(guid);
262+
if (entry) {
263+
entry.setSelected(true);
264+
}
265+
}
266+
}
267+
243268
// Expanded
244269

245270
get expandedKeys(): string[] {

typings/TreeViewProps.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export interface TreeViewContainerProps extends CommonProps {
5858
eventNodeOnClickForm: string;
5959
eventNodeOnClickOpenPageAs: OpenPageAs;
6060

61+
selectionSelectOnClick: boolean;
62+
6163
searchEnabled: boolean;
6264
searchHelperEntity: string;
6365
searchStringAttribute: string;

0 commit comments

Comments
 (0)