fix: username input

fix/username
Arik Chakma 4 months ago
parent 48b636b145
commit 159bbd903f
  1. 14
      src/components/UpdateProfile/ProfileUsername.tsx

@ -57,6 +57,8 @@ export function ProfileUsername(props: ProfileUsernameProps) {
setIsLoading(false); setIsLoading(false);
}; };
const USERNAME_REGEX = /^[a-zA-Z0-9]*$/;
return ( return (
<div className="flex w-full flex-col"> <div className="flex w-full flex-col">
<label <label
@ -88,7 +90,7 @@ export function ProfileUsername(props: ProfileUsernameProps) {
href={`${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/u/${username}`} href={`${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/u/${username}`}
target="_blank" target="_blank"
className={ className={
'ml-0.5 rounded-md border border-purple-500 px-1.5 py-0.5 text-xs font-medium text-purple-700 transition-colors hover:bg-purple-500 hover:text-purple-800 hover:text-white' 'ml-0.5 rounded-md border border-purple-500 px-1.5 py-0.5 text-xs font-medium text-purple-700 transition-colors hover:bg-purple-500 hover:text-white'
} }
> >
roadmap.sh/u/{username} roadmap.sh/u/{username}
@ -117,7 +119,7 @@ export function ProfileUsername(props: ProfileUsernameProps) {
// only allow letters, numbers // only allow letters, numbers
const keyCode = e.key; const keyCode = e.key;
const validKey = const validKey =
/^[a-zA-Z0-9]*$/.test(keyCode) && username.length < 10; USERNAME_REGEX.test(keyCode) && username.length <= 10;
if ( if (
!validKey && !validKey &&
!['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight'].includes( !['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight'].includes(
@ -127,7 +129,13 @@ export function ProfileUsername(props: ProfileUsernameProps) {
e.preventDefault(); e.preventDefault();
} }
}} }}
onChange={(e) => { onInput={(e) => {
const value = (e.target as HTMLInputElement).value?.trim();
const isValid = USERNAME_REGEX.test(value) && value.length <= 10;
if (!isValid) {
return;
}
setUsername((e.target as HTMLInputElement).value.toLowerCase()); setUsername((e.target as HTMLInputElement).value.toLowerCase());
}} }}
required={profileVisibility === 'public'} required={profileVisibility === 'public'}

Loading…
Cancel
Save