Skip to content

Commit

Permalink
Fix logic about props 'emails' (#132)
Browse files Browse the repository at this point in the history
* Fix logic about props 'emails'

* Fix check validation about props 'emails'

* Fix check validation about props 'emails'

---------

Co-authored-by: 김형석 <[email protected]>
  • Loading branch information
HyeongSeoku and 김형석 authored Jul 17, 2023
1 parent 50e4c14 commit a2aea8b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions react-multi-email/ReactMultiEmail.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { isEmail as isEmailFn } from './isEmail';
import { isEmail, isEmail as isEmailFn } from './isEmail';

export interface IReactMultiEmailProps {
id?: string;
Expand Down Expand Up @@ -31,15 +31,27 @@ export interface IReactMultiEmailProps {
autoComplete?: string;
}

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

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

export function ReactMultiEmail(props: IReactMultiEmailProps) {
const {
id,
style,
getLabel,
emails: propsEmails,
className = '',
noClass,
placeholder,
autoFocus,
delimiter = '[ ,;]',
initialInputValue = '',
inputClassName,
autoComplete,
getLabel,
enable,
onDisabled,
validateEmail,
Expand All @@ -50,22 +62,18 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
onKeyDown,
onKeyUp,
spinner,
delimiter = '[ ,;]',
initialInputValue = '',
inputClassName,
autoComplete,
} = props;
const emailInputRef = React.useRef<HTMLInputElement>(null);

const [focused, setFocused] = React.useState(false);
const [emails, setEmails] = React.useState<string[]>([]);
const [emails, setEmails] = React.useState<string[]>(() => initialEmailAddress(propsEmails));
const [inputValue, setInputValue] = React.useState(initialInputValue);
const [spinning, setSpinning] = React.useState(false);

const findEmailAddress = React.useCallback(
async (value: string, isEnter?: boolean) => {
const validEmails: string[] = [];
let inputValue: string = '';
let inputValue = '';
const re = new RegExp(delimiter, 'g');
const isEmail = validateEmail || isEmailFn;

Expand Down Expand Up @@ -235,10 +243,6 @@ export function ReactMultiEmail(props: IReactMultiEmailProps) {
onFocus?.();
}, [onFocus]);

React.useEffect(() => {
setEmails(props.emails ?? []);
}, [props.emails]);

return (
<div
className={`${className} ${noClass ? '' : 'react-multi-email'} ${focused ? 'focused' : ''} ${
Expand Down

1 comment on commit a2aea8b

@vercel
Copy link

@vercel vercel bot commented on a2aea8b Jul 17, 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.vercel.app
react-multi-email-git-master-axframe.vercel.app

Please sign in to comment.