From bafb03a2286bf2b2d108728117cf809e951e6925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B0=94-Rain?= <958414905@qq.com> Date: Mon, 6 Nov 2023 07:10:41 +0800 Subject: [PATCH] fix(variable): compat $date (#2971) * fix(variable): compat $date * chore: update tests --- .../variables/__tests__/useVariables.test.tsx | 81 +++++++++++++++++++ .../variables/hooks/useBuiltinVariables.ts | 8 ++ .../src/variables/hooks/useLocalVariables.tsx | 14 ++++ 3 files changed, 103 insertions(+) diff --git a/packages/core/client/src/variables/__tests__/useVariables.test.tsx b/packages/core/client/src/variables/__tests__/useVariables.test.tsx index 477b38ea8..0a2d24776 100644 --- a/packages/core/client/src/variables/__tests__/useVariables.test.tsx +++ b/packages/core/client/src/variables/__tests__/useVariables.test.tsx @@ -140,6 +140,33 @@ describe('useVariables', () => { await waitFor(async () => { expect(result.current.ctxRef.current).toMatchInlineSnapshot(` { + "$date": { + "last30Days": [Function], + "last7Days": [Function], + "last90Days": [Function], + "lastIsoWeek": [Function], + "lastMonth": [Function], + "lastQuarter": [Function], + "lastWeek": [Function], + "lastYear": [Function], + "next30Days": [Function], + "next7Days": [Function], + "next90Days": [Function], + "nextIsoWeek": [Function], + "nextMonth": [Function], + "nextQuarter": [Function], + "nextWeek": [Function], + "nextYear": [Function], + "now": [Function], + "thisIsoWeek": [Function], + "thisMonth": [Function], + "thisQuarter": [Function], + "thisWeek": [Function], + "thisYear": [Function], + "today": [Function], + "tomorrow": [Function], + "yesterday": [Function], + }, "$nDate": { "last30Days": [Function], "last7Days": [Function], @@ -323,6 +350,33 @@ describe('useVariables', () => { await waitFor(async () => { expect(result.current.ctxRef.current).toMatchInlineSnapshot(` { + "$date": { + "last30Days": [Function], + "last7Days": [Function], + "last90Days": [Function], + "lastIsoWeek": [Function], + "lastMonth": [Function], + "lastQuarter": [Function], + "lastWeek": [Function], + "lastYear": [Function], + "next30Days": [Function], + "next7Days": [Function], + "next90Days": [Function], + "nextIsoWeek": [Function], + "nextMonth": [Function], + "nextQuarter": [Function], + "nextWeek": [Function], + "nextYear": [Function], + "now": [Function], + "thisIsoWeek": [Function], + "thisMonth": [Function], + "thisQuarter": [Function], + "thisWeek": [Function], + "thisYear": [Function], + "today": [Function], + "tomorrow": [Function], + "yesterday": [Function], + }, "$nDate": { "last30Days": [Function], "last7Days": [Function], @@ -379,6 +433,33 @@ describe('useVariables', () => { await waitFor(async () => { expect(result.current.ctxRef.current).toMatchInlineSnapshot(` { + "$date": { + "last30Days": [Function], + "last7Days": [Function], + "last90Days": [Function], + "lastIsoWeek": [Function], + "lastMonth": [Function], + "lastQuarter": [Function], + "lastWeek": [Function], + "lastYear": [Function], + "next30Days": [Function], + "next7Days": [Function], + "next90Days": [Function], + "nextIsoWeek": [Function], + "nextMonth": [Function], + "nextQuarter": [Function], + "nextWeek": [Function], + "nextYear": [Function], + "now": [Function], + "thisIsoWeek": [Function], + "thisMonth": [Function], + "thisQuarter": [Function], + "thisWeek": [Function], + "thisYear": [Function], + "today": [Function], + "tomorrow": [Function], + "yesterday": [Function], + }, "$nDate": { "last30Days": [Function], "last7Days": [Function], diff --git a/packages/core/client/src/variables/hooks/useBuiltinVariables.ts b/packages/core/client/src/variables/hooks/useBuiltinVariables.ts index 18a4b3ebb..7c95ad75d 100644 --- a/packages/core/client/src/variables/hooks/useBuiltinVariables.ts +++ b/packages/core/client/src/variables/hooks/useBuiltinVariables.ts @@ -31,6 +31,14 @@ const useBuiltInVariables = () => { name: '$nDate', ctx: dateVars, }, + /** + * @deprecated + * 兼容旧版本的 `$date` 变量,新版本已弃用 + */ + { + name: '$date', + ctx: dateVars, + }, /** * @deprecated * 兼容旧版本的 `$system` 变量,新版本已弃用 diff --git a/packages/core/client/src/variables/hooks/useLocalVariables.tsx b/packages/core/client/src/variables/hooks/useLocalVariables.tsx index 471d824a9..5d67e25cc 100644 --- a/packages/core/client/src/variables/hooks/useLocalVariables.tsx +++ b/packages/core/client/src/variables/hooks/useLocalVariables.tsx @@ -4,6 +4,7 @@ import { useFormBlockContext } from '../../block-provider'; import { useCollection } from '../../collection-manager'; import { useRecord } from '../../record-provider'; import { useSubFormValue } from '../../schema-component/antd/association-field/hooks'; +import { getDateRanges } from '../../schema-component/antd/date-picker/util'; import { useBlockCollection } from '../../schema-settings/VariableInput/hooks/useBlockCollection'; import { VariableOption } from '../types'; @@ -31,6 +32,7 @@ const useLocalVariables = (props?: Props) => { } return useMemo(() => { + const dateVars = getDateRanges(); return ( [ /** @@ -75,6 +77,18 @@ const useLocalVariables = (props?: Props) => { ctx: form?.values, collectionName: name, }, + { + name: '$nDate', + ctx: dateVars, + }, + /** + * @deprecated + * 兼容旧版本的 `$date` 变量,新版本已弃用 + */ + { + name: '$date', + ctx: dateVars, + }, subFormValue && { name: '$iteration', ctx: subFormValue, collectionName: currentCollectionName }, ] as VariableOption[] ).filter(Boolean);