Skip to content

Commit c4d428e

Browse files
committed
fix(table): conditional style with case sensitive field name
1 parent 6d29f53 commit c4d428e

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

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

Lines changed: 29 additions & 13 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,
@@ -655,16 +670,17 @@ class BasicTableChart extends ReactChart {
655670
onCell: (_, rowIndex) => {
656671
const row = chartDataSet[rowIndex];
657672
const cellValue = row.getCell(c);
658-
const rawData = { [chartDataSet.getFieldOriginKey(c)]: cellValue };
673+
const rowData = { [chartDataSet.getFieldOriginKey(c)]: cellValue };
659674
return {
660675
uid: c.uid,
661676
cellValue,
662-
dataIndex: row.getFieldKey(c),
677+
dataIndex: row.getFieldIndex(c),
678+
rowData,
663679
...this.registerTableCellEvents(
664680
colName,
665681
cellValue,
666682
rowIndex,
667-
rawData,
683+
rowData,
668684
c.aggregate,
669685
),
670686
};
@@ -906,7 +922,7 @@ class BasicTableChart extends ReactChart {
906922
seriesName: string,
907923
value: any,
908924
dataIndex: number,
909-
record: any,
925+
rowData: any,
910926
aggOperator?: string,
911927
) {
912928
const eventParams = this.createrEventParams({
@@ -916,7 +932,7 @@ class BasicTableChart extends ReactChart {
916932
format: undefined,
917933
name: seriesName,
918934
aggOperator,
919-
rowData: record,
935+
rowData,
920936
value: value,
921937
},
922938
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/types/ChartDataSet.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export interface IChartDataSetRow<T> extends Array<T> {
3434
export interface IChartDataSet<T> extends Array<IChartDataSetRow<T>> {
3535
getFieldKey(field: ChartDataSectionField): string;
3636

37+
getFieldOriginKey(field: ChartDataSectionField): string;
38+
3739
getFieldIndex(field: ChartDataSectionField): number;
3840

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

0 commit comments

Comments
 (0)