import React, { useState } from 'react'; import { useIntl } from '@edx/frontend-platform/i18n'; import { Form, Icon, IconButton, OverlayTrigger, Tooltip, useToggle, } from '@edx/paragon'; import { Check, Remove, Visibility, VisibilityOff, } from '@edx/paragon/icons'; import PropTypes from 'prop-types'; import { LETTER_REGEX, NUMBER_REGEX } from '../data/constants'; import messages from './messages'; const PasswordField = (props) => { const { formatMessage } = useIntl(); const [isPasswordHidden, setHiddenTrue, setHiddenFalse] = useToggle(true); const [showTooltip, setShowTooltip] = useState(false); const handleBlur = (e) => { if (props.handleBlur) { props.handleBlur(e); } setShowTooltip(props.showRequirements && false); }; const handleFocus = (e) => { if (props.handleFocus) { props.handleFocus(e); } setTimeout(() => setShowTooltip(props.showRequirements && true), 150); }; const HideButton = ( ); const ShowButton = ( ); const placement = window.innerWidth < 768 ? 'top' : 'left'; const tooltip = ( {LETTER_REGEX.test(props.value) ? : } {formatMessage(messages['one.letter'])} {NUMBER_REGEX.test(props.value) ? : } {formatMessage(messages['one.number'])} {props.value.length >= 8 ? : } {formatMessage(messages['eight.characters'])} ); return ( {props.errorMessage !== '' && ( {props.errorMessage} {formatMessage(messages['password.sr.only.helping.text'])} )} ); }; PasswordField.defaultProps = { borderClass: '', errorMessage: '', handleBlur: null, handleFocus: null, handleChange: () => {}, showRequirements: true, autoComplete: null, }; PasswordField.propTypes = { borderClass: PropTypes.string, errorMessage: PropTypes.string, floatingLabel: PropTypes.string.isRequired, handleBlur: PropTypes.func, handleFocus: PropTypes.func, handleChange: PropTypes.func, name: PropTypes.string.isRequired, showRequirements: PropTypes.bool, value: PropTypes.string.isRequired, autoComplete: PropTypes.string, }; export default PasswordField;