如果用户清除输入,则防止范围滑块重置
我有一个带有输入框的范围滑块。到目前为止,我基本上可以正常工作,但如果用户清除框中的输入,我需要拇指滑块不要在中间重置。如果用户清除输入,我希望滑块保持在当前位置。
如果用户清除并输入
这是 CodeSandbox
I have a range slider with input boxes. So far I have it mostly working but I need the thumb sliders to not reset in the middle if the user clears the input from the boxes. If a user clears the input I want the slider to stay at their current position.
Sliders reset in the middle if user clears and input
Here is the CodeSandbox
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个有点高级的解决方案,但您可以使用 lodash 中的 debounce(或编写您自己的 debounce 函数,但使用包代替)
您可以做的是对滑块的用户输入进行 debounce
在输入组件的“onChange”上,对状态中的滑块值进行反跳更新,并检查输入是否为空/未定义/空,然后不要更新状态:)
我要休息一下工作,但如果你需要我提供确切的代码,我可以这样做^-^
This is a bit of an advanced solution, but you can use debouce from lodash (or code your own debounce function, but use packages instead)
What you can do is have a debounce on the user input for the sliders
On the 'onChange' for your input component, debounce updating the slider value in your state, and have a check that if the input is null/undefined/empty, then don't update the state :)
I'm taking a break at work, but if you need me to provide exact code, I can do so ^-^
我不清楚你真正想做什么。因为假设最小值是 40,并且用户使用退格键清除文本框:第一次是 4,第二次按退格键时它将为空。您希望滑块保持在 40 还是 4?
在任何情况下,您都可以保留先前状态的最小/最大值,并且如果文本框的新值为空,则不要更新滑块状态。
it's not clear to me what you really want to do. Because let's say Min value is 40, and user clears the textbox with backspaces: first time, it's 4, the second time backspace is hit it will be empty. Do you want the slider stay at 40 or 4?
in any case, you can hold the value for previous state of min/max and if the new value of textbox is empty, don't update the slider state.
可以分别控制
input
和slide
的值,这样会方便很多,而且符合React
的state控制。
代码片段示例
完整代码示例
You can control the values of
input
andslide
separately, that will be much more convenient and conforms toReact
'sstate
control.Code fragment sample
Full code sample
您可以像这样简单地设置滑块:
CodeSandbox 上的演示。
You can simply setup the slider like this:
Demo on CodeSandbox.