diff --git a/src/shared/components/layout/tags/Static/shared.tsx b/src/shared/components/layout/tags/Static/shared.tsx new file mode 100644 index 000000000..d373cdbd9 --- /dev/null +++ b/src/shared/components/layout/tags/Static/shared.tsx @@ -0,0 +1,45 @@ +import React, { useContext, useRef } from 'react'; +import { TagComponent } from '../type'; +import { LayoutStateContext } from '../../context/LayoutStateContext'; +import { StateActionType } from '../../types'; +import _isEqual from 'lodash/isEqual'; +import _isString from 'lodash/isString'; +import _isNil from 'lodash/isNil'; +import { parseDataText } from '../../processor'; +import { TMemo } from '@shared/components/TMemo'; + +interface TagProps { + name: string; + value: any; +} +export const TagStaticShared: TagComponent = TMemo((props) => { + const context = useContext(LayoutStateContext); + const isRunRef = useRef(false); + + if (isRunRef.current === false) { + isRunRef.current = true; + + if (_isString(props.value)) { + // 需要处理数据 + try { + const realVal = parseDataText(`{{(${props.value})}}`, context); + + context.dispatch({ + type: StateActionType.SetGlobal, + payload: { + name: props.name, + value: realVal, + }, + }); + } catch (e) { + // 不处理解析错误的变量 + console.warn('Cannot parse attr:', props.name, props.value, e); + } + + return; + } + } + + return null; +}); +TagStaticShared.displayName = 'TagStaticShared'; diff --git a/src/shared/components/layout/tags/__all__.ts b/src/shared/components/layout/tags/__all__.ts index bd03eaf22..83544e8b1 100644 --- a/src/shared/components/layout/tags/__all__.ts +++ b/src/shared/components/layout/tags/__all__.ts @@ -26,6 +26,7 @@ import { TagInputNumberEdit } from './InputNumber/edit'; import { TagStyledShared } from './Styled/shared'; import { TagScriptShared } from './Script/shared'; import { TagForEachShared } from './ForEach/shared'; +import { TagStaticShared } from './Static/shared'; // 展示数据组件 registerTag('Template', TagTemplateShared); @@ -70,6 +71,8 @@ registerTag('Define', TagDefineShared); registerTag('Var', TagVarShared); +registerTag('Static', TagStaticShared); + registerTag('Computed', TagComputedShared); registerTag('Function', TagFunctionShared);