import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; import { Form, IconButton, useToggle, Tooltip, OverlayTrigger, Icon, } from '@edx/paragon'; import { Check, Remove, Visibility, VisibilityOff, } from '@edx/paragon/icons'; import messages from './messages'; import { LETTER_REGEX, NUMBER_REGEX } from '../data/constants'; const PasswordField = (props) => { const { formatMessage } = props.intl; 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, }; PasswordField.propTypes = { borderClass: PropTypes.string, errorMessage: PropTypes.string, floatingLabel: PropTypes.string.isRequired, handleBlur: PropTypes.func, handleFocus: PropTypes.func, handleChange: PropTypes.func, intl: intlShape.isRequired, name: PropTypes.string.isRequired, showRequirements: PropTypes.bool, value: PropTypes.string.isRequired, }; export default injectIntl(PasswordField);