Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
[fixed] Input.active for Chrome's autofill
Browse files Browse the repository at this point in the history
  • Loading branch information
appsforartists committed Jun 1, 2015
1 parent 38ab67b commit d868d68
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/components/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ var Input = React.createClass(
"componentDidMount": function () {
// Detect autofill
this.updateStateFromDOM();

// try again shortly in case the autofill didn't happen right away
setTimeout(
this.updateStateFromDOM,
400
);
},

"componentWillReceiveProps": function (nextProps) {
Expand Down Expand Up @@ -191,29 +197,50 @@ var Input = React.createClass(
},

"updateStateFromDOM": function () {
var value = this.refs.input.getDOMNode().value;
var node = this.refs.input && this.refs.input.getDOMNode();

this.setState(
{
"value": value,
}
);

this.updateActiveState();
},

"updateActiveState": function () {
var value = this.state.value;
if (!node)
return;

var value = node.value;

if (!value) {
// For security reasons, we can't get an autofilled value from Chrome;
// however, we can check the `:-webkit-autofill` selector and infer
// `active` accordingly.
//
// Firefox will let you read an autofilled value, so we don't need to sweat
// `:-moz-autofill`.

var autofilled = false;

// Must wrap these tests in a `try` because the browser will throw if it
// doesn't recognize a selector

This comment has been minimized.

try {
autofilled = Boolean(node.parentNode.querySelector(":autofill"));

This comment has been minimized.

Copy link
@mems

mems Jun 1, 2015

I think, you should use autofilled = Boolean(node.matches(":autofill"));

This comment has been minimized.

Copy link
@appsforartists

appsforartists Jun 1, 2015

Author Owner

Thanks @mems. Didn't know about matches.

Fixed in 24d915e.


} catch (error) {
try {
autofilled = Boolean(node.parentNode.querySelector(":-webkit-autofill"));

} catch (error) {}
}
}

this.setState(
{
"active": this.state.focused || Boolean(value && value.trim())
"value": value,
"active": this.state.focused || autofilled || Boolean(value && value.trim())
}
);
},
}
);

// We should probably be smarter about TRANSITION_DURATION, only using it when
// transitioning between active and !active. The current implementation
// transitions color on focus, even if the input is already active. This makes
// the UI feel slow.
Input.TRANSITION_DURATION = ".5s";
Input.PADDING = 8;

Expand Down

0 comments on commit d868d68

Please sign in to comment.