Skip to content

Commit 6652c88

Browse files
committed
react
1 parent 707e06d commit 6652c88

File tree

5 files changed

+5309
-4621
lines changed

5 files changed

+5309
-4621
lines changed

React/src/App.css

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
1-
.main {
1+
.app {
22
margin: 50px;
33
width: 90vw;
4-
}
4+
}
5+
6+
#sales {
7+
margin-top: 80px;
8+
}
9+
10+
.long-title h3 {
11+
font-family: 'Segoe UI Light', 'Helvetica Neue Light', 'Segoe UI', 'Helvetica Neue', 'Trebuchet MS', Verdana;
12+
font-weight: 200;
13+
font-size: 28px;
14+
text-align: center;
15+
margin-bottom: 20px;
16+
}
17+
18+
.options {
19+
margin-top: 20px;
20+
padding: 20px;
21+
background: #f5f5f5;
22+
}
23+
24+
.options .caption {
25+
font-size: 18px;
26+
font-weight: 500;
27+
}
28+
29+
.option {
30+
margin-top: 10px;
31+
}
32+
33+
.option > span {
34+
width: 120px;
35+
display: inline-block;
36+
}
37+
38+
.option > .dx-widget {
39+
display: inline-block;
40+
vertical-align: middle;
41+
width: 100%;
42+
max-width: 350px;
43+
}

React/src/App.tsx

Lines changed: 117 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,124 @@
1-
import { useCallback, useState } from 'react';
2-
import './App.css';
31
import 'devextreme/dist/css/dx.material.blue.light.compact.css';
4-
import Button from 'devextreme-react/button';
2+
import {
3+
useMemo, useState, useRef, useCallback,
4+
} from 'react';
5+
import { type DataSourceOptions } from 'devextreme/common/data';
6+
import PivotGrid, { FieldChooser, type PivotGridRef } from 'devextreme-react/pivot-grid';
7+
import CheckBox, { type CheckBoxTypes } from 'devextreme-react/check-box';
8+
import service, { type Sale } from './service.ts';
9+
import './App.css';
10+
11+
interface CustomSummaryValue {
12+
summaryProcess?: string;
13+
value?: any;
14+
totalValue?: any;
15+
}
16+
17+
const tempSales = service.getSales();
18+
19+
tempSales.forEach((sale: Sale, index: number) => {
20+
sale.isApproved = index % 2 !== 0;
21+
});
22+
23+
const sales = tempSales;
24+
25+
function citySelector(data: Sale): string {
26+
return `${data.city} (${data.country})`;
27+
}
528

629
function App(): JSX.Element {
7-
var [count, setCount] = useState<number>(0);
8-
const clickHandler = useCallback(() => {
9-
setCount((prev) => prev + 1);
10-
}, [setCount]);
30+
const [isConditional, setIsConditional] = useState(true);
31+
const isConditionalRef = useRef(isConditional);
32+
const pivotGridRef = useRef<PivotGridRef>(null);
33+
34+
isConditionalRef.current = isConditional;
35+
36+
const calculateCustomSummary = useCallback((options: CustomSummaryValue) => {
37+
switch (options.summaryProcess) {
38+
case 'start':
39+
options.totalValue = { conditionalVal: 0, rawVal: 0, count: 0 };
40+
break;
41+
case 'calculate':
42+
options.totalValue.count += 1;
43+
options.totalValue.rawVal += options.value.amount;
44+
if (options.value.isApproved) {
45+
options.totalValue.conditionalVal += options.value.amount;
46+
}
47+
break;
48+
case 'finalize':
49+
if (options.totalValue.count === 1 || !isConditionalRef.current) {
50+
options.totalValue = options.totalValue.rawVal;
51+
} else {
52+
options.totalValue = options.totalValue.conditionalVal;
53+
}
54+
break;
55+
default: break;
56+
}
57+
}, []);
58+
59+
const dataSource = useMemo<DataSourceOptions>(() => ({
60+
fields: [{
61+
caption: 'Region',
62+
width: 120,
63+
dataField: 'region',
64+
area: 'row',
65+
expanded: true,
66+
}, {
67+
caption: 'City',
68+
dataField: 'city',
69+
width: 150,
70+
area: 'row',
71+
selector: citySelector,
72+
}, {
73+
dataField: 'date',
74+
dataType: 'date',
75+
area: 'column',
76+
expanded: true,
77+
}, {
78+
caption: 'Sales',
79+
dataType: 'number',
80+
summaryType: 'custom',
81+
format: 'currency',
82+
area: 'data',
83+
calculateCustomSummary,
84+
}, {
85+
caption: 'Approved',
86+
dataField: 'isApproved',
87+
summaryType: 'sum',
88+
area: 'data',
89+
}],
90+
store: sales,
91+
}), [calculateCustomSummary]);
92+
93+
const onValueChanged = useCallback((arg: CheckBoxTypes.ValueChangedEvent) => {
94+
setIsConditional(arg.value);
95+
pivotGridRef.current?.instance().getDataSource().reload().catch(() => {});
96+
}, []);
97+
1198
return (
12-
<div className="main">
13-
<Button text={`Click count: ${count}`} onClick={clickHandler} />
99+
<div className="app">
100+
<div className="long-title"><h3>Conditional Summary Calculation</h3></div>
101+
<PivotGrid id="sales"
102+
ref={pivotGridRef}
103+
allowSortingBySummary={true}
104+
allowSorting={true}
105+
allowFiltering={true}
106+
allowExpandAll={true}
107+
height={440}
108+
showBorders={true}
109+
dataSource={dataSource}>
110+
<FieldChooser enabled={false} />
111+
</PivotGrid>
112+
113+
<div className="options">
114+
<div className="caption">Options</div>
115+
<div className="option">
116+
<CheckBox
117+
value={isConditional}
118+
text="Toggle Conditional Summary Calculation"
119+
onValueChanged={onValueChanged} />
120+
</div>
121+
</div>
14122
</div>
15123
);
16124
}

React/src/orig_App.js

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)