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

Support email duplicating #152

Merged
merged 2 commits into from
Aug 11, 2023
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
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);
});