Skip to content

Commit d733204

Browse files
authored
[WC-1637] Datagrid selection api (#433)
2 parents 64aff84 + 248755a commit d733204

File tree

28 files changed

+1213
-60
lines changed

28 files changed

+1213
-60
lines changed

packages/modules/data-widgets/src/themesource/datawidgets/web/_datagrid.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ $grid-border-color: #ced0d3 !default;
1515

1616
$brand-primary: #264ae5 !default;
1717
$brand-light: #e6eaff !default;
18+
$grid-selected-row-background: $brand-light;
1819

1920
.table {
2021
position: relative;
@@ -347,3 +348,11 @@ $brand-light: #e6eaff !default;
347348
}
348349
}
349350
}
351+
352+
.widget-datagrid {
353+
&.widget-datagrid-selection-method-click {
354+
.tr.tr-selected .td {
355+
background-color: $grid-selected-row-background;
356+
}
357+
}
358+
}

packages/pluggableWidgets/selection-helper-web/src/ui/CheckBox.scss renamed to packages/modules/data-widgets/src/themesource/datawidgets/web/_three-state-checkbox.scss

File renamed without changes.

packages/modules/data-widgets/src/themesource/datawidgets/web/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
@import "drop-down-sort";
66
@import "gallery";
77
@import "gallery-design-properties";
8+
@import "three-state-checkbox";
89
@import "tree-node";
910
@import "tree-node-design-properties";

packages/pluggableWidgets/datagrid-web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"devDependencies": {
4848
"@mendix-internal/automation-utils": "workspace:*",
4949
"@mendix/eslint-config-web-widgets": "workspace:*",
50+
"@mendix/pluggable-test-utils": "workspace:*",
5051
"@mendix/pluggable-widgets-tools": "^9.23.2",
5152
"@mendix/prettier-config-web-widgets": "workspace:*",
5253
"@testing-library/dom": "^8.20.0",

packages/pluggableWidgets/datagrid-web/src/Datagrid.editorConfig.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export function getProperties(
8282
hidePropertyIn(defaultProperties, values, "filterList");
8383
}
8484

85+
hideSelectionProperties(defaultProperties, values);
86+
8587
changePropertyIn(
8688
defaultProperties,
8789
values,
@@ -140,6 +142,18 @@ export function getProperties(
140142
return defaultProperties;
141143
}
142144

145+
function hideSelectionProperties(defaultProperties: Properties, values: DatagridPreviewProps): void {
146+
const { itemSelection, itemSelectionMethod } = values;
147+
148+
if (itemSelection === "None") {
149+
hidePropertiesIn(defaultProperties, values, ["itemSelectionMethod", "onSelectionChange"]);
150+
}
151+
152+
if (itemSelection !== "Multi" || itemSelectionMethod !== "checkbox") {
153+
hidePropertyIn(defaultProperties, values, "showSelectAllToggle");
154+
}
155+
}
156+
143157
export const getPreview = (
144158
values: DatagridPreviewProps,
145159
isDarkMode: boolean,
@@ -359,6 +373,19 @@ const checkSortingSettings = (
359373
}
360374
};
361375

376+
const checkSelectionSettings = (values: DatagridPreviewProps): Problem[] => {
377+
if (values.itemSelection !== "None" && values.onClick !== null) {
378+
return [
379+
{
380+
property: "onClick",
381+
message: '"On click action" must be set to "Do nothing" when "Selection" is enabled'
382+
}
383+
];
384+
}
385+
386+
return [];
387+
};
388+
362389
export function check(values: DatagridPreviewProps): Problem[] {
363390
const errors: Problem[] = [];
364391

@@ -381,5 +408,7 @@ export function check(values: DatagridPreviewProps): Problem[] {
381408
}
382409
});
383410

411+
errors.push(...checkSelectionSettings(values));
412+
384413
return errors;
385414
}

packages/pluggableWidgets/datagrid-web/src/Datagrid.editorPreview.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Selectable } from "mendix/preview/Selectable";
99
import { ObjectItem, GUID } from "mendix";
1010
import classNames from "classnames";
1111
import { isSortable } from "./features/column";
12+
import { selectionSettings, useOnSelectProps } from "./features/selection";
1213

