Skip to content

Commit

Permalink
fix: the formula calculate error when the rollup field referenced is …
Browse files Browse the repository at this point in the history
…a date field (#1048)

* fix: record presort

* fix: the formula calculate error when the rollup field referenced is a date field
  • Loading branch information
Sky-FE authored Nov 1, 2024
1 parent f088aed commit 77c889c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
30 changes: 29 additions & 1 deletion packages/core/src/formula/visitor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/* eslint-disable sonarjs/no-duplicate-string */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { plainToInstance } from 'class-transformer';
import { DateFormattingPreset, TimeFormatting } from '../models';
import { CellValueType, DbFieldType, FieldType } from '../models/field/constant';
import { LinkFieldCore, FormulaFieldCore, NumberFieldCore } from '../models/field/derivate';
import {
LinkFieldCore,
FormulaFieldCore,
NumberFieldCore,
DateFieldCore,
} from '../models/field/derivate';
import type { FieldCore } from '../models/field/field';
import type { IRecord } from '../models/record';
import { evaluate } from './evaluate';
Expand All @@ -15,6 +21,7 @@ describe('EvalVisitor', () => {
fldNumber: 8,
fldMultipleNumber: [1, 2, 3],
fldMultipleLink: [{ id: 'recxxxxxxx' }, { id: 'recyyyyyyy', title: 'A2' }],
fldDate: new Date('2024-01-01'),
},
createdTime: new Date().toISOString(),
};
Expand All @@ -31,6 +38,21 @@ describe('EvalVisitor', () => {
cellValueType: CellValueType.Number,
};

const dateFieldJson = {
id: 'fldDate',
name: 'fldDateName',
description: 'A test date field',
type: FieldType.Date,
options: {
formatting: {
date: DateFormattingPreset.ISO,
time: TimeFormatting.None,
timeZone: 'Asia/Shanghai',
},
},
cellValueType: CellValueType.DateTime,
};

const multipleNumberFieldJson = {
id: 'fldMultipleNumber',
name: 'fldMultipleNumberName',
Expand Down Expand Up @@ -58,10 +80,12 @@ describe('EvalVisitor', () => {
const numberField = plainToInstance(NumberFieldCore, numberFieldJson);
const multipleNumberField = plainToInstance(NumberFieldCore, multipleNumberFieldJson);
const multipleLinkField = plainToInstance(LinkFieldCore, multipleLinkFieldJson);
const dateField = plainToInstance(DateFieldCore, dateFieldJson);
fieldContext = {
[numberField.id]: numberField,
[multipleNumberField.id]: multipleNumberField,
[multipleLinkField.id]: multipleLinkField,
[dateField.id]: dateField,
};
});

Expand Down Expand Up @@ -220,6 +244,10 @@ describe('EvalVisitor', () => {
expect(() => evalFormula('{}', fieldContext, record)).toThrowError();
});

it('should calculate date field when value type is Date', () => {
expect(evalFormula('{fldDate}', fieldContext, record)).toEqual('2024-01-01');
});

it('should calculate multiple number field', () => {
expect(evalFormula('{fldMultipleNumber}', fieldContext, record)).toEqual([1, 2, 3]);
});
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/formula/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ export class EvalVisitor

private createTypedValueByField(field: FieldCore) {
let value: any = this.record ? this.record.fields[field.id] : null;
if (value == null || field.cellValueType !== CellValueType.String) {

if (
value == null ||
![CellValueType.String, CellValueType.DateTime].includes(field.cellValueType)
) {
return new TypedValue(value, field.cellValueType, field.isMultipleCellValue, field);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ export const useGridSelection = (props: IUseGridSelectionProps) => {

if (recordMap[rowIndex]?.id === activeRecordId) return;

const isVisibleRecord = Object.values(recordMap).find(
(record) => record?.id === activeRecordId
);

if (isVisibleRecord) return;

mutateGetRecordStatus({
tableId,
recordId: activeCell.recordId,
Expand Down

0 comments on commit 77c889c

Please sign in to comment.