From 4f56b0e55981db4d6e69a5ba248f284923e96b05 Mon Sep 17 00:00:00 2001 From: Katherine Date: Tue, 3 Sep 2024 10:16:16 +0800 Subject: [PATCH] fix: formula field adaptation time field (#5168) --- .../src/client/components/Formula/Result.tsx | 29 ++++++++++++++++--- .../plugin-field-formula/src/utils/index.ts | 2 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-field-formula/src/client/components/Formula/Result.tsx b/packages/plugins/@nocobase/plugin-field-formula/src/client/components/Formula/Result.tsx index d5d9938f85..1af2fbbdac 100644 --- a/packages/plugins/@nocobase/plugin-field-formula/src/client/components/Formula/Result.tsx +++ b/packages/plugins/@nocobase/plugin-field-formula/src/client/components/Formula/Result.tsx @@ -18,8 +18,8 @@ import { useCollection_deprecated, useCollectionManager_deprecated, useFormBlockContext, - ActionContext, } from '@nocobase/client'; +import _ from 'lodash'; import { Evaluator, evaluators } from '@nocobase/evaluators/client'; import { Registry, toFixedByStep } from '@nocobase/utils/client'; import React, { useEffect, useState, useContext } from 'react'; @@ -61,6 +61,22 @@ function getValuesByPath(values, key, index?) { } } +function areValuesEqual(value1, value2) { + if (_.isString(value1) && !isNaN(Date.parse(value1))) { + value1 = new Date(value1); + } + + if (_.isString(value2) && !isNaN(Date.parse(value2))) { + value2 = new Date(value2); + } + + if (_.isDate(value1) && _.isDate(value2)) { + return value1.getTime() === value2.getTime(); + } + + return _.isEqual(value1, value2); +} + export function Result(props) { const { value, ...others } = props; const fieldSchema = useFieldSchema(); @@ -99,11 +115,16 @@ export function Result(props) { setEditingValue(v); } setEditingValue(v); - if (v !== field.value) { - field.value = v; - } }); }); + + useEffect(() => { + if (!areValuesEqual(field.value, editingValue)) { + setTimeout(() => { + field.value = editingValue; + }); + } + }, [editingValue]); const Component = TypedComponents[dataType] ?? InputString; return ( diff --git a/packages/plugins/@nocobase/plugin-field-formula/src/utils/index.ts b/packages/plugins/@nocobase/plugin-field-formula/src/utils/index.ts index c0dc020f7d..6f031db95f 100644 --- a/packages/plugins/@nocobase/plugin-field-formula/src/utils/index.ts +++ b/packages/plugins/@nocobase/plugin-field-formula/src/utils/index.ts @@ -151,7 +151,7 @@ export const DataTypeTransformers = { }; export function toDbType(value: any, type: string) { - if (value == null) { + if (value == null || (!value && type === 'date')) { return null; }