如何清除MUI自动完成至初始状态

发布于 2025-02-08 20:54:36 字数 460 浏览 2 评论 0原文

我想重置/清除我的MUI自动完成组件。 我有两个带有{label:string,value:string}的模型的模型,如果第一个更改其值,那么我想清除第二个一。

此外 - 我正在使用setValue进行react-hook-form。我在此方法中用作值{label:'',value:''},但会导致警告说,在我的新选项中,没有这样的选择(我认为这是次要的问题),但它不会重置第二个自动完成输入,而是部分重置。我仍然看到x清除值。我在null中使用了setValue中的值,但也没有防滑输入。

我要实现的是 - 在第一个输入上选择一些选项,我想重置第二个输入,例如单击x做。是否可以 ?

干杯!

I would like to reset/clear my MUI Autocomplete component.
I have two of them with model like { label: string, value: string } and if first will change its value then I would like to clear the second one since second will get options by selected value in first one.

Moreover - I am using react-hook-form with setValue. I use as value in this method { label: '', value: '' } but it causes warning that in my new sort of options there is no such option to select (this is minor issue I think) but it does not reset second Autocomplete input but partially. I still see X to clear value. I used null as value in setValue but it does not cleat input as well.

What I want to achieve is - selecting some option on first input I would like to reset second input like clicking on X does. Is it possible ?

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2025-02-15 20:54:36

我发现是什么原因引起了我上面描述的问题。这是因为用clearonblur = {false} props声明了我的代码库中的自动完成。当我执行setValue(autoCompleteFieldName,null,null)通过react> react-hook-form时,它阻止了我的自动完成的价值。我希望它能对一个奇怪的问题有帮助,因为我根本没有与输入进行互动以调用onblur事件。干杯!

I found what caused the issue I describe above. It was because Autocomplete in my codebase was declared with clearOnBlur={false} props. It prevented to clear value of my Autocomplete when I was doing setValue(autocompleteFieldName, null) via react-hook-form. I hope it helps someone with the same strange issue since I was not interacting with input at all to invoke onBlur event. Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文