在滑块中禁用拖动功能,超出特定数值。

huangapple go评论66阅读模式
英文:

Disabling drag functionality in slider beyond certain value

问题

我正在创建一个滑块,其值范围从0到100,但用户只能在2到100之间拖动,0到2之间的空间仍然应该在UI上可见。如何在值达到2时停止拖动功能?

<Slider
   aria-label='DiceRoll'
   defaultValue={50}
   step={1}
   min={0}
   max={100}
   value={
      overUnder ? Number(flippedTargetNumber) : Number(targetNumber)
   }
   marks={marks}
   onChange={handleSelectionChange}>

我尝试使用 min={overUnder ? 2 : 0} max={overUnder ? 98 : 100} 但这只限制了整个滑块,不完全反映我想要做的事情。

英文:

I'm creating a slider that ranges its value from 0 to 100, but users should only be able to drag between 2 to 100, the space between 0 and 2 should still be visible on the UI. How can I stop the dragging functionality once the value reaches 2?

       &lt;Slider
          aria-label=&#39;DiceRoll&#39;
          defaultValue={50}
          step={1}
          min={0}
          max={100}
          value={
            overUnder ? Number(flippedTargetNumber) : Number(targetNumber)
          }
          marks={marks}
          onChange={handleSelectionChange}&gt;

I tried using min={overUnder ? 2 : 0}
max={overUnder ? 98 : 100}

but that just limits the entire slider entirely and doesn't exactly reflect what I'm trying to do.

答案1

得分: 0

保持minmax属性为0和100。
通过handleSelectionChange函数更改用户可以如何操作该值,确保不会将值设定为小于2。

const handleSelectionChange = (value) => {
  if (value > 2 && value <= 100) {
    setTargetNumber(value); // 我假设这是你的设置函数
  }
}
英文:

Keep the min and max props as 0 and 100.
Change how the user can manipulate the value through your handleSelectionChange function, making it not ever setting a value below 2

const handleSelectionChange = (value) =&gt; {
  if (value &gt; 2 &amp;&amp; value &lt;= 100) {
    setTargetNumber(value); // I&#39;m assuming this is your setter
  }
}

</details>



huangapple
  • 本文由 发表于 2023年3月3日 20:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定