在 Schema 模式下,提供一个 x-inject-run 字段,值为函数,允许在 ReactiveField 组件中进行注入运行 #3898
MeetzhDing
started this conversation in
Ideas
Replies: 1 comment
-
|
@janryWang 这个提议看看呢?实现起来不复杂,也可以对表单能力有一个较大的增强。 想要做到所有表单相关的业务数据,全都在 formily自身的 Reactive模型里面,还是有点困难的。 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
在 JSX 开发模式中,用户可以随意插入自定义的逻辑,如注册字段生命周期事件,绑定外部数据源等。
但在 Schema 开发模式中,想要注册一段运行时执行逻辑,却不容易做到。
真实场景:
将外部数据源同步到特定Field中。
存量业务逻辑/全局性非表单数据,当数据更新时希望影响表单值。
在Schema模式下,允许注册 Form/Field 的其他响应事件,如 onFieldMount。
实现思路:
在 Schema 中提供一个 x-inject-run 字段,值为函数,接受参数为 Field。
x-inject-run 函数内容的可能性
在 Vue 中,可以使用 watchEffect watch 等 api
在 React 中,可以使用 useEffect 等 api
在 Vue/React 中,都可以使用以上内容来注册外部的数据源,如 redux / rxjs 的 subscribe 等。
一个具体的样例:
https://codesandbox.io/s/formily-vue-schema-x-inject-run-rfc-38z5xl?file=/src/App.vue
这里将相同的逻辑,同时写在了
x-inject-run和x-reactions中,在x-reactions中,使用autorun.memo模拟了x-inject-run逻辑。但这不是一个好的实践,因为这依赖了 autorun.memo 的内部实现,且整体语法上更加繁琐。Beta Was this translation helpful? Give feedback.
All reactions