检测React Native中文本输入框中的自动填充。

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

Detect auto fill in a text input on react native

问题

在React Native中是否有可能检测到用户在文本输入框上使用了自动填充功能?

我有一个登录表单,如果他们从钥匙串等处自动填充了字段,我想提交登录。

我想要类似于onAutoFill这样的功能,但似乎并没有类似的东西。

<TextInput onChange={setEmail} autoComplete="email" />
<TextInput onChange={setPassword} autoComplete="password" secureTextEntry={true} />

我尝试过查看onChange事件,但似乎没有任何提示表明更改来自于自动填充。

英文:

Is it possible to detect when a user uses auto fill on a text input in react native.

I have a login form and I want to submit the login if they autofill the fields for example from the keychain.

I would love something like onAutoFill but there doesn't seem to be anything like that.

&lt;TextInput onChange={setEmail} autoComplete=&quot;email&quot; /&gt;
&lt;TextInput onChange={setPassword} autoComplete=&quot;password&quot; secureTextEntry={true} /&gt;

I've tried looking at the onChange event but there doesn't seem to be any hint that a change comes from autofill.

答案1

得分: 0

由于没有本机事件,最终采用了一种变通方法。

在这里,如果先前的值为空且新值至少为最小密码长度,我们将提交表单。

onChangeText={(newValue) => {
  onChange(newValue);

  // 注意:由于我们的密码必须至少为8个字符,如果字段变化了8个字符且之前为空,我们可以假设它是自动填充的
  if (value.length === 0 && newValue.length >= 8) {
    onSubmit();
  }
}}
英文:

Ended up going with a bit of a workaround since there is no native event.

Here we submit the form if the previous value was empty and the new one is atleast the minimum password length.

  onChangeText={(newValue) =&gt; {
    onChange(newValue);

    // NOTE since our passwords must be atleast 8 characters if the field changes by 8 characters and was empty we can assume it was autofilled
    if (value.length === 0 &amp;&amp; newValue.length &gt;= 8) {
      onSubmit();
    }
  }}

huangapple
  • 本文由 发表于 2023年6月15日 04:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477266.html
匿名

发表评论

匿名网友

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

确定