VariableInput

VariableInput 用来输入字面量和变量表达式。变量来自 flowEngine.context 的 meta tree,比如 $env$user 或插件自己追加的业务变量。

它是单行输入。变量会显示成 pill,适合标题、主题、URL 片段这类短文本。

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

function DemoPage() {
  return (
    <Form layout="vertical" style={{ maxWidth: 520 }}>
      <Form.Item name="subject" label="Subject">
        <VariableInput
          placeholder="Input subject"
          namespaces={['$user', '$env']}
          extraNodes={[
            {
              name: '$resetLink',
              title: 'Reset password link',
              type: 'string',
              paths: ['$resetLink'],
            },
          ]}
        />
      </Form.Item>
    </Form>
  );
}

class DemoPlugin extends Plugin {
  async load() {
    this.flowEngine.context.defineProperty('$user', {
      value: { name: 'Alice', email: 'alice@example.com' },
      meta: {
        title: 'Current user',
        type: 'object',
        properties: {
          name: { title: 'Name', type: 'string' },
          email: { title: 'Email', type: 'string' },
        },
      },
    });

    this.flowEngine.context.defineProperty('$env', {
      value: { SMTP_HOST: 'smtp.example.com', SMTP_PORT: '465' },
      meta: {
        title: 'Environment variables',
        type: 'object',
        properties: {
          SMTP_HOST: { title: 'SMTP_HOST', type: 'string' },
          SMTP_PORT: { title: 'SMTP_PORT', type: 'string' },
        },
      },
    });

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

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

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

<Form.Item name="subject" label={t('Subject')}>
  <VariableInput
    namespaces={['$env']}
    extraNodes={[
      { name: '$resetLink', title: t('Reset password link'), type: 'string', paths: ['$resetLink'] },
    ]}
  />
</Form.Item>;

API

参数类型说明
valuestring当前值
onChange(value: string) => void值变化回调
disabledboolean是否禁用
placeholderstring占位文字
addonBeforeReact.ReactNode前置内容
namespacesstring[]限定可选的顶层命名空间
extraNodesMetaTreeNode[]追加局部变量节点
convertersVariableHybridInputConverters自定义 path 和字符串的转换
delimiters[string, string]变量分隔符,默认 ['{{', '}}']
classNamestring自定义 class
styleReact.CSSProperties自定义样式

分隔符

默认分隔符是 {{}},对应 NocoBase 服务端模板的 HTML 转义输出。如果字段最终按 HTML 渲染,且变量展开后不能被转义,可以传三花括号:

<VariableInput delimiters={['{{{', '}}}']} />
注意

三花括号会让变量按未转义内容输出。只有在确认渲染链路已经处理安全问题时再使用。

相关链接