diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index 416717cec2..3860f43a0c 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -20,8 +20,8 @@ import { CollectionProvider, useCollection, useCollectionManager } from '../coll import { FilterBlockRecord } from '../filter-provider/FilterProvider'; import { useRecordIndex } from '../record-provider'; import { SharedFilterProvider } from './SharedFilterProvider'; -import { useAssociationNames } from './hooks'; import { useTemplateBlockContext } from './TemplateBlockProvider'; +import { useAssociationNames } from './hooks'; export const BlockResourceContext = createContext(null); export const BlockAssociationContext = createContext(null); @@ -125,7 +125,6 @@ const useResourceAction = (props, opts = {}) => { params['appends'] = appends; } } - console.log(templateFinshed); const result = useRequest( snapshot ? async () => ({ diff --git a/packages/core/client/src/variables/VariablesProvider.tsx b/packages/core/client/src/variables/VariablesProvider.tsx index 3b443b6677..0f0894d019 100644 --- a/packages/core/client/src/variables/VariablesProvider.tsx +++ b/packages/core/client/src/variables/VariablesProvider.tsx @@ -11,7 +11,7 @@ import { filterEmptyValues } from './utils/filterEmptyValues'; import { getAction } from './utils/getAction'; import { getPath } from './utils/getPath'; import { clearRequested, getRequested, hasRequested, stashRequested } from './utils/hasRequested'; -import { REGEX_OF_VARIABLE, isVariable } from './utils/isVariable'; +import { isVariable } from './utils/isVariable'; import { uniq } from './utils/uniq'; export const VariablesContext = createContext(null); @@ -234,9 +234,7 @@ const VariablesProvider = ({ children }) => { let result = null; await onLocalVariablesReady(localVariables, async () => { - const matches = variableString.match(REGEX_OF_VARIABLE); - const path = matches[0].replace(REGEX_OF_VARIABLE, '$1'); - + const path = getPath(variableString); result = getCollectionJoinField(getFieldPath(path)); // 当仅有一个例如 `$user` 这样的字符串时,需要拼一个假的 `collectionField` 返回 diff --git a/packages/core/client/src/variables/__tests__/utils/getPath.test.ts b/packages/core/client/src/variables/__tests__/utils/getPath.test.ts new file mode 100644 index 0000000000..7b93a19b38 --- /dev/null +++ b/packages/core/client/src/variables/__tests__/utils/getPath.test.ts @@ -0,0 +1,5 @@ +import { getPath } from '../../utils/getPath'; + +test('{{ $user.name }} => $user.name', () => { + expect(getPath('{{ $user.name }}')).toBe('$user.name'); +}); diff --git a/packages/core/client/src/variables/__tests__/utils/getVariableName.test.ts b/packages/core/client/src/variables/__tests__/utils/getVariableName.test.ts new file mode 100644 index 0000000000..2c60d1828b --- /dev/null +++ b/packages/core/client/src/variables/__tests__/utils/getVariableName.test.ts @@ -0,0 +1,5 @@ +import { getVariableName } from '../../utils/getVariableName'; + +test('{{ $user.name }} => $user', () => { + expect(getVariableName('{{ $user.name }}')).toBe('$user'); +}); diff --git a/packages/core/client/src/variables/__tests__/utils/isVariable.test.ts b/packages/core/client/src/variables/__tests__/utils/isVariable.test.ts new file mode 100644 index 0000000000..82522ac83e --- /dev/null +++ b/packages/core/client/src/variables/__tests__/utils/isVariable.test.ts @@ -0,0 +1,29 @@ +import { isVariable } from '../../utils/isVariable'; + +it('should return false for a string with only one opening brace', () => { + expect(isVariable('{variable}}')).toBe(false); +}); + +it('should return false for a string with only one closing brace', () => { + expect(isVariable('{{variable}')).toBe(false); +}); + +it('should return false for a string with no braces', () => { + expect(isVariable('variable')).toBe(false); +}); + +it('should return true for a string with a valid variable', () => { + expect(isVariable('{{variable}}')).toBe(true); +}); + +it('should return true for a string with a valid variable "{{ $nRecord.cc-cc }}"', () => { + expect(isVariable('{{ $nRecord.cc-cc }}')).toBe(true); +}); + +it('should return true for a string with a valid variable "{{ $nRecord._name }}"', () => { + expect(isVariable('{{ $nRecord._name }}')).toBe(true); +}); + +it('should return true for a string with a valid variable " {{ $nRecord.name }} "', () => { + expect(isVariable(' {{ $nRecord.name }} ')).toBe(true); +}); diff --git a/packages/core/client/src/variables/utils/isVariable.tsx b/packages/core/client/src/variables/utils/isVariable.tsx index 2dca20632a..f21cb75134 100644 --- a/packages/core/client/src/variables/utils/isVariable.tsx +++ b/packages/core/client/src/variables/utils/isVariable.tsx @@ -1,4 +1,4 @@ -export const REGEX_OF_VARIABLE = /\{\{\s*([a-zA-Z0-9_$.]+?)\s*\}\}/g; +export const REGEX_OF_VARIABLE = /\{\{\s*([a-zA-Z0-9_$-.]+?)\s*\}\}/g; export const isVariable = (str: unknown) => { if (typeof str !== 'string') {