|
| 1 | +import React from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import ProgressBar from "../ProgressBar"; |
| 4 | +import EyeIcon from "../EyeIcon"; |
| 5 | +import TextInput from "../TextInput"; |
| 6 | + |
| 7 | +/** Password input with integrated label, quality tips, and show password toggle. */ |
| 8 | +class PasswordInput extends React.Component { |
| 9 | + constructor(props) { |
| 10 | + super(props); |
| 11 | + this.state = { |
| 12 | + showPassword: false |
| 13 | + }; |
| 14 | + } |
| 15 | + |
| 16 | + toggleShowPassword = event => { |
| 17 | + this.setState(prevState => { |
| 18 | + return { showPassword: !prevState.showPassword }; |
| 19 | + }); |
| 20 | + if (event) event.preventDefault(); |
| 21 | + }; |
| 22 | + |
| 23 | + render() { |
| 24 | + const { |
| 25 | + htmlId, |
| 26 | + value, |
| 27 | + label, |
| 28 | + error, |
| 29 | + onChange, |
| 30 | + placeholder, |
| 31 | + maxLength, |
| 32 | + showVisibilityToggle, |
| 33 | + quality, |
| 34 | + ...props |
| 35 | + } = this.props; |
| 36 | + const { showPassword } = this.state; |
| 37 | + |
| 38 | + return ( |
| 39 | + <TextInput |
| 40 | + htmlId={htmlId} |
| 41 | + label={label} |
| 42 | + placeholder={placeholder} |
| 43 | + type={showPassword ? "text" : "password"} |
| 44 | + onChange={onChange} |
| 45 | + value={value} |
| 46 | + maxLength={maxLength} |
| 47 | + error={error} |
| 48 | + required |
| 49 | + {...props} |
| 50 | + > |
| 51 | + {showVisibilityToggle && ( |
| 52 | + <a |
| 53 | + href="" |
| 54 | + onClick={this.toggleShowPassword} |
| 55 | + style={{ marginLeft: 5 }} |
| 56 | + > |
| 57 | + <EyeIcon /> |
| 58 | + </a> |
| 59 | + )} |
| 60 | + {value.length > 0 && quality && ( |
| 61 | + <ProgressBar percent={quality} width={130} /> |
| 62 | + )} |
| 63 | + </TextInput> |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +PasswordInput.propTypes = { |
| 69 | + /** Unique HTML ID. Used for tying label to HTML input. Handy hook for automated testing. */ |
| 70 | + htmlId: PropTypes.string.isRequired, |
| 71 | + |
| 72 | + /** Input name. Recommend setting this to match object's property so a single change handler can be used by convention.*/ |
| 73 | + name: PropTypes.string.isRequired, |
| 74 | + |
| 75 | + /** Password value */ |
| 76 | + value: PropTypes.any, |
| 77 | + |
| 78 | + /** Input label */ |
| 79 | + label: PropTypes.string, |
| 80 | + |
| 81 | + /** Function called when password input value changes */ |
| 82 | + onChange: PropTypes.func.isRequired, |
| 83 | + |
| 84 | + /** Max password length accepted */ |
| 85 | + maxLength: PropTypes.number, |
| 86 | + |
| 87 | + /** Placeholder displayed when no password is entered */ |
| 88 | + placeholder: PropTypes.string, |
| 89 | + |
| 90 | + /** Set to true to show the toggle for displaying the currently entered password */ |
| 91 | + showVisibilityToggle: PropTypes.bool, |
| 92 | + |
| 93 | + /** Display password quality visually via ProgressBar, accepts a number between 0 and 100 */ |
| 94 | + quality: PropTypes.number, |
| 95 | + |
| 96 | + /** Validation error to display */ |
| 97 | + error: PropTypes.string |
| 98 | +}; |
| 99 | + |
| 100 | +PasswordInput.defaultProps = { |
| 101 | + maxLength: 50, |
| 102 | + showVisibilityToggle: false, |
| 103 | + label: "Password" |
| 104 | +}; |
| 105 | + |
| 106 | +export default PasswordInput; |
0 commit comments