From 7fec05d34af509d63aa7e8664828de3fb1f650fb Mon Sep 17 00:00:00 2001 From: Gimling Date: Thu, 24 Sep 2020 09:56:50 +0800 Subject: [PATCH 1/2] add `hideRoot` and `source` to Json Renderer --- src/renderers/Json.tsx | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/renderers/Json.tsx b/src/renderers/Json.tsx index 3dea2adb9..5b2fcbe7e 100644 --- a/src/renderers/Json.tsx +++ b/src/renderers/Json.tsx @@ -1,10 +1,10 @@ import React from 'react'; -import {Renderer, RendererProps} from '../factory'; +import { Renderer, RendererProps } from '../factory'; import JSONTree from 'react-json-tree'; -import {autobind} from '../utils/helper'; -import {BaseSchema} from '../Schema'; - +import { autobind } from '../utils/helper'; +import { BaseSchema } from '../Schema'; +import { resolveVariable } from '../utils/tpl-builtin'; /** * JSON 数据展示控件。 * 文档:https://baidu.gitee.io/amis/docs/components/json @@ -19,6 +19,16 @@ export interface JsonSchema extends BaseSchema { * 默认展开的级别 */ levelExpand?: number; + + /** + * 是否隐藏根节点 + */ + hideRoot?: boolean; + + /** + * 支持从数据链取值 + */ + source?: string; } export interface JSONProps extends RendererProps, JsonSchema { @@ -26,6 +36,8 @@ export interface JSONProps extends RendererProps, JsonSchema { className?: string; placeholder?: string; jsonTheme: string; + hideRoot?: boolean; + source?: string; } const twilight = { @@ -109,7 +121,9 @@ export class JSONField extends React.Component { static defaultProps: Partial = { placeholder: '-', levelExpand: 1, - jsonTheme: 'twilight' + jsonTheme: 'twilight', + hideRoot: false, + source: '' }; @autobind @@ -130,16 +144,17 @@ export class JSONField extends React.Component { } shouldExpandNode = (keyName: any, data: any, level: any) => { - const {levelExpand} = this.props; + const { levelExpand } = this.props; return level < levelExpand; }; render() { - const {className, value, jsonTheme, classnames: cx} = this.props; + const { className, value, jsonTheme, classnames: cx, hideRoot, source } = this.props; let data = value; - - if (typeof value === 'string') { + if (source !== undefined && source !== '') { + data = resolveVariable(source, this.props.data) + } else if (typeof value === 'string') { try { data = JSON.parse(value); } catch (e) { @@ -158,6 +173,7 @@ export class JSONField extends React.Component { theme={theme} shouldExpandNode={this.shouldExpandNode} valueRenderer={this.valueRenderer} + hideRoot={hideRoot} /> ); @@ -168,4 +184,4 @@ export class JSONField extends React.Component { test: /(^|\/)json$/, name: 'json' }) -export class JSONFieldRenderer extends JSONField {} +export class JSONFieldRenderer extends JSONField { } From a843a60d471fdb6528cdd0169f26b0e0787c6d25 Mon Sep 17 00:00:00 2001 From: Gimling Date: Thu, 24 Sep 2020 11:59:37 +0800 Subject: [PATCH 2/2] change resolveVariable to resolveVariableAndFilter --- src/renderers/Json.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/renderers/Json.tsx b/src/renderers/Json.tsx index 5b2fcbe7e..83c5d6245 100644 --- a/src/renderers/Json.tsx +++ b/src/renderers/Json.tsx @@ -4,7 +4,7 @@ import { Renderer, RendererProps } from '../factory'; import JSONTree from 'react-json-tree'; import { autobind } from '../utils/helper'; import { BaseSchema } from '../Schema'; -import { resolveVariable } from '../utils/tpl-builtin'; +import { resolveVariableAndFilter, isPureVariable } from '../utils/tpl-builtin'; /** * JSON 数据展示控件。 * 文档:https://baidu.gitee.io/amis/docs/components/json @@ -152,8 +152,8 @@ export class JSONField extends React.Component { const { className, value, jsonTheme, classnames: cx, hideRoot, source } = this.props; let data = value; - if (source !== undefined && source !== '') { - data = resolveVariable(source, this.props.data) + if (source !== undefined && isPureVariable(source)) { + data = resolveVariableAndFilter(source, this.props.data, '| raw') } else if (typeof value === 'string') { try { data = JSON.parse(value);