在 mantine 形式输入上使用自定义 onChange 事件

iam*_*ord 5 reactjs react-hooks react-hook-form mantine

我想做一些响应逻辑,当一个值发生变化时,它会触发表单中的其他值发生变化。

我正在使用 mantine 形式,到目前为止我能想到的最好的方法如下:

const onUserChange = (e) => {
   // form.values.acounts.user contains the previous user value
   // e contains the incoming update to it
   form.setFieldValue('other.property.associated.with.user', e);
 }

 <Select label="User"
              data={users}
              {...form.getInputProps(`accounts.user`)}
              onChange={(e) => {
                onUserChange(e);
                form.getInputProps(`accounts.user`).onChange(e)
              }}
      ></Select>
Run Code Online (Sandbox Code Playgroud)

这种方法“似乎”不错,但我不确定是否有更好的方法。有人以前遇到过这个吗?也许一些简洁的回调语法之类的?

iam*_*ord 0

事实证明,该库在 onchange 方面并没有发挥多大作用,实际上只是通过 form.setFieldValue 设置表单值。只要提供的 onchange 设置值,您就不需要重新引用从“getInputProps”获取的 onchange

更新:根据用例,enhancedInputProps 可能有助于完成此任务:

https://mantine.dev/form/get-input-props/#enhancegetinputprops