Skip to content

Commit

Permalink
fix(variable): compat $date (nocobase#2971)
Browse files Browse the repository at this point in the history
* fix(variable): compat $date

* chore: update tests
  • Loading branch information
zhangzhonghe authored Nov 5, 2023
1 parent b109a57 commit bafb03a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/core/client/src/variables/__tests__/useVariables.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ const useBuiltInVariables = () => {
name: '$nDate',
ctx: dateVars,
},
/**
* @deprecated
* 兼容旧版本的 `$date` 变量,新版本已弃用
*/
{
name: '$date',
ctx: dateVars,
},
/**
* @deprecated
* 兼容旧版本的 `$system` 变量,新版本已弃用
Expand Down
14 changes: 14 additions & 0 deletions packages/core/client/src/variables/hooks/useLocalVariables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -31,6 +32,7 @@ const useLocalVariables = (props?: Props) => {
}

return useMemo(() => {
const dateVars = getDateRanges();
return (
[
/**
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bafb03a

Please sign in to comment.