// react import React from "react"; const isEmailValid = (email: string) => String(email) .toLowerCase() .match( /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); export const MultiInput = ({ label, name, placeholder, setValue, watch }: any) => { const handleKeyDown = (e: any) => { if (e.key !== "Enter") return; const value = e.target.value; if (!value.trim()) return; if (isEmailValid(value) && !watch(name)?.find((item: any) => item.email === value)) { setValue(name, [...(watch(name) || []), { email: value }]); e.target.value = ""; } }; const handleBlur = (e: React.FocusEvent) => { const value = e.target.value; if (!value.trim()) return; if (isEmailValid(value) && !watch(name)?.find((item: any) => item.email === value)) { setValue(name, [...(watch(name) || []), { email: value }]); e.target.value = ""; } else { e.target.value = ""; } }; const handleChange = (e: any) => { const value = e.target.value.trim(); if (!value) return; if (value.includes(",")) { const tags = value.split(","); tags.forEach((tag: string) => { if (isEmailValid(tag) && !watch(name)?.find((item: any) => item.email === tag)) { setValue(name, [...(watch(name) || []), { email: tag }]); } }); e.target.value = ""; } }; const removeTag = (index: Number) => { setValue( name, watch(name).filter((_: string, i: any) => i !== index) ); }; return ( <> {label && }
{watch(name)?.map((tag: any, index: number) => ( ))}
); };