Skip to content

Commit d845151

Browse files
committed
refactor: improve naming, reduce prop drilling and unify component rendering
1 parent 3e437aa commit d845151

File tree

9 files changed

+60
-72
lines changed

9 files changed

+60
-72
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ const Container = observer((props: Props): ReactElement => {
117117
pageSize={props.pageSize}
118118
paginationType={props.pagination}
119119
loadMoreButtonCaption={props.loadMoreButtonCaption?.value}
120-
clearSelectionButtonLabel={props.clearSelectionButtonLabel?.value}
121-
selectionCountVisibility={props.selectionCountVisibility}
120+
selectionCountPosition={props.selectionCountPosition}
122121
paging={paginationCtrl.showPagination}
123122
pagingPosition={props.pagingPosition}
124123
showPagingButtons={props.showPagingButtons}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@
244244
<enumerationValue key="both">Both</enumerationValue>
245245
</enumerationValues>
246246
</property>
247-
<property key="selectionCountVisibility" type="enumeration" defaultValue="bottom" required="true">
247+
<property key="selectionCountPosition" type="enumeration" defaultValue="bottom" required="true">
248248
<caption>Show selection count</caption>
249249
<description />
250250
<enumerationValues>

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ import { observer } from "mobx-react-lite";
33
import { createElement } from "react";
44
import { useDatagridRootScope } from "../helpers/root-context";
55

6+
type SelectionCounterLocation = "top" | "bottom" | undefined;
7+
68
export const SelectionCounter = observer(function SelectionCounter({
7-
clearSelectionButtonLabel
9+
location
810
}: {
9-
clearSelectionButtonLabel?: string;
11+
location?: SelectionCounterLocation;
1012
}) {
1113
const { selectionCountStore, selectActionHelper } = useDatagridRootScope();
1214

15+
const containerClass = location === "top" ? "widget-datagrid-tb-start" : "widget-datagrid-pb-start";
16+
1317
return (
1418
<If condition={selectionCountStore.displayCount !== ""}>
15-
<span className="widget-datagrid-selection-count">{selectionCountStore.displayCount}</span>&nbsp;|&nbsp;
16-
<button className="widget-datagrid-clear-selection" onClick={selectActionHelper.onClearSelection}>
17-
{clearSelectionButtonLabel || "Clear selection"}
18-
</button>
19+
<div className={containerClass}>
20+
<span className="widget-datagrid-selection-count">{selectionCountStore.displayCount}</span>&nbsp;|&nbsp;
21+
<button className="widget-datagrid-clear-selection" onClick={selectActionHelper.onClearSelection}>
22+
{selectionCountStore.clearButtonLabel}
23+
</button>
24+
</div>
1925
</If>
2026
);
2127
});

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
LoadingTypeEnum,
1010
PaginationEnum,
1111
PagingPositionEnum,
12-
SelectionCountVisibilityEnum,
12+
SelectionCountPositionEnum,
1313
ShowPagingButtonsEnum
1414
} from "../../typings/DatagridProps";
1515
import { SelectActionHelper } from "../helpers/SelectActionHelper";
@@ -26,6 +26,7 @@ import { WidgetFooter } from "./WidgetFooter";
2626
import { WidgetHeader } from "./WidgetHeader";
2727
import { WidgetRoot } from "./WidgetRoot";
2828
import { WidgetTopBar } from "./WidgetTopBar";
29+
import { SelectionCounter } from "./SelectionCounter";
2930

3031
export interface WidgetProps<C extends GridColumn, T extends ObjectItem = ObjectItem> {
3132
CellComponent: CellComponent<C>;
@@ -49,8 +50,7 @@ export interface WidgetProps<C extends GridColumn, T extends ObjectItem = Object
4950
page: number;
5051
paginationType: PaginationEnum;
5152
loadMoreButtonCaption?: string;
52-
clearSelectionButtonLabel?: string;
53-
selectionCountVisibility?: SelectionCountVisibilityEnum;
53+
selectionCountPosition?: SelectionCountPositionEnum;
5454
pageSize: number;
5555
paging: boolean;
5656
pagingPosition: PagingPositionEnum;
@@ -121,8 +121,7 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
121121
headerContent,
122122
headerTitle,
123123
loadMoreButtonCaption,
124-
clearSelectionButtonLabel,
125-
selectionCountVisibility,
124+
selectionCountPosition,
126125
numberOfItems,
127126
page,
128127
pageSize,
@@ -139,8 +138,8 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
139138
const { basicData, selectionCountStore } = useDatagridRootScope();
140139

141140
const showHeader = !!headerContent;
142-
const showTopBar = paging && (pagingPosition === "top" || pagingPosition === "both");
143-
const showFooter = paging && (pagingPosition === "bottom" || pagingPosition === "both");
141+
const showTopBarPagination = paging && (pagingPosition === "top" || pagingPosition === "both");
142+
const showFooterPagination = paging && (pagingPosition === "bottom" || pagingPosition === "both");
144143

145144
const pagination = paging ? (
146145
<Pagination
@@ -157,6 +156,14 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
157156
/>
158157
) : null;
159158

159+
const selectionCount =
160+
selectionCountStore.selectedCount > 0 && selectionCountPosition !== "off" && selectionCountPosition ? (
161+
<SelectionCounter location={selectionCountPosition} />
162+
) : null;
163+
164+
const showTopbarSelectionCount = selectionCount && selectionCountPosition === "top";
165+
const showFooterSelectionCount = selectionCount && selectionCountPosition === "bottom";
166+
160167
const cssGridStyles = gridStyle(visibleColumns, {
161168
selectItemColumn: selectActionHelper.showCheckboxColumn,
162169
visibilitySelectorColumn: columnsHidable
@@ -167,13 +174,9 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
167174
return (
168175
<Fragment>
169176
<WidgetTopBar
170-
selectedCount={selectionCountStore.selectedCount}
171-
showTopBar={showTopBar}
172-
selectionCountVisibility={selectionCountVisibility}
173-
clearSelectionButtonLabel={clearSelectionButtonLabel}
174-
>
175-
{pagination}
176-
</WidgetTopBar>
177+
pagination={showTopBarPagination ? pagination : undefined}
178+
selectionCount={showTopbarSelectionCount ? selectionCount : undefined}
179+
></WidgetTopBar>
177180
{showHeader && <WidgetHeader headerTitle={headerTitle}>{headerContent}</WidgetHeader>}
178181
<WidgetContent>
179182
<Grid
@@ -244,13 +247,10 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
244247
</Grid>
245248
</WidgetContent>
246249
<WidgetFooter
247-
selectedCount={selectionCountStore.selectedCount}
248-
showFooter={showFooter}
249-
pagination={pagination}
250+
pagination={showFooterPagination ? pagination : undefined}
251+
selectionCount={showFooterSelectionCount ? selectionCount : undefined}
250252
paginationType={paginationType}
251253
loadMoreButtonCaption={loadMoreButtonCaption}
252-
clearSelectionButtonLabel={clearSelectionButtonLabel}
253-
selectionCountVisibility={selectionCountVisibility}
254254
hasMoreItems={hasMoreItems}
255255
setPage={setPage}
256256
/>

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
11
import { createElement, ReactElement, ReactNode } from "react";
2-
import { PaginationEnum, SelectionCountVisibilityEnum } from "../../typings/DatagridProps";
3-
import { SelectionCounter } from "./SelectionCounter";
2+
import { PaginationEnum } from "../../typings/DatagridProps";
43

54
type WidgetFooterProps = {
65
pagination: ReactNode;
6+
selectionCount: ReactNode;
77
paginationType: PaginationEnum;
88
loadMoreButtonCaption?: string;
9-
clearSelectionButtonLabel?: string;
10-
selectionCountVisibility?: SelectionCountVisibilityEnum;
119
hasMoreItems: boolean;
12-
showFooter: boolean;
13-
selectedCount: number;
1410
setPage?: (computePage: (prevPage: number) => number) => void;
1511
} & JSX.IntrinsicElements["div"];
1612

1713
export function WidgetFooter(props: WidgetFooterProps): ReactElement | null {
18-
const {
19-
pagination,
20-
paginationType,
21-
loadMoreButtonCaption,
22-
clearSelectionButtonLabel,
23-
selectionCountVisibility,
24-
hasMoreItems,
25-
setPage,
26-
showFooter,
27-
selectedCount,
28-
...rest
29-
} = props;
14+
const { pagination, selectionCount, paginationType, loadMoreButtonCaption, hasMoreItems, setPage, ...rest } = props;
3015

3116
return (
3217
<div {...rest} className="widget-datagrid-footer table-footer">
3318
<div className="widget-datagrid-paging-bottom">
34-
{selectionCountVisibility === "bottom" && selectedCount > 0 && (
35-
<div className="widget-datagrid-pb-start">
36-
<SelectionCounter clearSelectionButtonLabel={clearSelectionButtonLabel} />
37-
</div>
38-
)}
19+
{selectionCount}
3920
<div className="widget-datagrid-pb-end">
40-
{showFooter && pagination}
21+
{pagination}
4122
{hasMoreItems && paginationType === "loadMore" && (
4223
<button
4324
className="btn btn-primary widget-datagrid-load-more"

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
import { createElement, ReactElement } from "react";
2-
import { SelectionCountVisibilityEnum } from "../../typings/DatagridProps";
3-
import { SelectionCounter } from "./SelectionCounter";
1+
import { createElement, ReactElement, ReactNode } from "react";
42

53
type WidgetTopBarProps = {
6-
selectionCountVisibility?: SelectionCountVisibilityEnum;
7-
clearSelectionButtonLabel?: string;
8-
showTopBar: boolean;
9-
selectedCount: number;
4+
pagination: ReactNode;
5+
selectionCount: ReactNode;
106
} & JSX.IntrinsicElements["div"];
117

128
export function WidgetTopBar(props: WidgetTopBarProps): ReactElement {
13-
const { selectionCountVisibility, clearSelectionButtonLabel, showTopBar, selectedCount, ...restProps } = props;
9+
const { pagination, selectionCount, ...rest } = props;
10+
1411
return (
15-
<div {...restProps} className="widget-datagrid-top-bar table-header">
12+
<div {...rest} className="widget-datagrid-top-bar table-header">
1613
<div className="widget-datagrid-padding-top">
17-
{selectionCountVisibility === "top" && selectedCount > 0 && (
18-
<div className="widget-datagrid-tb-start">
19-
<SelectionCounter clearSelectionButtonLabel={clearSelectionButtonLabel} />
20-
</div>
21-
)}
22-
{showTopBar && <div className="widget-datagrid-tb-end">{props.children}</div>}
14+
{selectionCount}
15+
{pagination && <div className="widget-datagrid-tb-end">{pagination}</div>}
2316
</div>
2417
</div>
2518
);

packages/pluggableWidgets/datagrid-web/src/helpers/state/RootGridStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type RequiredProps = Pick<
3434
| "pagination"
3535
| "showPagingButtons"
3636
| "showNumberOfRows"
37+
| "clearSelectionButtonLabel"
3738
>;
3839

3940
type Gate = DerivedPropsGate<RequiredProps>;

packages/pluggableWidgets/datagrid-web/typings/DatagridProps.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type ShowPagingButtonsEnum = "always" | "auto";
5353

5454
export type PagingPositionEnum = "bottom" | "top" | "both";
5555

56-
export type SelectionCountVisibilityEnum = "top" | "bottom" | "off";
56+
export type SelectionCountPositionEnum = "top" | "bottom" | "off";
5757

5858
export type ShowEmptyPlaceholderEnum = "none" | "custom";
5959

@@ -107,7 +107,7 @@ export interface DatagridContainerProps {
107107
showPagingButtons: ShowPagingButtonsEnum;
108108
showNumberOfRows: boolean;
109109
pagingPosition: PagingPositionEnum;
110-
selectionCountVisibility: SelectionCountVisibilityEnum;
110+
selectionCountPosition: SelectionCountPositionEnum;
111111
clearSelectionButtonLabel?: DynamicValue<string>;
112112
loadMoreButtonCaption?: DynamicValue<string>;
113113
showEmptyPlaceholder: ShowEmptyPlaceholderEnum;
@@ -161,7 +161,7 @@ export interface DatagridPreviewProps {
161161
showPagingButtons: ShowPagingButtonsEnum;
162162
showNumberOfRows: boolean;
163163
pagingPosition: PagingPositionEnum;
164-
selectionCountVisibility: SelectionCountVisibilityEnum;
164+
selectionCountPosition: SelectionCountPositionEnum;
165165
clearSelectionButtonLabel: string;
166166
loadMoreButtonCaption: string;
167167
showEmptyPlaceholder: ShowEmptyPlaceholderEnum;

packages/shared/widget-plugin-grid/src/selection/stores/SelectionCountStore.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,34 @@ type Gate = DerivedPropsGate<{
66
itemSelection?: SelectionSingleValue | SelectionMultiValue;
77
selectedCountTemplateSingular?: DynamicValue<string>;
88
selectedCountTemplatePlural?: DynamicValue<string>;
9+
clearSelectionButtonLabel?: DynamicValue<string>;
910
}>;
1011

1112
export class SelectionCountStore {
1213
private gate: Gate;
1314
private singular: string = "%d row selected";
1415
private plural: string = "%d rows selected";
16+
private defaultClearLabel: string = "Clear selection";
1517

16-
constructor(gate: Gate, spec: { singular?: string; plural?: string } = {}) {
18+
constructor(gate: Gate, spec: { singular?: string; plural?: string; clearLabel?: string } = {}) {
1719
this.singular = spec.singular ?? this.singular;
1820
this.plural = spec.plural ?? this.plural;
21+
this.defaultClearLabel = spec.clearLabel ?? this.defaultClearLabel;
1922
this.gate = gate;
2023

2124
makeObservable(this, {
2225
displayCount: computed,
2326
selectedCount: computed,
2427
fmtSingular: computed,
25-
fmtPlural: computed
28+
fmtPlural: computed,
29+
clearButtonLabel: computed
2630
});
2731
}
2832

33+
get clearButtonLabel(): string {
34+
return this.gate.props.clearSelectionButtonLabel?.value || this.defaultClearLabel;
35+
}
36+
2937
get fmtSingular(): string {
3038
return this.gate.props.selectedCountTemplateSingular?.value || this.singular;
3139
}

0 commit comments

Comments
 (0)