Skip to content

Commit

Permalink
Merge pull request #6122 from melloware/PR6119
Browse files Browse the repository at this point in the history
Fix #6119: Password reset feedback meter when value changes
  • Loading branch information
nitrogenous authored Mar 18, 2024
2 parents 4c5a93c + 8082b25 commit c007c7d
Showing 1 changed file with 61 additions and 42 deletions.
103 changes: 61 additions & 42 deletions components/lib/password/Password.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import PrimeReact, { PrimeReactContext, localeOption } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { CSSTransition } from '../csstransition/CSSTransition';
import { ESC_KEY_HANDLING_PRIORITIES, useDisplayOrder, useGlobalOnEscapeKey, useMergeProps, useMountEffect, useOverlayListener, useUnmountEffect } from '../hooks/Hooks';
import { ESC_KEY_HANDLING_PRIORITIES, useDisplayOrder, useGlobalOnEscapeKey, useMergeProps, useMountEffect, useOverlayListener, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { EyeIcon } from '../icons/eye';
import { EyeSlashIcon } from '../icons/eyeslash';
import { InputText } from '../inputtext/InputText';
Expand Down Expand Up @@ -99,6 +99,51 @@ export const Password = React.memo(
}
};

const updateFeedback = (value) => {
if (!props.feedback) {
return false;
}

let label = null;
let meter = null;

switch (testStrength(value)) {
case 1:
label = weakLabel;
meter = {
strength: 'weak',
width: '33.33%'
};
break;

case 2:
label = mediumLabel;
meter = {
strength: 'medium',
width: '66.66%'
};
break;

case 3:
label = strongLabel;
meter = {
strength: 'strong',
width: '100%'
};
break;

default:
label = promptLabel;
meter = null;
break;
}

setMeterState(meter);
setInfoTextState(label);

return true;
};

const onPanelClick = (event) => {
if (props.feedback) {
OverlayService.emit('overlay-click', {
Expand Down Expand Up @@ -173,44 +218,6 @@ export const Password = React.memo(
const keyCode = e.code;

if (props.feedback) {
let value = e.target.value;
let label = null;
let meter = null;

switch (testStrength(value)) {
case 1:
label = weakLabel;
meter = {
strength: 'weak',
width: '33.33%'
};
break;

case 2:
label = mediumLabel;
meter = {
strength: 'medium',
width: '66.66%'
};
break;

case 3:
label = strongLabel;
meter = {
strength: 'strong',
width: '100%'
};
break;

default:
label = promptLabel;
meter = null;
break;
}

setMeterState(meter);
setInfoTextState(label);

if (!!keyCode && keyCode !== 'Escape' && !overlayVisibleState) {
show();
}
Expand All @@ -230,9 +237,17 @@ export const Password = React.memo(
};

const testStrength = (str) => {
if (strongCheckRegExp.current.test(str)) return 3;
else if (mediumCheckRegExp.current.test(str)) return 2;
else if (str.length) return 1;
if (!str || str.length === 0) {
return 0;
}

if (strongCheckRegExp.current.test(str)) {
return 3;
} else if (mediumCheckRegExp.current.test(str)) {
return 2;
} else if (str.length > 0) {
return 1;
}

return 0;
};
Expand Down Expand Up @@ -264,6 +279,10 @@ export const Password = React.memo(
}
}, [isFilled]);

useUpdateEffect(() => {
updateFeedback(props.value);
}, [props.value]);

useMountEffect(() => {
alignOverlay();
});
Expand Down

0 comments on commit c007c7d

Please sign in to comment.