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 9a8d2b66a3..e31f9b044e 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 @@ -254,12 +254,12 @@ 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]; } export function inferPickerType(dateString: string): 'year' | 'month' | 'quarter' | 'date' { 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 a441cec2e1..263bd14067 100644 --- a/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts +++ b/packages/core/client/src/schema-settings/VariableInput/hooks/useVariableOptions.ts @@ -75,7 +75,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 05cd96546e..066cdfcc63 100644 --- a/packages/core/client/src/variables/VariablesProvider.tsx +++ b/packages/core/client/src/variables/VariablesProvider.tsx @@ -185,7 +185,9 @@ const VariablesProvider = ({ children, filterVariables }: any) => { } } - 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