如果用户清除输入,则防止范围滑块重置

发布于 2025-01-12 11:07:18 字数 499 浏览 3 评论 0原文

我有一个带有输入框的范围滑块。到目前为止,我基本上可以正常工作,但如果用户清除框中的输入,我需要拇指滑块不要在中间重置。如果用户清除输入,我希望滑块保持在当前位置。

如果用户清除并输入

滑块重置

滑块重置最大值

这是 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

Slider reset

Slider reset max value

Here is the CodeSandbox

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

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

发布评论

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

评论(4

颜漓半夏 2025-01-19 11:07:18

这是一个有点高级的解决方案,但您可以使用 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 ^-^

千笙结 2025-01-19 11:07:18

我不清楚你真正想做什么。因为假设最小值是 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.

-柠檬树下少年和吉他 2025-01-19 11:07:18

可以分别控制inputslide的值,这样会方便很多,而且符合Reactstate控制。

代码片段示例

    const [min, setMin] = useState({ input: 0, slide: 0 });
    const [max, setMax] = useState({ input: 100, slide: 100 });

    const handleMaxChange = (e) => {
    const v = parseInt(e.target.value, 10);
    if (v) {
      if (v < min.input && !!min.input) {
        setMax({ input: min.input + 1, slide: min.slide + 1 });
      } else if (v > extremeMax) {
        setMax({ input: extremeMax, slide: extremeMax });
      } else {
        setMax({ input: v, slide: v });
      }
    } else {
      setMax((prev) => ({ ...prev, input: "" }));
    }
  };

完整代码示例

编辑文本字段选择开始

You can control the values of input and slide separately, that will be much more convenient and conforms to React's state control.

Code fragment sample

    const [min, setMin] = useState({ input: 0, slide: 0 });
    const [max, setMax] = useState({ input: 100, slide: 100 });

    const handleMaxChange = (e) => {
    const v = parseInt(e.target.value, 10);
    if (v) {
      if (v < min.input && !!min.input) {
        setMax({ input: min.input + 1, slide: min.slide + 1 });
      } else if (v > extremeMax) {
        setMax({ input: extremeMax, slide: extremeMax });
      } else {
        setMax({ input: v, slide: v });
      }
    } else {
      setMax((prev) => ({ ...prev, input: "" }));
    }
  };

Full code sample

Edit TextField selectionStart

叶落知秋 2025-01-19 11:07:18

您可以像这样简单地设置滑块:

<MultiRangeSlider
      minVal={min ? min : 0}
      maxVal={max ? max : 0}
      ...
/>

CodeSandbox 上的演示。

You can simply setup the slider like this:

<MultiRangeSlider
      minVal={min ? min : 0}
      maxVal={max ? max : 0}
      ...
/>

Demo on CodeSandbox.

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