Skip to content

Commit

Permalink
Support email duplicating (#152)
Browse files Browse the repository at this point in the history
* Support duplicating

* Add tests
  • Loading branch information
134130 committed Aug 11, 2023
1 parent 4b5570b commit 1ca3287
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
10 changes: 7 additions & 3 deletions react-multi-email/ReactMultiEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface IReactMultiEmailProps {
disableOnBlurValidation?: boolean;
allowDisplayName?: boolean;
stripDisplayName?: boolean;
allowDuplicate?: boolean;
}

const initialEmailAddress = (emails?: string[]) => {
Expand All @@ -52,6 +53,7 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
autoFocus,
allowDisplayName = false,
stripDisplayName = false,
allowDuplicate = false,
delimiter = `[${allowDisplayName ? '' : ' '},;]`,
initialInputValue = '',
inputClassName,
Expand Down Expand Up @@ -84,9 +86,11 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
const isEmail = validateEmail || isEmailFn;

const addEmails = (email: string) => {
for (let i = 0, l = emails.length; i < l; i++) {
if (emails[i] === email) {
return false;
if (!allowDuplicate) {
for (let i = 0, l = emails.length; i < l; i++) {
if (emails[i] === email) {
return false;
}
}
}
validEmails.push(email);
Expand Down
64 changes: 64 additions & 0 deletions test/allowDuplicate.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { cleanup, fireEvent, render } from '@testing-library/react';
import { ReactMultiEmail } from '../react-multi-email';
import React from 'react';

afterEach(cleanup);

function createReactMultiEmail(allowDuplicate?: boolean) {
return (
<ReactMultiEmail
allowDuplicate={allowDuplicate}
getLabel={(email, index, removeEmail) => {
return (
<div data-tag key={index}>
<div data-tag-item>{email}</div>
<span data-tag-handle onClick={() => removeEmail(index)}>
×
</span>
</div>
);
}}
/>
);
}
it('allowDuplicate = true', async () => {
const { getByRole } = render(createReactMultiEmail(true));
const textbox = getByRole('textbox');

const num = 3;
for (let i = 0; i < num; i++) {
fireEvent.change(textbox, { target: { value: `test@example.com` } });
fireEvent.keyUp(textbox, { key: 'Enter', code: 'Enter' });
}

const emailsWrapper = document.querySelector('.data-labels');
expect(emailsWrapper?.childElementCount)?.toBe(num);
});

it('allowDuplicate = false', async () => {
const { getByRole } = render(createReactMultiEmail(false));
const textbox = getByRole('textbox');

const num = 3;
for (let i = 0; i < num; i++) {
fireEvent.change(textbox, { target: { value: `test@example.com` } });
fireEvent.keyUp(textbox, { key: 'Enter', code: 'Enter' });
}

const emailsWrapper = document.querySelector('.data-labels');
expect(emailsWrapper?.childElementCount)?.toBe(1);
});

it('allowDuplicate = undefined', async () => {
const { getByRole } = render(createReactMultiEmail());
const textbox = getByRole('textbox');

const num = 3;
for (let i = 0; i < num; i++) {
fireEvent.change(textbox, { target: { value: `test@example.com` } });
fireEvent.keyUp(textbox, { key: 'Enter', code: 'Enter' });
}

const emailsWrapper = document.querySelector('.data-labels');
expect(emailsWrapper?.childElementCount)?.toBe(1);
});

1 comment on commit 1ca3287

@vercel
Copy link

@vercel vercel bot commented on 1ca3287 Aug 11, 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-axframe.vercel.app
react-multi-email-git-master-axframe.vercel.app
react-multi-email.vercel.app

Please sign in to comment.