Skip to content

Instantly share code, notes, and snippets.

@aderbas
Last active May 12, 2020 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aderbas/de881e7315b0e289f92bab613de54ed9 to your computer and use it in GitHub Desktop.
Save aderbas/de881e7315b0e289f92bab613de54ed9 to your computer and use it in GitHub Desktop.
Password Input with MDL UI
/**
* Password Input text
* @author: Aderbal Nunes <aderbalnunes@gmail.com>
* @since: 08/05/2020
*
*/
import React from "react"
import {FormControl,InputLabel,InputAdornment,Input} from '@material-ui/core';
import {Visibility,VisibilityOff} from '@material-ui/icons';
import PropTypes from 'prop-types';
// end adorment for toggle password text
const EndAdornment = ({...props}) => {
const {toggle,visibility} = props;
return (
<InputAdornment position="end">
<span style={{cursor: 'pointer'}} onClick={toggle}>
{visibility? <Visibility color="action" /> : <VisibilityOff color="action" />}
</span>
</InputAdornment>
);
};
class InputPassword extends React.PureComponent {
state = {
visibility: false
}
_toggleVisibility = () => {
this.setState(state => ({...state, visibility: !state.visibility}));
};
render(){
const {label,fullWidth,id,value,onChange,name} = this.props;
const {visibility} = this.state;
return (
<FormControl fullWidth={fullWidth||false}>
<InputLabel htmlFor={id}>{label}</InputLabel>
<Input
id={id}
name={name}
type={!visibility?'password':'text'}
value={value}
onChange={onChange}
endAdornment={
<EndAdornment
toggle={this._toggleVisibility}
visibility={visibility}
/>
}
/>
</FormControl>
);
}
}
InputPassword.defaultProps = {
label: 'Password',
id: `${Math.ceil(Math.random()*10000)}-password`,
name: 'password'
}
InputPassword.propTypes = {
label: PropTypes.string.isRequired,
value: PropTypes.string,
onChange: PropTypes.func,
placeholder: PropTypes.string,
name: PropTypes.string,
id: PropTypes.string,
}
export default InputPassword;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment