1

I have a React app (with React Segment UI), which so far is working fine. I have a login form where, as any login form, I allow it to be auto-filled by either a password manager or the browser. What I would like to do is grab the values in the pre-filled forms, mainly to check if they have already been filled.

How can I do that?

This is currently what my component looks like:

export default class SignIn extends React.PureComponent {
  handleSubmit (evt) {
    // handle submit
  }

  isSignInDisabled () {
    // Would like to check here is the email and password
    // have been prefilled since with this method it won't work
    // in this case.
    return (
      !this.props.email.length ||
      !this.props.password.length
    )
  }

  render () {
    const { email, password } = this.props

    return (
        <Form onSubmit={evt => {
            this.handleSubmit(evt)
        }}>
          <Form.Field>
            <Form.Input
              type='email'
              label='Email'
              value={email}
              onChange={evt => setEmail(evt.target.value)}
            />
          </Form.Field>
            <Form.Field>
              <Form.Input
                type='password'
                label='Password'
                value={password}
                onChange={evt => setPassword(evt.target.value)}
              />
            </Form.Field>,
            <Form.Field>
              <Button type='submit' disabled={this.isSignInDisabled()}>Sign In</Button>
            </Form.Field>
          </Form>
    )
  }
}
theJuls
  • 6,788
  • 14
  • 73
  • 160
  • Possible duplicate of https://stackoverflow.com/questions/11708092/detecting-browser-autofill/11710295 – inaumov17 Jun 13 '18 at 16:50
  • Hum... That address the problem with autofill itself, however I wonder if it is possible to work around it by grabbing the values from the inputs – theJuls Jun 13 '18 at 17:01

1 Answers1

1

To roughly grab the values from input fields you can use ref mechanism.

You should be careful: ref interacts with the real DOM, not the virtual React DOM.

Learn more about ref in the official docs.

class Form extends React.Component {

  handleSubmit = (e) => {
    e.preventDefault();
    console.log('Values are', this.firstName.value, this.lastName.value);
  }
 
  render() {
    return (
      <form className="custom-form" onSubmit={this.handleSubmit}>
        <input
          type="text"
          name="first_name"
          value="John"
          ref={(input) => { this.firstName = input; }}
        />
        <input
          type="text"
          name="last_name"
          value="Doe"
          ref={(input) => { this.lastName = input; }}
        />
        <button type="submit">Submit</button>
      </form>
    );
  }
};

const App = () => (
  <div>
    <Form />
  </div>
);

ReactDOM.render(<App />, document.getElementById('react'));
.custom-form {
  display: block;
  padding: 10px;
}

.custom-form > input {
  display: block;
  margin-top: 10px;
  padding: 5px 10px;
  border: 1px solid #ddd;
  border-radius: 3px;
}

.custom-form > button {
  display: block;
  margin-top: 10px;
  padding: 5px 10px;
  background: #fff;
  border-radius: 3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>
inaumov17
  • 1,329
  • 11
  • 18