Skip to content

Commit

Permalink
feat(layout): 增加了Static标签专门用于处理静态数据
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Mar 1, 2020
1 parent 463e66d commit e52a4b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/shared/components/layout/tags/Static/shared.tsx
Original file line number Diff line number Diff line change
@@ -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<TagProps> = 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';
3 changes: 3 additions & 0 deletions src/shared/components/layout/tags/__all__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -70,6 +71,8 @@ registerTag('Define', TagDefineShared);

registerTag('Var', TagVarShared);

registerTag('Static', TagStaticShared);

registerTag('Computed', TagComputedShared);

registerTag('Function', TagFunctionShared);
Expand Down

0 comments on commit e52a4b2

Please sign in to comment.