Skip to content

Commit

Permalink
fix: account for emails being inputed as "Name <[email protected]>" (#127)
Browse files Browse the repository at this point in the history
account for emails being inputed as "Name <[email protected]>"
  • Loading branch information
jhaltercmu2020 authored Jul 23, 2023
1 parent c303cb2 commit dfff6aa
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions react-multi-email/ReactMultiEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface IReactMultiEmailProps {
delimiter?: string;
initialInputValue?: string;
autoComplete?: string;
allowDisplayName?: boolean;
stripDisplayName?: boolean;
}

const initialEmailAddress = (emails?: string[]) => {
Expand All @@ -47,7 +49,9 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
noClass,
placeholder,
autoFocus,
delimiter = '[ ,;]',
allowDisplayName = false,
stripDisplayName = false,
delimiter = `[${allowDisplayName ? '' : ' '},;]`,
initialInputValue = '',
inputClassName,
autoComplete,
Expand Down Expand Up @@ -104,7 +108,22 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
addEmails('' + arr.shift());
} else {
if (arr.length === 1) {
inputValue = '' + arr.shift();
if (allowDisplayName) {
const validateResultWithDisplayName = isEmail('' + arr[0], { allowDisplayName });
if (validateResultWithDisplayName) {
// Strip display name from email formatted as such "First Last <[email protected]>"
const email = stripDisplayName ? arr.shift()?.split("<")[1].split(">")[0] : arr.shift();
addEmails('' + email);
} else {
if (arr.length === 1) {
inputValue = '' + arr.shift();
} else {
arr.shift();
}
}
} else {
inputValue = '' + arr.shift();
}
} else {
arr.shift();
}
Expand Down Expand Up @@ -136,6 +155,15 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
if (typeof validateResult === 'boolean') {
if (validateResult) {
addEmails(value);
} else if (allowDisplayName) {
const validateResultWithDisplayName = isEmail(value, { allowDisplayName });
if (validateResultWithDisplayName) {
// Strip display name from email formatted as such "First Last <[email protected]>"
const email = stripDisplayName ? value.split("<")[1].split(">")[0] : value;
addEmails(email);
} else {
inputValue = value;
}
} else {
inputValue = value;
}
Expand Down

1 comment on commit dfff6aa

@vercel
Copy link

@vercel vercel bot commented on dfff6aa Jul 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-multi-email – ./

react-multi-email-git-master-axframe.vercel.app
react-multi-email.vercel.app
react-multi-email-axframe.vercel.app

Please sign in to comment.