Skip to content

Commit

Permalink
Merge pull request #1482 from clerkinc/jr/password_changes
Browse files Browse the repository at this point in the history
USR-135 fix(clerk-js): Address spacing issues when password feedback message changes
  • Loading branch information
raptisj authored Jul 20, 2023
2 parents 2e11664 + d2bba46 commit 86f2fbc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changeset/tidy-worms-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': patch
---

- Address spacing issues when password feedback message changes
- Add a full stop in form feedback(errors and warnings) when needed
33 changes: 18 additions & 15 deletions packages/clerk-js/src/ui/elements/FormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,25 @@ function useFormTextAnimation() {
};
}

type CalculateConfigProps = {
recalculate?: string | undefined;
};
type Px = number;
const useCalculateErrorTextHeight = () => {
const useCalculateErrorTextHeight = (config: CalculateConfigProps = {}) => {
const [height, setHeight] = useState<Px>(24);

const calculateHeight = useCallback((element: HTMLElement | null) => {
if (element) {
const computedStyles = getComputedStyle(element);
const marginTop = parseInt(computedStyles.marginTop.replace('px', ''));
const calculateHeight = useCallback(
(element: HTMLElement | null) => {
if (element) {
const computedStyles = getComputedStyle(element);
const marginTop = parseInt(computedStyles.marginTop.replace('px', ''));

setHeight(prevHeight => {
const newHeight = 1.5 * marginTop + element.scrollHeight;
if (prevHeight < newHeight) {
return newHeight;
}
return prevHeight;
});
}
}, []);
const newHeight = 1.1 * marginTop + element.scrollHeight;
setHeight(newHeight);
}
},
[config?.recalculate],
);
return {
height,
calculateHeight,
Expand All @@ -136,7 +137,9 @@ export const FormFeedback = (props: FormFeedbackProps) => {
const messageToDisplay = informationMessage || successMessage || errorMessage || warningMessage;
const isSomeMessageVisible = !!messageToDisplay;

const { calculateHeight, height } = useCalculateErrorTextHeight();
const { calculateHeight, height } = useCalculateErrorTextHeight({
recalculate: warningMessage || errorMessage || informationMessage,
});
const { getFormTextAnimation } = useFormTextAnimation();
const defaultElementDescriptors = {
error: descriptors.formFieldErrorText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ describe('usePasswordComplexity', () => {
result.current.getComplexity('@aP');
});

expect(result.current.failedValidationsText).toBe('Your password must contain 8 or more characters and a number');
expect(result.current.failedValidationsText).toBe('Your password must contain 8 or more characters and a number.');

await act(() => {
result.current.getComplexity('aP');
});

expect(result.current.failedValidationsText).toBe(
'Your password must contain 8 or more characters, a special character, and a number',
'Your password must contain 8 or more characters, a special character, and a number.',
);
});
});
16 changes: 15 additions & 1 deletion packages/clerk-js/src/ui/hooks/usePasswordComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ const errorMessages = {
require_special_char: 'unstable__errors.passwordComplexity.requireSpecialCharacter',
};

const addFullStop = (string: string | undefined) => {
if (!string) {
return '';
}

if (string.charAt(string.length - 1) === '.') {
return string;
}

return string + '.';
};

export const generateErrorTextUtil = ({
config,
failedValidations,
Expand Down Expand Up @@ -84,7 +96,9 @@ export const generateErrorTextUtil = ({
} else {
messageWithPrefix = messages.join(', ');
}
return `${t(localizationKeys('unstable__errors.passwordComplexity.sentencePrefix'))} ${messageWithPrefix}`;
return addFullStop(
`${t(localizationKeys('unstable__errors.passwordComplexity.sentencePrefix'))} ${messageWithPrefix}`,
);
};

const validate = (password: string, config: UsePasswordComplexityConfig): ComplexityErrors => {
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/ui/primitives/FormErrorText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { applyVariants } = createVariants(theme => ({
marginTop: theme.sizes.$2,
animation: `${animations.textInSmall} ${theme.transitionDuration.$fast}`,
display: 'flex',
gap: theme.sizes.$2,
gap: theme.sizes.$1,
position: 'absolute',
top: '0',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/ui/primitives/FormSuccessText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const { applyVariants } = createVariants(theme => ({
marginTop: theme.sizes.$2,
animation: `${animations.textInSmall} ${theme.transitionDuration.$fast}`,
display: 'flex',
gap: theme.sizes.$2,
gap: theme.sizes.$1,
position: 'absolute',
top: '0',
},
Expand Down

0 comments on commit 86f2fbc

Please sign in to comment.