Skip to content

Commit 0935671

Browse files
authored
Merge pull request #874 from Cuiyansong/hotfix-869
Hotfix 869
2 parents da6bd49 + 265a2c2 commit 0935671

File tree

6 files changed

+129
-45
lines changed

6 files changed

+129
-45
lines changed

frontend/src/app/components/ChartGraph/BasicTableChart/BasicTableChart.tsx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class BasicTableChart extends ReactChart {
162162
return {
163163
rowKey: 'id',
164164
pagination: tablePagination,
165-
dataSource: chartDataSet.map(row => row.convertToObject()),
165+
dataSource: chartDataSet,
166166
columns: tableColumns,
167167
summaryFn: this.getTableSummaryFn(
168168
settingConfigs,
@@ -171,6 +171,17 @@ class BasicTableChart extends ReactChart {
171171
aggregateConfigs,
172172
context,
173173
),
174+
onRow: (_, index) => {
175+
const row = chartDataSet?.[index];
176+
const rowData = mixedSectionConfigRows?.reduce(
177+
(acc, cur, sectionIndex) => {
178+
acc[chartDataSet.getFieldOriginKey(cur)] = row?.[sectionIndex];
179+
return acc;
180+
},
181+
{},
182+
);
183+
return { index, rowData };
184+
},
174185
components: this.getTableComponents(styleConfigs, widgetSpecialConfig),
175186
...this.getAntdTableStyleOptions(styleConfigs, settingConfigs, context),
176187
onChange: (pagination, filters, sorter, extra) => {
@@ -500,29 +511,34 @@ class BasicTableChart extends ReactChart {
500511
},
501512
body: {
502513
cell: props => {
503-
const { style, dataIndex, ...rest } = props;
514+
const { style, key, rowData, ...rest } = props;
504515
const uid = props.uid;
505516
const [conditionStyle] = getStyles(
506517
getAllColumnListInfo,
507518
[uid, 'conditionStyle'],
508519
['conditionStylePanel'],
509520
);
510521
const conditionalCellStyle = getCustomBodyCellStyle(
511-
props,
522+
props?.cellValue,
512523
conditionStyle,
513524
);
525+
const sensitiveFieldName = Object.keys(rowData || {})?.[0];
514526
return (
515527
<TableComponentsTd
516528
{...rest}
517529
style={Object.assign(style || {}, conditionalCellStyle)}
518-
isLinkCell={linkFields?.includes(dataIndex)}
519-
isJumpCell={jumpField === dataIndex}
530+
isLinkCell={linkFields?.includes(sensitiveFieldName)}
531+
isJumpCell={jumpField === sensitiveFieldName}
520532
/>
521533
);
522534
},
523535
row: props => {
524536
const { style, ...rest } = props;
525-
const rowStyle = getCustomBodyRowStyle(props, allConditionStyle);
537+
// NOTE: rowData is case sensitive row keys object
538+
const rowStyle = getCustomBodyRowStyle(
539+
props.rowData,
540+
allConditionStyle,
541+
);
526542
return <tr {...rest} style={Object.assign(style || {}, rowStyle)} />;
527543
},
528544
wrapper: props => {
@@ -633,8 +649,7 @@ class BasicTableChart extends ReactChart {
633649
return {
634650
sorter: true,
635651
title: getColumnRenderName(c),
636-
dataIndex: chartDataSet.getFieldKey(c),
637-
key: chartDataSet.getFieldKey(c),
652+
dataIndex: chartDataSet.getFieldIndex(c),
638653
aggregate: c?.aggregate,
639654
colName,
640655
width: colMaxWidth,
@@ -652,18 +667,21 @@ class BasicTableChart extends ReactChart {
652667
uid: c.uid,
653668
};
654669
},
655-
onCell: (record, rowIndex) => {
670+
onCell: (_, rowIndex) => {
656671
const row = chartDataSet[rowIndex];
657672
const cellValue = row.getCell(c);
673+
const rowData = { [chartDataSet.getFieldOriginKey(c)]: cellValue };
658674
return {
659675
uid: c.uid,
660676
cellValue,
661-
dataIndex: row.getFieldKey(c),
677+
dataIndex: row.getFieldIndex(c),
678+
rowData,
662679
...this.registerTableCellEvents(
663-
row.getFieldKey(c),
680+
colName,
664681
cellValue,
665682
rowIndex,
666-
record,
683+
rowData,
684+
c.aggregate,
667685
),
668686
};
669687
},
@@ -904,15 +922,17 @@ class BasicTableChart extends ReactChart {
904922
seriesName: string,
905923
value: any,
906924
dataIndex: number,
907-
record: any,
925+
rowData: any,
926+
aggOperator?: string,
908927
) {
909928
const eventParams = this.createrEventParams({
910929
seriesType: 'body',
911930
name: seriesName,
912931
data: {
913932
format: undefined,
914933
name: seriesName,
915-
rowData: record,
934+
aggOperator,
935+
rowData,
916936
value: value,
917937
},
918938
seriesName, // column name/index

frontend/src/app/components/ChartGraph/BasicTableChart/conditionStyle.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Datart
3+
*
4+
* Copyright 2021
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
119
import { ConditionStyleFormValues } from 'app/components/FormGenerator/Customize/ConditionStylePanel';
220
import { OperatorTypes } from 'app/components/FormGenerator/Customize/ConditionStylePanel/types';
321
import { CSSProperties } from 'react';
@@ -17,20 +35,22 @@ const isMatchedTheCondition = (
1735
matchTheCondition = value !== conditionValues;
1836
break;
1937
case OperatorTypes.Contain:
20-
matchTheCondition = (value as string).includes(conditionValues as string);
38+
matchTheCondition = (value as string)?.includes(
39+
conditionValues as string,
40+
);
2141
break;
2242
case OperatorTypes.NotContain:
23-
matchTheCondition = !(value as string).includes(
43+
matchTheCondition = !(value as string)?.includes(
2444
conditionValues as string,
2545
);
2646
break;
2747
case OperatorTypes.In:
28-
matchTheCondition = (conditionValues as (string | number)[]).includes(
48+
matchTheCondition = (conditionValues as (string | number)[])?.includes(
2949
value,
3050
);
3151
break;
3252
case OperatorTypes.NotIn:
33-
matchTheCondition = !(conditionValues as (string | number)[]).includes(
53+
matchTheCondition = !(conditionValues as (string | number)[])?.includes(
3454
value,
3555
);
3656
break;
@@ -70,13 +90,6 @@ const isMatchedTheCondition = (
7090
const getTheSameRange = (list, type) =>
7191
list?.filter(item => item?.range === type);
7292

73-
const getRowRecord = row => {
74-
if (!row?.length) {
75-
return {};
76-
}
77-
return row?.[0]?.props?.record || {};
78-
};
79-
8093
const deleteUndefinedProps = props => {
8194
return Object.keys(props).reduce((acc, cur) => {
8295
if (props[cur] !== undefined || props[cur] !== null) {
@@ -87,14 +100,14 @@ const deleteUndefinedProps = props => {
87100
};
88101

89102
export const getCustomBodyCellStyle = (
90-
props: any,
103+
cellValue: any,
91104
conditionStyle: ConditionStyleFormValues[],
92105
): CSSProperties => {
93106
const currentConfigs = getTheSameRange(conditionStyle, 'cell');
94107
if (!currentConfigs?.length) {
95108
return {};
96109
}
97-
const text = props.cellValue;
110+
const text = cellValue;
98111
let cellStyle: CSSProperties = {};
99112

100113
try {
@@ -112,7 +125,7 @@ export const getCustomBodyCellStyle = (
112125
};
113126

114127
export const getCustomBodyRowStyle = (
115-
props: any,
128+
rowRecord: { [k in string]: any },
116129
conditionStyle: ConditionStyleFormValues[],
117130
): CSSProperties => {
118131
const currentConfigs: ConditionStyleFormValues[] = getTheSameRange(
@@ -122,8 +135,6 @@ export const getCustomBodyRowStyle = (
122135
if (!currentConfigs?.length) {
123136
return {};
124137
}
125-
126-
const rowRecord = getRowRecord(props.children);
127138
let rowStyle: CSSProperties = {};
128139

129140
try {
@@ -134,7 +145,7 @@ export const getCustomBodyRowStyle = (
134145
color: { background, textColor },
135146
target: { name },
136147
}) => {
137-
rowStyle = isMatchedTheCondition(rowRecord[name], operator, value)
148+
rowStyle = isMatchedTheCondition(rowRecord?.[name], operator, value)
138149
? { backgroundColor: background, color: textColor }
139150
: rowStyle;
140151
},

frontend/src/app/components/ChartGraph/models/ChartDataSet.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class ChartDataSetBase extends Array {
4545
return this.toCaseInsensitive(getValueByColumnKey(field));
4646
}
4747

48+
protected toOriginKey(field: ChartDataSectionField): string {
49+
return getValueByColumnKey(field);
50+
}
51+
4852
protected createColumnIndexTable(metas?: ChartDatasetMeta[]): {
4953
[key: string]: number;
5054
} {
@@ -57,25 +61,35 @@ class ChartDataSetBase extends Array {
5761
protected toCaseInsensitive(key) {
5862
return String(key).toUpperCase();
5963
}
64+
65+
public getFieldOriginKey(field: ChartDataSectionField) {
66+
return this.toOriginKey(field);
67+
}
6068
}
6169

6270
export class ChartDataSet<T>
6371
extends ChartDataSetBase
6472
implements IChartDataSet<T>
6573
{
6674
private columnIndexTable: ColumnIndexTable = {};
75+
private originalFields?: ChartDataSectionField[];
6776

68-
constructor(columns: T[][], metas?: ChartDatasetMeta[]) {
77+
constructor(
78+
columns: T[][],
79+
metas?: ChartDatasetMeta[],
80+
fields?: ChartDataSectionField[],
81+
) {
6982
super(
7083
...(Array.prototype.map.call(columns, c => {
7184
if (c?.length === 1) {
7285
const row = new ChartDataSetRow(metas, []);
7386
row.push(c[0]);
7487
return row;
7588
}
76-
return new ChartDataSetRow(metas, c);
89+
return new ChartDataSetRow(metas, c, fields);
7790
}) as any),
7891
);
92+
this.originalFields = fields;
7993
this.columnIndexTable = super.createColumnIndexTable(metas);
8094
}
8195

@@ -87,6 +101,7 @@ export class ChartDataSet<T>
87101
return this.toIndexBy(this.columnIndexTable, field);
88102
}
89103

104+
// TODO(Stephen): should be passed by sorted fields not data configs
90105
public sortBy(dataConfigs: ChartDataConfig[]): void {
91106
const orderConfigs = dataConfigs
92107
.filter(
@@ -121,9 +136,13 @@ export class ChartDataSetRow<T>
121136
implements IChartDataSetRow<T>
122137
{
123138
private columnIndexTable: ColumnIndexTable = {};
139+
private metas: Array<{ name: string }> = [];
140+
private originalFields?: ChartDataSectionField[];
124141

125-
constructor(metas, items: T[]) {
142+
constructor(metas, items: T[], fields?: ChartDataSectionField[]) {
126143
super(...(items as any));
144+
this.metas = metas;
145+
this.originalFields = fields;
127146
this.columnIndexTable = this.createColumnIndexTable(metas);
128147
}
129148

@@ -149,4 +168,11 @@ export class ChartDataSetRow<T>
149168
return acc;
150169
}, {});
151170
}
171+
172+
public convertToCaseSensitiveObject(): object {
173+
return (this.originalFields || []).reduce((acc, cur) => {
174+
acc[this.getFieldOriginKey(cur)] = this[this.getFieldIndex(cur)];
175+
return acc;
176+
}, {});
177+
}
152178
}

frontend/src/app/types/ChartDataSet.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ export interface IChartDataSetRow<T> extends Array<T> {
2929
getFieldIndex(field: ChartDataSectionField): number;
3030

3131
convertToObject(): object;
32+
33+
convertToCaseSensitiveObject(): object;
3234
}
3335

3436
export interface IChartDataSet<T> extends Array<IChartDataSetRow<T>> {
3537
getFieldKey(field: ChartDataSectionField): string;
3638

39+
getFieldOriginKey(field: ChartDataSectionField): string;
40+
3741
getFieldIndex(field: ChartDataSectionField): number;
3842

3943
sortBy(dataConfigs: ChartDataConfig[]): void;

frontend/src/app/utils/__tests__/chartHelper.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,5 +468,30 @@ describe('Chart Helper ', () => {
468468
).toEqual(1);
469469
expect(chartDataSet[0].getCellByKey('AVG(age)')).toEqual('r1-c2-v');
470470
});
471+
472+
test('should get dataset row data with case sensitive', () => {
473+
const columns = [['r1-c1-v', 'r1-c2-v']];
474+
const metas = [{ name: 'name' }, { name: 'avg(age)' }];
475+
const chartDataSet = transformToDataSet(columns, metas, [
476+
{
477+
rows: [
478+
{
479+
colName: 'Name',
480+
},
481+
{
482+
colName: 'Age',
483+
aggregate: 'AVG',
484+
},
485+
],
486+
},
487+
] as any);
488+
489+
expect(chartDataSet?.length).toEqual(1);
490+
expect(chartDataSet[0] instanceof ChartDataSetRow).toBeTruthy();
491+
expect(chartDataSet[0].convertToCaseSensitiveObject()).toEqual({
492+
Name: 'r1-c1-v',
493+
'AVG(Age)': 'r1-c2-v',
494+
});
495+
});
471496
});
472497
});

0 commit comments

Comments
 (0)