UNPKG

ghg-react

Version:

A library of React components for my own use.

126 lines (98 loc) 5.16 kB
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React from "react"; import PropTypes from "prop-types"; import ProgressBar from "../ProgressBar"; import EyeIcon from "../EyeIcon"; import TextInput from "../TextInput"; /** Password input with integrated label, quality tips, and show password toggle. */ var PasswordInput = function (_React$Component) { _inherits(PasswordInput, _React$Component); function PasswordInput(props) { _classCallCheck(this, PasswordInput); var _this = _possibleConstructorReturn(this, (PasswordInput.__proto__ || Object.getPrototypeOf(PasswordInput)).call(this, props)); _this.toggleShowPassword = function (event) { _this.setState(function (prevState) { return { showPassword: !prevState.showPassword }; }); if (event) event.preventDefault(); }; _this.state = { showPassword: false }; return _this; } _createClass(PasswordInput, [{ key: "render", value: function render() { var _props = this.props, htmlId = _props.htmlId, value = _props.value, label = _props.label, error = _props.error, onChange = _props.onChange, placeholder = _props.placeholder, maxLength = _props.maxLength, showVisibilityToggle = _props.showVisibilityToggle, quality = _props.quality, props = _objectWithoutProperties(_props, ["htmlId", "value", "label", "error", "onChange", "placeholder", "maxLength", "showVisibilityToggle", "quality"]); var showPassword = this.state.showPassword; return React.createElement( TextInput, Object.assign({ htmlId: htmlId, label: label, placeholder: placeholder, type: showPassword ? "text" : "password", onChange: onChange, value: value, maxLength: maxLength, error: error, required: true }, props), showVisibilityToggle && React.createElement( "a", { href: "", onClick: this.toggleShowPassword, style: { marginLeft: 5 } }, React.createElement(EyeIcon, null) ), value.length > 0 && quality && React.createElement(ProgressBar, { percent: quality, width: 130 }) ); } }]); return PasswordInput; }(React.Component); PasswordInput.propTypes = { /** Unique HTML ID. Used for tying label to HTML input. Handy hook for automated testing. */ htmlId: PropTypes.string.isRequired, /** Input name. Recommend setting this to match object's property so a single change handler can be used by convention.*/ name: PropTypes.string.isRequired, /** Password value */ value: PropTypes.any, /** Input label */ label: PropTypes.string, /** Function called when password input value changes */ onChange: PropTypes.func.isRequired, /** Max password length accepted */ maxLength: PropTypes.number, /** Placeholder displayed when no password is entered */ placeholder: PropTypes.string, /** Set to true to show the toggle for displaying the currently entered password */ showVisibilityToggle: PropTypes.bool, /** Display password quality visually via ProgressBar, accepts a number between 0 and 100 */ quality: PropTypes.number, /** Validation error to display */ error: PropTypes.string }; PasswordInput.defaultProps = { maxLength: 50, showVisibilityToggle: false, label: "Password" }; export default PasswordInput;