Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This pull request addresses the issue where the eact-multi-email com… #171

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', 'abc@def.com']}
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);
});
Loading