Created
July 26, 2016 08:32
-
-
Save adamtal3/403e8ebec93104e2870e301eea96523e to your computer and use it in GitHub Desktop.
Material-UI + redux-form chrome's auto-fill / auto complete patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Login extends Component { | |
// This was tested under chrome only. It's ugly, but it works. Code was modified a bit for simplicity so it might not work out of the box. | |
componentDidMount() { | |
// Fix chrome auto-fill | |
setTimeout(() => { | |
const { change, dispatch }= this.props; | |
if (this.refs.usernameAutoFill.value && ! this.refs.username.value) { | |
dispatch(change('username', this.refs.usernameAutoFill.value)); | |
} | |
if (this.refs.passwordAutoFill.value && !this.refs.password.value) { | |
dispatch(change('password', this.refs.passwordAutoFill.value)); | |
} | |
}, 500); | |
} | |
render() { | |
const styles = { | |
autofill: { | |
height: 0, | |
width: '1px', | |
position: 'absolute', | |
left: 0, | |
top: 0 | |
} | |
}; | |
return ( | |
<div> | |
<form style={styles.autofill}> | |
<input type="text" ref="usernameAutoFill" name="usernameAutoFill" /> | |
<input type="password" ref="passwordAutoFill" name="passwordAutoFill" /> | |
</form> | |
<form autocomplete="off"> | |
<div style={styles.autofill}><input type="text" name="fakeusername" /><input type="password" name="fakepassword" /></div> | |
<Field name="username" component={() => <TextField ref="username" name="username" floatingLabelText="Username" />}/> | |
<Field name="password" component={() => <TextField ref="password" name="password" type="password" floatingLabelText="Password" />}/> | |
<RaisedButton primary={true} label="Login" type="submit"/> | |
</form> | |
</div> | |
) | |
} | |
} | |
export default { | |
Form: reduxForm({ | |
form: 'Login' | |
})(Login) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment