# 配置响应器

- 响应器由 formily 提供，写在 schema 字段的 `x-reactions` 上，用于「监听其他字段 → 改变当前字段」。
- 在 Schema JSON 配置界面中的操作路径：选中表单项 → 属性配置 → 字段属性 → 配置响应器 →
  选择依赖字段 → 填写属性响应器（state）或执行响应器（run）。
- 参数速查同时见 [README.md §响应器中可用的参数](../README.md#响应器中可用的参数)；官方文档见
  [formily Schema 内表达式作用域](https://react.formilyjs.org/api/shared/schema#%E5%86%85%E7%BD%AE%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%BD%9C%E7%94%A8%E5%9F%9F)。

## 1. 两种响应器

| 类型     | schema 位置                 | 用途                                                                            |
| -------- | --------------------------- | ------------------------------------------------------------------------------- |
| 属性响应 | `x-reactions.fulfill.state` | 直接改字段自身状态：`visible` / `hidden` / `none`、`value`、`componentProps` 等 |
| 执行响应 | `x-reactions.fulfill.run`   | 执行一段 JS（异步取数、调用外部函数），通常配合 `$effect` 使用                  |

## 2. scenario：当前所处环境

`list-render` 会把当前场景注入 schemaScope，响应器中可直接读取 `scenario`：

| scenario 取值 | 出现位置              |
| ------------- | --------------------- |
| create        | 新增表单弹窗          |
| edit          | 编辑表单弹窗          |
| detail        | 详情弹层              |
| query         | 列表上方查询表单      |
| table-render  | 表格列渲染 / 卡片渲染 |

```JSON
{
  "x-reactions": {
    "dependencies": [{ "property": "value", "type": "any" }],
    "fulfill": {
      "state": {
        "visible": "{{scenario === 'edit'}}"
      }
    }
  }
}
```

## 3. 可用参数

| 参数名      | 说明                                                                                                   |
| ----------- | ------------------------------------------------------------------------------------------------------ |
| scenario    | 环境参数，当前环境，取值见上表                                                                         |
| $self       | 代表当前字段实例，可以在普通属性表达式中使用，也能在 x-reactions 中使用                                |
| $form       | 代表当前 Form 实例，可以在普通属性表达式中使用，也能在 x-reactions 中使用                              |
| $deps       | 只能在 x-reactions 中的表达式消费，与 x-reactions 定义的 dependencies 对应，数组顺序一致               |
| $values     | 代表顶层表单数据，可以在普通属性表达式中使用，也能在 x-reactions 中使用                                |
| $observable | 用于创建响应式对象，使用方式与 observable 一致                                                         |
| $memo       | 用于创建持久引用数据，使用方式与 autorun.memo 一致                                                     |
| $effect     | 用于响应 autorun 第一次执行的下一个微任务时机与响应 autorun 的 dispose，使用方式与 autorun.effect 一致 |
| $props      | 用于对当前字段实例设置组件 props                                                                       |

## 4. 常见场景

### 4.1 依赖其他字段控制显隐

- 完整步骤与 schema 见 [form-linkage.md](./form-linkage.md#表单项显隐)。

```JSON
{
  "x-reactions": {
    "dependencies": [{ "property": "value", "type": "string", "source": "menuType", "name": "menuType" }],
    "fulfill": {
      "state": {
        "visible": "{{$deps.menuType === \"C\"}}"
      }
    }
  }
}
```

### 4.2 依赖其他字段改变当前字段的值

```JSON
{
  "x-reactions": {
    "dependencies": [{ "property": "value", "type": "string", "source": "test", "name": "test" }],
    "fulfill": {
      "state": {
        "value": "{{$deps.test + 10}}"
      }
    }
  }
}
```

### 4.3 动态取数（远程数据源）

- 通过 `run` 调用 `schemaScope` 中传入的函数，并用 `$effect` 声明依赖，字段卸载时自动回收。
- 完整示例见 [remote-data.md](./remote-data.md) 与 [form-linkage.md](./form-linkage.md#根据其他表单项改变当前-select-数据)。

```JSON
{
  "x-reactions": {
    "dependencies": [{ "property": "value", "type": "string", "source": "test", "name": "test" }],
    "fulfill": {
      "run": "$effect(() => {\n  try {\n    fetchSelectOptions && fetchSelectOptions($self, { test: $deps.test })\n  } catch (e) {}\n}, [$deps.test])\n"
    }
  }
}
```

- Select 通过 `field.dataSource` 配置选项；TreeSelect 等组件的 props 通过 `field.component[1]` 配置。

## 5. 注意事项

- `dependencies` 必须声明被依赖字段，表达式中的 `$deps.xxx` 才可用；`source` / `name` 要与依赖字段的 name 一致。
- 依赖链达到三层及以上时请使用 `display` 而不是 `visible`：3 依赖 2、2 依赖 1 时，
  编辑态赋值 2 不会触发 3 的 `visible` 重新计算，`display` 可以正确响应。
- `run` 中调用外部函数建议包一层 `try / catch`，避免单个响应器抛错中断整个表单渲染。
- 表格列渲染场景（`scenario === "table-render"`）的响应器在列表数据变化时按字段重新计算，
  枚举/字典数据更新后若展示未刷新，可用 `ref.current.forceUpdate()` 强制重渲染。
