From c99c5f12d93a7ac2b9da7ccbf4e1f912f380fcf6 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Thu, 10 Oct 2024 09:37:32 +0800 Subject: [PATCH] feat(variable): do not disable date variables (#5376) * feat(variable): do not disable date variables * chore: fix unit tests * Revert "chore: fix unit tests" This reverts commit 54e696af989cb96b8d0de2a932551168e3972bcb. * chore: fix unit tests * chore: fix unit tests --- .../__tests__/getDateRanges.test.ts | 84 +++++++++---------- .../schema-component/antd/date-picker/util.ts | 8 +- .../VariableInput/hooks/useVariableOptions.ts | 2 +- .../src/variables/VariablesProvider.tsx | 4 +- .../variables/__tests__/useVariables.test.tsx | 2 +- 5 files changed, 51 insertions(+), 49 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/date-picker/__tests__/getDateRanges.test.ts b/packages/core/client/src/schema-component/antd/date-picker/__tests__/getDateRanges.test.ts index c2638ed40d..a18c6502ce 100644 --- a/packages/core/client/src/schema-component/antd/date-picker/__tests__/getDateRanges.test.ts +++ b/packages/core/client/src/schema-component/antd/date-picker/__tests__/getDateRanges.test.ts @@ -144,127 +144,127 @@ describe('getDateRanges: fieldOperator is $dateBetween', () => { const dateRanges = getDateRanges(); it('today', () => { - const [start, end] = dateRanges.today({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.today({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('day').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('day').toISOString()); }); test('yesterday', () => { - const [start, end] = dateRanges.yesterday({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.yesterday({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(dayjs(start).isSame(dayjs().subtract(1, 'day'), 'day')).toBe(true); expect(dayjs(end).isSame(dayjs().subtract(1, 'day'), 'day')).toBe(true); }); test('tomorrow', () => { - const [start, end] = dateRanges.tomorrow({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.tomorrow({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(dayjs(start).isSame(dayjs().add(1, 'day'), 'day')).toBe(true); expect(dayjs(end).isSame(dayjs().add(1, 'day'), 'day')).toBe(true); }); it('lastWeek', () => { - const [start, end] = dateRanges.lastWeek({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.lastWeek({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'week').startOf('isoWeek').toISOString()); expect(end.toISOString()).toBe(dayjs().add(-1, 'week').endOf('isoWeek').toISOString()); }); it('thisWeek', () => { - const [start, end] = dateRanges.thisWeek({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.thisWeek({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('isoWeek').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('isoWeek').toISOString()); }); it('nextWeek', () => { - const [start, end] = dateRanges.nextWeek({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.nextWeek({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'week').startOf('isoWeek').toISOString()); expect(end.toISOString()).toBe(dayjs().add(1, 'week').endOf('isoWeek').toISOString()); }); it('lastMonth', () => { - const [start, end] = dateRanges.lastMonth({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.lastMonth({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'month').startOf('month').toISOString()); expect(end.toISOString()).toBe(dayjs().add(-1, 'month').endOf('month').toISOString()); }); it('thisMonth', () => { - const [start, end] = dateRanges.thisMonth({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.thisMonth({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('month').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('month').toISOString()); }); it('nextMonth', () => { - const [start, end] = dateRanges.nextMonth({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.nextMonth({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'month').startOf('month').toISOString()); expect(end.toISOString()).toBe(dayjs().add(1, 'month').endOf('month').toISOString()); }); it('lastQuarter', () => { - const [start, end] = dateRanges.lastQuarter({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.lastQuarter({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'quarter').startOf('quarter').toISOString()); expect(end.toISOString()).toBe(dayjs().add(-1, 'quarter').endOf('quarter').toISOString()); }); it('thisQuarter', () => { - const [start, end] = dateRanges.thisQuarter({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.thisQuarter({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('quarter').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('quarter').toISOString()); }); it('nextQuarter', () => { - const [start, end] = dateRanges.nextQuarter({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.nextQuarter({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'quarter').startOf('quarter').toISOString()); expect(end.toISOString()).toBe(dayjs().add(1, 'quarter').endOf('quarter').toISOString()); }); it('lastYear', () => { - const [start, end] = dateRanges.lastYear({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.lastYear({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'year').startOf('year').toISOString()); expect(end.toISOString()).toBe(dayjs().add(-1, 'year').endOf('year').toISOString()); }); it('thisYear', () => { - const [start, end] = dateRanges.thisYear({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.thisYear({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('year').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('year').toISOString()); }); it('nextYear', () => { - const [start, end] = dateRanges.nextYear({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.nextYear({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'year').startOf('year').toISOString()); expect(end.toISOString()).toBe(dayjs().add(1, 'year').endOf('year').toISOString()); }); it('last7Days', () => { - const [start, end] = dateRanges.last7Days({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.last7Days({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-6, 'days').startOf('days').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('days').toISOString()); }); it('next7Days', () => { - const [start, end] = dateRanges.next7Days({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.next7Days({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'day').startOf('day').toISOString()); expect(end.toISOString()).toBe(dayjs().add(7, 'days').endOf('days').toISOString()); }); it('last30Days', () => { - const [start, end] = dateRanges.last30Days({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.last30Days({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-29, 'days').startOf('days').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('days').toISOString()); }); it('next30Days', () => { - const [start, end] = dateRanges.next30Days({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.next30Days({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'day').startOf('day').toISOString()); expect(end.toISOString()).toBe(dayjs().add(30, 'days').endOf('days').toISOString()); }); it('last90Days', () => { - const [start, end] = dateRanges.last90Days({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.last90Days({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-89, 'days').startOf('days').toISOString()); expect(end.toISOString()).toBe(dayjs().endOf('days').toISOString()); }); it('next90Days', () => { - const [start, end] = dateRanges.next90Days({ fieldOperator: '$dateBetween' }); + const [start, end] = dateRanges.next90Days({ fieldOperator: '$dateBetween', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'day').startOf('day').toISOString()); expect(end.toISOString()).toBe(dayjs().add(90, 'days').endOf('days').toISOString()); }); @@ -274,107 +274,107 @@ describe('getDateRanges: fieldOperator is not $dateBetween', () => { const dateRanges = getDateRanges(); it('today', () => { - const start = dateRanges.today({ fieldOperator: '$dateOn' }); + const start = dateRanges.today({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('day').toISOString()); }); test('yesterday', () => { - const start = dateRanges.yesterday({ fieldOperator: '$dateOn' }); + const start = dateRanges.yesterday({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(dayjs(start).isSame(dayjs().subtract(1, 'day'), 'day')).toBe(true); }); test('tomorrow', () => { - const start = dateRanges.tomorrow({ fieldOperator: '$dateOn' }); + const start = dateRanges.tomorrow({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(dayjs(start).isSame(dayjs().add(1, 'day'), 'day')).toBe(true); }); it('lastWeek', () => { - const start = dateRanges.lastWeek({ fieldOperator: '$dateOn' }); + const start = dateRanges.lastWeek({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'week').startOf('isoWeek').toISOString()); }); it('thisWeek', () => { - const start = dateRanges.thisWeek({ fieldOperator: '$dateOn' }); + const start = dateRanges.thisWeek({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('isoWeek').toISOString()); }); it('nextWeek', () => { - const start = dateRanges.nextWeek({ fieldOperator: '$dateOn' }); + const start = dateRanges.nextWeek({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'week').startOf('isoWeek').toISOString()); }); it('lastMonth', () => { - const start = dateRanges.lastMonth({ fieldOperator: '$dateOn' }); + const start = dateRanges.lastMonth({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'month').startOf('month').toISOString()); }); it('thisMonth', () => { - const start = dateRanges.thisMonth({ fieldOperator: '$dateOn' }); + const start = dateRanges.thisMonth({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('month').toISOString()); }); it('nextMonth', () => { - const start = dateRanges.nextMonth({ fieldOperator: '$dateOn' }); + const start = dateRanges.nextMonth({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'month').startOf('month').toISOString()); }); it('lastQuarter', () => { - const start = dateRanges.lastQuarter({ fieldOperator: '$dateOn' }); + const start = dateRanges.lastQuarter({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'quarter').startOf('quarter').toISOString()); }); it('thisQuarter', () => { - const start = dateRanges.thisQuarter({ fieldOperator: '$dateOn' }); + const start = dateRanges.thisQuarter({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('quarter').toISOString()); }); it('nextQuarter', () => { - const start = dateRanges.nextQuarter({ fieldOperator: '$dateOn' }); + const start = dateRanges.nextQuarter({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'quarter').startOf('quarter').toISOString()); }); it('lastYear', () => { - const start = dateRanges.lastYear({ fieldOperator: '$dateOn' }); + const start = dateRanges.lastYear({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-1, 'year').startOf('year').toISOString()); }); it('thisYear', () => { - const start = dateRanges.thisYear({ fieldOperator: '$dateOn' }); + const start = dateRanges.thisYear({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().startOf('year').toISOString()); }); it('nextYear', () => { - const start = dateRanges.nextYear({ fieldOperator: '$dateOn' }); + const start = dateRanges.nextYear({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'year').startOf('year').toISOString()); }); it('last7Days', () => { - const start = dateRanges.last7Days({ fieldOperator: '$dateOn' }); + const start = dateRanges.last7Days({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-6, 'days').startOf('days').toISOString()); }); it('next7Days', () => { - const start = dateRanges.next7Days({ fieldOperator: '$dateOn' }); + const start = dateRanges.next7Days({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'day').startOf('day').toISOString()); }); it('last30Days', () => { - const start = dateRanges.last30Days({ fieldOperator: '$dateOn' }); + const start = dateRanges.last30Days({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-29, 'days').startOf('days').toISOString()); }); it('next30Days', () => { - const start = dateRanges.next30Days({ fieldOperator: '$dateOn' }); + const start = dateRanges.next30Days({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'day').startOf('day').toISOString()); }); it('last90Days', () => { - const start = dateRanges.last90Days({ fieldOperator: '$dateOn' }); + const start = dateRanges.last90Days({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(-89, 'days').startOf('days').toISOString()); }); it('next90Days', () => { - const start = dateRanges.next90Days({ fieldOperator: '$dateOn' }); + const start = dateRanges.next90Days({ fieldOperator: '$dateOn', isParsingVariable: true }); expect(start.toISOString()).toBe(dayjs().add(1, 'day').startOf('day').toISOString()); }); }); diff --git a/packages/core/client/src/schema-component/antd/date-picker/util.ts b/packages/core/client/src/schema-component/antd/date-picker/util.ts index 526792466d..790af539ab 100644 --- a/packages/core/client/src/schema-component/antd/date-picker/util.ts +++ b/packages/core/client/src/schema-component/antd/date-picker/util.ts @@ -216,10 +216,10 @@ export const getDateRanges = (props?: { }; }; -function withParams(value: any[], params: { fieldOperator?: string }) { - if (params?.fieldOperator && params.fieldOperator !== '$dateBetween') { - return value[0]; +function withParams(value: any[], params: { fieldOperator?: string; isParsingVariable?: boolean }) { + if (params?.fieldOperator === '$dateBetween' || !params?.isParsingVariable) { + return value; } - return value; + return value[0]; } diff --git a/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts b/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts index 485b721d9b..28344a8da8 100644 --- a/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts +++ b/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts @@ -73,7 +73,7 @@ export const useVariableOptions = ({ targetFieldSchema, }); const { apiTokenSettings } = useAPITokenVariable({ noDisabled }); - const { datetimeSettings } = useDatetimeVariable({ operator, schema: uiSchema, noDisabled, targetFieldSchema }); + const { datetimeSettings } = useDatetimeVariable({ operator, schema: uiSchema, noDisabled: true, targetFieldSchema }); const { currentFormSettings, shouldDisplayCurrentForm } = useCurrentFormVariable({ schema: uiSchema, collectionField, diff --git a/packages/core/client/src/variables/VariablesProvider.tsx b/packages/core/client/src/variables/VariablesProvider.tsx index 8fdda1a896..7ec04f2244 100644 --- a/packages/core/client/src/variables/VariablesProvider.tsx +++ b/packages/core/client/src/variables/VariablesProvider.tsx @@ -185,7 +185,9 @@ const VariablesProvider = ({ children }) => { } } - const _value = compile(_.isFunction(current) ? current({ fieldOperator: options?.fieldOperator }) : current); + const _value = compile( + _.isFunction(current) ? current({ fieldOperator: options?.fieldOperator, isParsingVariable: true }) : current, + ); return { value: _value === undefined ? variableOption.defaultValue : _value, dataSource, diff --git a/packages/core/client/src/variables/__tests__/useVariables.test.tsx b/packages/core/client/src/variables/__tests__/useVariables.test.tsx index fd666a8d28..d658edaf7a 100644 --- a/packages/core/client/src/variables/__tests__/useVariables.test.tsx +++ b/packages/core/client/src/variables/__tests__/useVariables.test.tsx @@ -298,7 +298,7 @@ describe('useVariables', () => { }); await waitFor(async () => { - expect(await result.current.parseVariable('{{ $date.today }}').then(({ value }) => value)).toHaveLength(2); + expect(await result.current.parseVariable('{{ $date.today }}').then(({ value }) => typeof value)).toBe('string'); expect( Array.isArray( await result.current