1314
const dummyColumns: ColumnsPreviewType[] = [
1415
{
@@ -57,7 +58,8 @@ export function preview(props: DatagridPreviewProps): ReactElement {
5758
);
5859

5960
const EmptyPlaceholder = props.emptyPlaceholder.renderer;
60-
61+
const selectActionProps = useOnSelectProps(undefined);
62+
const { selectionStatus, selectionMethod } = selectionSettings(props, undefined);
6163
return (
6264
<Table
6365
cellRenderer={useCallback(
@@ -135,6 +137,11 @@ export function preview(props: DatagridPreviewProps): ReactElement {
135137
preview
136138
styles={parseStyle(props.style)}
137139
valueForSort={useCallback(() => undefined, [])}
140+
onSelect={selectActionProps.onSelect}
141+
onSelectAll={selectActionProps.onSelectAll}
142+
isSelected={selectActionProps.isSelected}
143+
selectionStatus={selectionStatus}
144+
selectionMethod={selectionMethod}
138145
/>
139146
);
140147
}

packages/pluggableWidgets/datagrid-web/src/Datagrid.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { createElement, ReactElement, useCallback, useEffect, useMemo, useRef, u
22
import { ColumnsType, DatagridContainerProps } from "../typings/DatagridProps";
33
import { FilterCondition } from "mendix/filters";
44
import { and } from "mendix/filters/builders";
5-
65
import { Table, TableColumn } from "./components/Table";
76
import {
87
FilterFunction,
@@ -11,10 +10,12 @@ import {
1110
useFilterContext,
1211
useMultipleFiltering
1312
} from "@mendix/pluggable-widgets-commons/components/web";
14-
import { isAvailable } from "@mendix/pluggable-widgets-commons";
13+
import { isAvailable, useSelectionHelper } from "@mendix/pluggable-widgets-commons";
1514
import { extractFilters } from "./features/filters";
1615
import { useCellRenderer } from "./features/cell";
1716
import { getColumnAssociationProps, isSortable } from "./features/column";
17+
import { selectionSettings, useOnSelectProps } from "./features/selection";
18+
import "./ui/Datagrid.scss";
1819

1920
export default function Datagrid(props: DatagridContainerProps): ReactElement {
2021
const id = useRef(`DataGrid${generateUUID()}`);
@@ -115,8 +116,14 @@ export default function Datagrid(props: DatagridContainerProps): ReactElement {
115116
[props.filterList]
116117
);
117118

119+
const selection = useSelectionHelper(props.itemSelection, props.datasource, props.onSelectionChange);
120+
const selectActionProps = useOnSelectProps(selection);
121+
const { selectionStatus, selectionMethod } = selectionSettings(props, selection);
122+
118123
return (
119124
<Table
125+
selectionStatus={selectionStatus}
126+
selectionMethod={selectionMethod}
120127
cellRenderer={cellRenderer}
121128
className={props.class}
122129
columns={columns}
@@ -212,6 +219,9 @@ export default function Datagrid(props: DatagridContainerProps): ReactElement {
212219
},
213220
[props.columns]
214221
)}
222+
onSelect={selectActionProps.onSelect}
223+
onSelectAll={selectActionProps.onSelectAll}
224+
isSelected={selectActionProps.isSelected}
215225
/>
216226
);
217227
}

packages/pluggableWidgets/datagrid-web/src/Datagrid.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
<caption>Refresh time (in seconds)</caption>
2121
<description />
2222
</property>
23+
<property key="itemSelection" type="selection" dataSource="datasource">
24+
<caption>Selection</caption>
25+
<description />
26+
<selectionTypes>
27+
<selectionType name="None" />
28+
<selectionType name="Single" />
29+
<selectionType name="Multi" />
30+
</selectionTypes>
31+
</property>
32+
<property key="itemSelectionMethod" type="enumeration" defaultValue="checkbox">
33+
<caption>Selection method</caption>
34+
<description />
35+
<enumerationValues>
36+
<enumerationValue key="checkbox">Checkbox</enumerationValue>
37+
<enumerationValue key="rowClick">Row click</enumerationValue>
38+
</enumerationValues>
39+
</property>
40+
<property key="showSelectAllToggle" type="boolean" defaultValue="true">
41+
<caption>Show (un)check all toggle</caption>
42+
<description>Show a checkbox in the grid header to check or uncheck multiple items.</description>
43+
</property>
2344
</propertyGroup>
2445
<propertyGroup caption="Columns">
2546
<property key="columns" type="object" isList="true">
@@ -196,6 +217,10 @@
196217
<caption>On click action</caption>
197218
<description />
198219
</property>
220+
<property key="onSelectionChange" type="action" required="false">
221+
<caption>On selection change</caption>
222+
<description />
223+
</property>
199224
</propertyGroup>
200225
</propertyGroup>
201226
<propertyGroup caption="Personalization">

packages/pluggableWidgets/datagrid-web/src/components/ColumnSelector.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export function ColumnSelector(props: ColumnSelectorProps): ReactElement {
9797
style={{ pointerEvents: "none" }}
9898
type="checkbox"
9999
tabIndex={-1}
100+
onChange={onChangeStub}
100101
/>
101102
<label htmlFor={`${props.id}_checkbox_toggle_${index}`} style={{ pointerEvents: "none" }}>
102103
{column.header}
@@ -141,3 +142,7 @@ export function ColumnSelector(props: ColumnSelectorProps): ReactElement {
141142
</div>
142143
);
143144
}
145+
146+
function onChangeStub(): void {
147+
// Stub to prevent react warnings in unit tests
148+
}

0 commit comments

Comments
 (0)