Skip to content

Commit d86971d

Browse files
author
Illia Obukhau
committed
refactor(datagrid-web): convert SelectionMethod enum to type
1 parent 2d2a406 commit d86971d

File tree

4 files changed

+33
-68
lines changed

4 files changed

+33
-68
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ export function Table<T extends ObjectItem>(props: TableProps<T>): ReactElement
142142
const [columnsWidth, setColumnsWidth] = useState<ColumnWidth>(
143143
Object.fromEntries(columns.map((_c, index) => [index.toString(), undefined]))
144144
);
145-
const checkboxSelectionOn = selectionMethod === SelectionMethod.checkbox;
146-
const rowClickSelectionOn = selectionMethod === SelectionMethod.rowClick;
145+
const checkboxSelectionOn = selectionMethod === "checkbox";
146+
const rowClickSelectionOn = selectionMethod === "rowClick";
147147

148148
const { updateSettings } = useSettings(
149149
settings,
@@ -303,7 +303,7 @@ export function Table<T extends ObjectItem>(props: TableProps<T>): ReactElement
303303
>
304304
{selectionStatus && (
305305
<input
306-
checked={selectionStatus !== SelectionMethod.none}
306+
checked={selectionStatus !== "none"}
307307
type="checkbox"
308308
onChange={props.onSelectAll}
309309
/>

packages/pluggableWidgets/datagrid-web/src/components/__tests__/Table.spec.tsx

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { render } from "enzyme";
22
import * as testingLibrary from "@testing-library/react";
33
import { GUID, ObjectItem } from "mendix";
44
import { createElement } from "react";
5-
import { SelectionMethod } from "../../features/selection";
65
import { Table, TableProps } from "../Table";
76
import { objectItems } from "@mendix/pluggable-test-utils";
87
import "@testing-library/jest-dom";
@@ -178,7 +177,7 @@ describe("Table", () => {
178177
const items = objectItems(3);
179178

180179
const { container } = render(
181-
<Table {...mockTableProps()} data={items} paging selectionMethod={SelectionMethod.checkbox} />
180+
<Table {...mockTableProps()} data={items} paging selectionMethod={"checkbox"} />
182181
);
183182

184183
expect(container.firstChild).toHaveClass("widget-datagrid-selection-method-checkbox");
@@ -189,21 +188,15 @@ describe("Table", () => {
189188
const items = objectItems(3);
190189

191190
const { asFragment } = render(
192-
<Table
193-
{...mockTableProps()}
194-
data={items}
195-
paging
196-
selectionMethod={SelectionMethod.checkbox}
197-
isSelected={() => true}
198-
/>
191+
<Table {...mockTableProps()} data={items} paging selectionMethod={"checkbox"} isSelected={() => true} />
199192
);
200193

201194
expect(asFragment()).toMatchSnapshot();
202195
});
203196

204197
it("set negative tabindex on row checkbox", () => {
205198
const { getAllByRole } = testingLibrary.render(
206-
<Table {...mockTableProps()} paging selectionMethod={SelectionMethod.checkbox} />
199+
<Table {...mockTableProps()} paging selectionMethod={"checkbox"} />
207200
);
208201

209202
getAllByRole("checkbox").forEach(elt => {
@@ -215,7 +208,7 @@ describe("Table", () => {
215208
const items = objectItems(6);
216209
const [a, b, c, d, e, f] = items;
217210
const { rerender } = testingLibrary.render(
218-
<Table {...mockTableProps()} data={items} paging selectionMethod={SelectionMethod.checkbox} />
211+
<Table {...mockTableProps()} data={items} paging selectionMethod={"checkbox"} />
219212
);
220213

221214
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
@@ -229,7 +222,7 @@ describe("Table", () => {
229222
{...mockTableProps()}
230223
data={items}
231224
paging
232-
selectionMethod={SelectionMethod.checkbox}
225+
selectionMethod={"checkbox"}
233226
isSelected={item => [a, b, c].includes(item)}
234227
/>
235228
);
@@ -241,7 +234,7 @@ describe("Table", () => {
241234
{...mockTableProps()}
242235
data={items}
243236
paging
244-
selectionMethod={SelectionMethod.checkbox}
237+
selectionMethod={"checkbox"}
245238
isSelected={item => [c].includes(item)}
246239
/>
247240
);
@@ -253,7 +246,7 @@ describe("Table", () => {
253246
{...mockTableProps()}
254247
data={items}
255248
paging
256-
selectionMethod={SelectionMethod.checkbox}
249+
selectionMethod={"checkbox"}
257250
isSelected={item => [d, e].includes(item)}
258251
/>
259252
);
@@ -265,7 +258,7 @@ describe("Table", () => {
265258
{...mockTableProps()}
266259
data={items}
267260
paging
268-
selectionMethod={SelectionMethod.checkbox}
261+
selectionMethod={"checkbox"}
269262
isSelected={item => [f, e, d, a].includes(item)}
270263
/>
271264
);
@@ -279,13 +272,7 @@ describe("Table", () => {
279272
const onSelect = jest.fn();
280273

281274
render(
282-
<Table
283-
{...mockTableProps()}
284-
data={items}
285-
paging
286-
selectionMethod={SelectionMethod.checkbox}
287-
onSelect={onSelect}
288-
/>
275+
<Table {...mockTableProps()} data={items} paging selectionMethod={"checkbox"} onSelect={onSelect} />
289276
);
290277

291278
const checkbox1 = screen.getAllByRole("checkbox")[0];
@@ -317,7 +304,7 @@ describe("Table", () => {
317304
data={items}
318305
paging
319306
selectionStatus={undefined}
320-
selectionMethod={SelectionMethod.checkbox}
307+
selectionMethod={"checkbox"}
321308
/>
322309
);
323310

@@ -335,7 +322,7 @@ describe("Table", () => {
335322
data={items}
336323
paging
337324
selectionStatus={status}
338-
selectionMethod={SelectionMethod.checkbox}
325+
selectionMethod={"checkbox"}
339326
/>
340327
);
341328

@@ -360,7 +347,7 @@ describe("Table", () => {
360347
data={items}
361348
paging
362349
selectionStatus={"some"}
363-
selectionMethod={SelectionMethod.rowClick}
350+
selectionMethod={"rowClick"}
364351
/>
365352
);
366353

@@ -379,7 +366,7 @@ describe("Table", () => {
379366
data={items}
380367
paging
381368
selectionStatus="none"
382-
selectionMethod={SelectionMethod.checkbox}
369+
selectionMethod={"checkbox"}
383370
onSelectAll={onSelectAll}
384371
/>
385372
);
@@ -400,7 +387,7 @@ describe("Table", () => {
400387
const items = objectItems(3);
401388

402389
const { container } = render(
403-
<Table {...mockTableProps()} data={items} paging selectionMethod={SelectionMethod.rowClick} />
390+
<Table {...mockTableProps()} data={items} paging selectionMethod={"rowClick"} />
404391
);
405392

406393
expect(container.firstChild).toHaveClass("widget-datagrid-selection-method-click");
@@ -411,13 +398,7 @@ describe("Table", () => {
411398
const items = objectItems(3);
412399

413400
const { asFragment } = render(
414-
<Table
415-
{...mockTableProps()}
416-
data={items}
417-
paging
418-
selectionMethod={SelectionMethod.rowClick}
419-
isSelected={() => true}
420-
/>
401+
<Table {...mockTableProps()} data={items} paging selectionMethod={"rowClick"} isSelected={() => true} />
421402
);
422403

423404
expect(asFragment()).toMatchSnapshot();
@@ -442,7 +423,7 @@ describe("Table", () => {
442423
data={items}
443424
cellRenderer={(renderWrapper, _, columnIndex) => renderWrapper(columns[columnIndex].header)}
444425
paging
445-
selectionMethod={SelectionMethod.rowClick}
426+
selectionMethod={"rowClick"}
446427
onSelect={onSelect}
447428
/>
448429
);
@@ -514,7 +495,7 @@ function mockTableProps(): TableProps<ObjectItem> {
514495
onSelect: jest.fn(),
515496
onSelectAll: jest.fn(),
516497
isSelected: jest.fn(() => false),
517-
selectionMethod: SelectionMethod.none,
498+
selectionMethod: "none",
518499
selectionStatus: undefined
519500
};
520501
}

packages/pluggableWidgets/datagrid-web/src/features/__tests__/selection.spec.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SelectionProps, SelectionMethod, selectionSettings } from "../../features/selection";
1+
import { SelectionProps, selectionSettings } from "../../features/selection";
22
import {
33
SelectionSingleValueBuilder as SingleBuilder,
44
SelectionMultiValueBuilder as MultiBuilder
@@ -18,44 +18,36 @@ describe("features/selection", () => {
1818
it("is none, when selection is off", () => {
1919
const helper = undefined;
2020

21-
expect(selectionSettings(props(undefined, "checkbox"), helper).selectionMethod).toBe(SelectionMethod.none);
22-
expect(selectionSettings(props("None", "checkbox"), helper).selectionMethod).toBe(SelectionMethod.none);
21+
expect(selectionSettings(props(undefined, "checkbox"), helper).selectionMethod).toBe("none");
22+
expect(selectionSettings(props("None", "checkbox"), helper).selectionMethod).toBe("none");
2323
});
2424

2525
it("depends on the value of itemSelectionMethod prop", () => {
2626
const helper = undefined;
2727

2828
expect(selectionSettings(props(new SingleBuilder().build(), "checkbox"), helper).selectionMethod).toBe(
29-
SelectionMethod.checkbox
29+
"checkbox"
3030
);
3131

32-
expect(selectionSettings(props("Single", "checkbox"), helper).selectionMethod).toBe(
33-
SelectionMethod.checkbox
34-
);
32+
expect(selectionSettings(props("Single", "checkbox"), helper).selectionMethod).toBe("checkbox");
3533

3634
expect(selectionSettings(props(new SingleBuilder().build(), "rowClick"), helper).selectionMethod).toBe(
37-
SelectionMethod.rowClick
35+
"rowClick"
3836
);
3937

40-
expect(selectionSettings(props("Single", "rowClick"), helper).selectionMethod).toBe(
41-
SelectionMethod.rowClick
42-
);
38+
expect(selectionSettings(props("Single", "rowClick"), helper).selectionMethod).toBe("rowClick");
4339

4440
expect(selectionSettings(props(new MultiBuilder().build(), "checkbox"), helper).selectionMethod).toBe(
45-
SelectionMethod.checkbox
41+
"checkbox"
4642
);
4743

48-
expect(selectionSettings(props("Multi", "checkbox"), helper).selectionMethod).toBe(
49-
SelectionMethod.checkbox
50-
);
44+
expect(selectionSettings(props("Multi", "checkbox"), helper).selectionMethod).toBe("checkbox");
5145

5246
expect(selectionSettings(props(new MultiBuilder().build(), "rowClick"), helper).selectionMethod).toBe(
53-
SelectionMethod.rowClick
47+
"rowClick"
5448
);
5549

56-
expect(selectionSettings(props("Multi", "rowClick"), helper).selectionMethod).toBe(
57-
SelectionMethod.rowClick
58-
);
50+
expect(selectionSettings(props("Multi", "rowClick"), helper).selectionMethod).toBe("rowClick");
5951
});
6052
});
6153

packages/pluggableWidgets/datagrid-web/src/features/selection.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import type { ObjectItem } from "mendix";
33
import { MultiSelectionStatus, SelectionHelper } from "@mendix/pluggable-widgets-commons";
44
import { DatagridContainerProps, DatagridPreviewProps, ItemSelectionMethodEnum } from "typings/DatagridProps";
55

6-
export enum SelectionMethod {
7-
none = "none",
8-
rowClick = "rowClick",
9-
checkbox = "checkbox"
10-
}
6+
export type SelectionMethod = ItemSelectionMethodEnum | "none";
117

128
export type SelectActionProps = {
139
onSelect: (item: ObjectItem) => void;
@@ -74,10 +70,6 @@ export function selectionSettings(props: SelectionProps, helper: SelectionHelper
7470

7571
return {
7672
selectionStatus: selectAllOn ? status : undefined,
77-
selectionMethod: selectionOn
78-
? methodCheckbox
79-
? SelectionMethod.checkbox
80-
: SelectionMethod.rowClick
81-
: SelectionMethod.none
73+
selectionMethod: selectionOn ? itemSelectionMethod : "none"
8274
};
8375
}

0 commit comments

Comments
 (0)