文本输入自动完成需要2次点击。

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

Text input autocomplete forces 2 clicks

问题

这是您提供的React登录表单的部分代码。在这个表单中,问题出现在Chrome自动填充的时候。首先,当点击用户名字段时,您会得到Chrome的普通自动填充,而不是建议自动填充密码。只有在填写用户名字段后,再次点击时,您才会得到填写密码的建议,但这也会导致密码输入失去所有样式。

英文:

I have a very simple login form in React.

I don't think the React component code itself is the issue, but I'll add it here for clarity:

import React, { ChangeEvent, useState, useEffect } from "react"
import axios from "axios";
import { useNavigate } from 'react-router-dom';
import classnames from "classnames";

import "./Login.scss";

export default function Login() {
    const [user, setUser] = useState('');
    const [password, setPassword] = useState('');
    const [canSubmit, setCanSubmit] = useState(false);
    const navigate = useNavigate();
    useEffect(() => {
        setCanSubmit(!!user && !!password)
    }, [user, password])

    const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
        const { name, value } = event.target;

        if (name === "username") {
            setUser(value);
        } else {
            setPassword(value);
        }
    };

    const onSubmit = async e => {
        e.preventDefault();

        if (!canSubmit) {
            return;
        }

        try {
            await axios.post("/api/admin/login", {
                username: user,
                password
            });
        } catch (error) {
            alert("lol " + error);
            return;
        }

        navigate("/admin");
    };

    return <form onSubmit={onSubmit} autoComplete="on">
        <div className="login flex-column flex-align-items-center flex-align-self-center">
            <div className="title">LOGIN</div>

            <div className="field">
                <label htmlFor="username">Username</label>
                <input type="text" placeholder="Enter your username" value={user} name="username" onChange={handleChange} />
            </div>
            <div className="field">
                <label htmlFor="password">Password</label>
                <input type="password" placeholder="Enter your password" value={password} name="password" onChange={handleChange} />
            </div>

            <button type="submit" className={classnames({ 'disabled': !canSubmit })}>Login</button>
        </div>
    </form>
}

The inputs are styles like this: (it's SASS)

input {
  background-color: transparent;
  outline: none;
  border: none;
  padding-bottom: 10px;
  padding-left: 1.5rem;
  font-size: 1.3rem;

  box-sizing: border-box;
  box-shadow: inset 0px -3px 0px 0px rgba(187, 187, 187, 0.2);
  transition: box-shadow 0.2s ease-in;

  &:focus {
    box-shadow: inset 0px -3px 0px 0px rgba(34, 193, 195, 0.7);
    outline: none;
  }

  &:-webkit-autofill,
  &:-webkit-autofill:hover,
  &:-webkit-autofill:focus {
    color: white !important;
    background-color: #12130f !important;
  }
}

Initially, the form looks fine:

文本输入自动完成需要2次点击。

If I just type normally into the inputs, it looks great:

文本输入自动完成需要2次点击。

When clicking the username field, I'm getting Chrome's "normal" autocomplete, instead of a suggestion to autofill the password:

文本输入自动完成需要2次点击。

Only after the username field is filled, when I click again, I get the actual suggestion to fill in the password, which when clicked also causes the password input to lose all its styling:

文本输入自动完成需要2次点击。

文本输入自动完成需要2次点击。

答案1

得分: 1

Chrome没有将您的“用户名”和“密码”字段识别为凭据字段,因为您没有将“autocomplete”属性的值设置为“username”以用于“username”输入,也没有设置为“current-password”以用于“password”输入。

<input type="text" placeholder="输入您的用户名" value={user} name="username" onChange={handleChange} autoComplete="username" />

<input type="password" placeholder="输入您的密码" value={password} name="password" onChange={handleChange} autoComplete="current-password" />

另外,您可以通过使用CSS选择器来定位自动填充输入字段并应用所需的样式来修复Chrome对表单字段样式的忽视。

input:-webkit-autofill {
  box-shadow: inset 0px -3px 0px 0px rgba(34, 193, 195, 0.7);
}
英文:

Chrome is not recognizing your "username" and "password" fields as credentials fields because you did not add the autocomplete attribute with the value "username" to the "username" input and "current-password" to the "password" input.

&lt;input type=&quot;text&quot; placeholder=&quot;Enter your username&quot; value={user} name=&quot;username&quot; onChange={handleChange} autoComplete=&quot;username&quot; /&gt;

&lt;input type=&quot;password&quot; placeholder=&quot;Enter your password&quot; value={password} name=&quot;password&quot; onChange={handleChange} autoComplete=&quot;current-password&quot; /&gt;

BTW you can fix Chrome's ignorance for your form field styles by using CSS selector to target the autofilled input fields and apply the desired styles.

input:-webkit-autofill {
  box-shadow: inset 0px -3px 0px 0px rgba(34, 193, 195, 0.7);
}

huangapple
  • 本文由 发表于 2023年4月17日 22:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036308.html
匿名

发表评论

匿名网友

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

确定