VariableJsonTextArea

VariableJsonTextArea 用来编辑带变量的 JSON 配置。它基于 JsonTextArea,在右上角加了一个变量选择按钮,选中变量后会把 {{ ... }} 表达式插入当前光标位置。

如果 JSON 配置里不需要变量,直接用 JsonTextArea 更简单。

基本用法

import React from 'react';
import { Application, Plugin, VariableJsonTextArea } from '@nocobase/client-v2';
import { Form } from 'antd';

function DemoPage() {
  return (
    <Form
      layout="vertical"
      style={{ maxWidth: 520 }}
      initialValues={{
        config: {
          endpoint: '{{ $env.API_ENDPOINT }}',
          owner: '{{ $user.email }}',
        },
      }}
    >
      <Form.Item name="config" label="Config">
        <VariableJsonTextArea rows={8} json5 namespaces={['$env', '$user']} />
      </Form.Item>
    </Form>
  );
}

class DemoPlugin extends Plugin {
  async load() {
    this.flowEngine.context.defineProperty('$env', {
      value: { API_ENDPOINT: 'https://api.example.com' },
      meta: {
        title: 'Environment variables',
        type: 'object',
        properties: {
          API_ENDPOINT: { title: 'API_ENDPOINT', type: 'string' },
        },
      },
    });

    this.flowEngine.context.defineProperty('$user', {
      value: { email: 'alice@example.com' },
      meta: {
        title: 'Current user',
        type: 'object',
        properties: {
          email: { title: 'Email', type: 'string' },
        },
      },
    });

    this.router.add('root', {
      path: '/',
      element: <DemoPage />,
    });
  }
}

const app = new Application({
  router: { type: 'memory', initialEntries: ['/'] },
  plugins: [DemoPlugin],
});

export default app.getRootComponent();
import { VariableJsonTextArea } from '@nocobase/client-v2';

<VariableJsonTextArea
  json5
  rows={8}
  namespaces={['$env', '$user']}
  value={{ endpoint: '{{ $env.API_ENDPOINT }}' }}
  onChange={(value) => {
    console.log(value);
  }}
/>;

API

VariableJsonTextArea 继承 JsonTextArea 的参数,另外支持:

参数类型说明
namespacesstring[]限定变量选择器里的顶层命名空间
extraNodesMetaTreeNode[]追加局部变量节点
metaTreeMetaTreeNode[] | () => MetaTreeNode[] | Promise<MetaTreeNode[]>完全自定义变量树
delimiters[string, string]变量分隔符,默认 ['{{', '}}']
formatPathToValue(meta) => string | undefined自定义变量路径格式化

相关链接