Skip to content

Commit

Permalink
fix: fix the password field behaviour (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiyaIshaque authored Jun 28, 2024
1 parent ac35ba1 commit 46e5668
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 35 deletions.
45 changes: 14 additions & 31 deletions src/forms/fields/password-field/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ const PasswordField = forwardRef((props, ref) => {

const [isPasswordHidden, setHiddenTrue, setHiddenFalse] = useToggle(true);
const [showPasswordRequirements, setShowPasswordRequirements] = useState(false);
const [isFieldFocusOut, setFieldFocusOut] = useState(false);

const handleOnBlur = (e) => {
const { name: fieldName, value: fieldValue } = e.target;

if (isFieldFocusOut) {
setShowPasswordRequirements(false);
setFieldFocusOut(false);
}

if (fieldName === props.name && e.relatedTarget?.name === 'passwordIcon') {
return; // Do not run validations on password icon click
}
Expand Down Expand Up @@ -93,6 +100,12 @@ const PasswordField = forwardRef((props, ref) => {
setShowPasswordRequirements(showPasswordTooltip && true);
};

const handleKeyDown = (e) => {
if (e.shiftKey && e.key === 'Tab') {
setFieldFocusOut(true);
}
};

const HideButton = (
<IconButton
name="passwordIcon"
Expand Down Expand Up @@ -159,6 +172,7 @@ const PasswordField = forwardRef((props, ref) => {
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleOnBlur}
onKeyDown={handleKeyDown}
autoComplete="current-password"
trailingElement={isPasswordHidden ? ShowButton : HideButton}
floatingLabel={floatingLabel}
Expand All @@ -175,37 +189,6 @@ const PasswordField = forwardRef((props, ref) => {
{errorMessage}
</Form.Control.Feedback>
)}
{errorMessage && showPasswordTooltip && (
<>
<Form.Control.Feedback
key="letter-check"
className="form-text-size"
hasIcon
feedback-for={name}
type={LETTER_REGEX.test(props.value) ? 'valid' : 'invalid'}
>
{formatMessage(messages.oneLetter)}
</Form.Control.Feedback>
<Form.Control.Feedback
key="number-check"
className="form-text-size"
hasIcon
feedback-for={name}
type={NUMBER_REGEX.test(props.value) ? 'valid' : 'invalid'}
>
{formatMessage(messages.oneNumber)}
</Form.Control.Feedback>
<Form.Control.Feedback
key="characters-check"
className="form-text-size"
hasIcon
feedback-for={name}
type={props.value.length >= 8 ? 'valid' : 'invalid'}
>
{formatMessage(messages.eightCharacters)}
</Form.Control.Feedback>
</>
)}
</Form.Group>
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/forms/fields/password-field/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('PasswordField', () => {
expect(props.handleErrorChange).toHaveBeenCalledTimes(1);
expect(props.handleErrorChange).toHaveBeenCalledWith(
'password',
'Password needs to include:',
'Password criteria has not been met',
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/forms/fields/password-field/messages.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const messages = defineMessages({
},
passwordValidationMessage: {
id: 'password.validation.message',
defaultMessage: 'Password needs to include:',
defaultMessage: 'Password criteria has not been met',
description: 'Error message for empty or invalid password',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const mockStore = configureStore();
const emptyFieldValidation = {
name: 'Full name is required',
email: 'Email is required',
password: 'Password needs to include:',
password: 'Password criteria has not been met',
};

const populateRequiredFields = (
Expand Down
2 changes: 1 addition & 1 deletion src/forms/reset-password-popup/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const messages = defineMessages({
},
passwordValidationMessage: {
id: 'password.validation.message',
defaultMessage: 'Password needs to include:',
defaultMessage: 'Password criteria has not been met',
description: 'Error message for invalid password',
},
passwordDoNotMatch: {
Expand Down

0 comments on commit 46e5668

Please sign in to comment.