fix(data-vi): issue of getting wrong value when aggregating select fields (#5214)

* fix(data-vi): issue of getting wrong value when aggregating select fields

* test: add test case
This commit is contained in:
YANG QIA 2024-09-06 09:28:13 +08:00 committed by GitHub
parent 3e9ff85305
commit bd0a123cd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 31 deletions

View File

@ -10,7 +10,8 @@
import { processData } from '../utils';
describe('utils', () => {
it('processFields', () => {
describe('process data', () => {
it('should process select field', () => {
expect(
processData(
[
@ -41,4 +42,40 @@ describe('utils', () => {
),
).toEqual([{ tag: 'Yes' }]);
});
it('should not process when aggregating', () => {
expect(
processData(
[
{
name: 'tag',
type: 'bigInt',
interface: 'select',
uiSchema: {
type: 'string',
enum: [
{
value: '1',
label: 'Yes',
},
{
value: '2',
label: 'No',
},
],
},
label: 'Tag',
value: 'tag',
key: 'tag',
query: {
field: 'tag',
aggregation: 'count',
},
},
],
[{ tag: 1 }],
{},
),
).toEqual([{ tag: 1 }]);
});
});
});

View File

@ -73,6 +73,7 @@ export const getSelectedFields = (fields: FieldOption[], query: QueryProps) => {
key: selectedField.alias || fieldProps?.key,
label: selectedField.alias || fieldProps?.label,
value: selectedField.alias || fieldProps?.value,
query: selectedField,
};
});
};
@ -84,7 +85,7 @@ export const getSelectedFields = (fields: FieldOption[], query: QueryProps) => {
return selectedFields;
};
export const processData = (selectedFields: FieldOption[], data: any[], scope: any) => {
export const processData = (selectedFields: (FieldOption & { query?: any })[], data: any[], scope: any) => {
const parseEnum = (field: FieldOption, value: any) => {
const options = field.uiSchema?.enum as { value: string; label: string }[];
if (!options || !Array.isArray(options)) {
@ -99,7 +100,7 @@ export const processData = (selectedFields: FieldOption[], data: any[], scope: a
return data.map((record) => {
const processed = {};
Object.entries(record).forEach(([key, value]) => {
const field = selectedFields.find((field) => field.value === key);
const field = selectedFields.find((field) => field.value === key && !field?.query?.aggregation);
if (!field) {
processed[key] = value;
return;