Skip to content

Commit

Permalink
This pull request addresses the issue where the eact-multi-email com… (
Browse files Browse the repository at this point in the history
…#171)

* This pull request addresses the issue where the 
eact-multi-email component does not respect custom email validation. The fix ensures that the component respects the user's custom validation by modifying and moving the initialEmailAddress function to prioritize the provided custom validation over the internal validation. Additionally, tests have NOT been updated to cover the fixed behavior. Please review and merge at your earliest convenience. Thank you!

* test added to emails.test.tsx - making sure the custom validation is returning the emails properly based on the validateEmail prop

* test added to emails.test.tsx - making sure the custom validation is returning the emails properly based on the validateEmail prop
  • Loading branch information
Elnatanv authored Feb 14, 2024
1 parent cffda7f commit 0e7b3ce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
13 changes: 7 additions & 6 deletions react-multi-email/ReactMultiEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ export interface IReactMultiEmailProps {
allowDuplicate?: boolean;
}

const initialEmailAddress = (emails?: string[]) => {
if (typeof emails === 'undefined') return [];

const validEmails = emails.filter(email => isEmailFn(email));
return validEmails;
};

export function ReactMultiEmail(props: IReactMultiEmailProps) {
const {
Expand Down Expand Up @@ -77,6 +71,13 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
const [inputValue, setInputValue] = React.useState(initialInputValue);
const [spinning, setSpinning] = React.useState(false);

const initialEmailAddress = (emails?: string[]) => {
if (typeof emails === 'undefined') return [];

const validEmails = emails.filter(email => validateEmail ? validateEmail(email) : isEmailFn(email));
return validEmails;
};

const findEmailAddress = React.useCallback(
async (value: string, isEnter?: boolean) => {
const validEmails: string[] = [];
Expand Down
26 changes: 26 additions & 0 deletions test/emails.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,29 @@ describe('ReactMultEmail emails TEST', () => {
expect(emptyElement).toBeTruthy();
});
});


it('Emails with custom validation', async () => {

const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]/;

const mockValidateEmailFunc = jest.fn().mockImplementation((email) => regex.test(email));

render(
<ReactMultiEmail
validateEmail={(mockValidateEmailFunc)}
emails={['abc@gmail','abc','def', '[email protected]']}
getLabel={(email, index) => {
return (
<div data-tag key={index}>
<div data-tag-item>{email}</div>
</div>
);
}}
/>,
);

const emailsWrapper = document.querySelector('.data-labels');
// 4 emails are passed to the component, but only 2 are valid based on the custom validation.
expect(emailsWrapper?.childElementCount).toEqual(2);
});

0 comments on commit 0e7b3ce

Please sign in to comment.