简体   繁体   English

React 受限输入字段(例如具有最小约束的数字)不允许您轻松输入值

[英]React constrained input fields (e.g. number with min constraint) doesn't allow you to type in value comfortably

Consider this simple react component that has an input field, which I'd like to constrain to have a minimum value of 20:考虑这个具有输入字段的简单反应组件,我想将其限制为最小值为 20:

class InputTest extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      val: 20,
    };
  }

  valChange = event => {
    let newVal = event.target.value;
    if (newVal >= 20) {
      this.setState({
        val: newVal,
      });
    }
  };

  render() {
    return (
      <div>
        <input
          type={'number'}
          value={this.state.val}
          onChange={this.valChange}
        />
      </div>
    );
  }
}

Now everything more or less works fine with this, unless I try to type in a brand new number into the field.现在一切或多或少都可以正常工作,除非我尝试在该字段中输入一个全新的数字。

eg if I want to select the number currently in the field and overwrite it with lets say 243.例如,如果我想选择当前字段中的数字并用 243 覆盖它。

As I highlight the number and press the "2" key, the react component rejects number 2 as it is smaller then 20, and I am unable to comfortably type the number in.当我突出显示数字并按下“2”键时,反应组件拒绝数字 2,因为它小于 20,我无法轻松输入数字。

I am wondering if there already exists some kind of a solution to this.我想知道是否已经存在某种解决方案。

If not I could probably build something with some kind of a delayed constraint check.如果不是,我可能可以通过某种延迟约束检查来构建一些东西。

EDIT: I should have mentioned this, but I only decided to add the most simple example to make things clearer.编辑:我应该提到这一点,但我只是决定添加最简单的例子来使事情更清楚。 In my case I also had a slider bound to the input field, so I'd want change the slider position as the value inside input changes and vice versa.在我的情况下,我还有一个滑块绑定到输入字段,所以我想随着输入中的值的变化而改变滑块的位置,反之亦然。 So simply notifying the user won't work, as the slider's min and max values are what's constraining the input.所以简单地通知用户是行不通的,因为滑块的最小值和最大值限制了输入。

I ended up using a debounce function to delay the slider position change.我最终使用了去抖动功能来延迟滑块位置的变化。

Wrap your input inside a form and add the min and ( max if you want) attributes.将您的input包裹在一个form并添加min和(如果需要,则为max )属性。 Press Enter or Submit to see the notification (You can remove the button if you don't want it)EnterSubmit以查看通知(如果您不想要该按钮,可以删除该按钮)

 <form> <input type = "number" min = 20 /> <input type = "submit" value = "submit" /> </form>

And remove the condition in your valChange function you don't need it anymore.并删除您不再需要的valChange函数中的条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当值既不是数字也不是字符串类型时,Number() 是否显式调用 toString()? (例如功能) - Does Number() explicitly call toString() when value is neither number nor string type? (e.g. function) TRPC 不会对传递的配置/变量做出反应(例如:enabled: false) - TRPC doesn't react to config / variables passed (e.g.: enabled : false) Vue.js:如果值为空,则不呈现属性(例如:to=&quot;&quot; 或 type=&quot;&quot;) - Vue.js: Don't render attributes if value is empty (e.g.: to="" or type="") React Typescript 泛型类型重载(例如 useState) - React Typescript Generic Type Overloading (e.g. useState) 有关DOM元素的哪些信息将允许JavaScript(有些)唯一地识别它? (例如,当它没有`id`时) - What information about a DOM element would allow JavaScript to identify it (somewhat) uniquely? (e.g. when it doesn't have `id`) 您可以输入文本,但可以键入文本,但不允许删除文本吗? - Can you make a text input that allows you to type text, but doesn't allow you to delete text? 如何在输入字段上禁用特定控件(例如Tab键)的复制/粘贴? - How to disable copy/paste of specific control(for e.g. Tab key) on input fields? 在kebab情况下访问JSON字段(例如,某些值) - Access JSON fields in kebab case (e.g. some-value) 以简写形式呈现用户输入的字节数(例如“kB”、“MB”、“GB”等) - Render number of bytes from user input in short notation (e.g. 'kB', 'MB', 'GB', etc) 如何更改` <input> 来自wkWebview / webView中Objective-C的元素属性(例如值) - How to Change `<input>` element attributes(e.g. value) from objective-C in wkWebview/webView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM