plane/apps/app/components/ui/multi-input.tsx
Anmol Singh Bhatia 483c49d0ff
fix: ui and bug fixes (#980)
* style: sub issue theming

* style: shortcut modal theming

* style: blocking and blocked by modal theming

* fix: filter labels dropdown width fix

* style: display properties

* chore: workspace invite

* style: invite co workers theming

* style: create workspace theming

* style: attachment theming

* style: workspace sidebar theming

* style: issue property theming

* style: create module modal lead icon

* style: label list modal theming

* delete attachment and member modal theming

* style: transfer issue modal

* style: delete estimate modal theming

* style: module form status

* style: delete state modal theming

* style: shortcut modal

* style: onboarding logo

* style: onboarding command menu
2023-05-02 20:00:33 +05:30

79 lines
2.4 KiB
TypeScript

// 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<HTMLInputElement, Element>) => {
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 && <label className="mb-2 text-brand-secondary">{label}</label>}
<div className="rounded-md border border-brand-base p-2">
{watch(name)?.map((tag: any, index: number) => (
<button
type="button"
className="m-1.5 rounded-full bg-brand-surface-2 px-3 py-2 "
key={index}
>
{tag.email} <span onClick={() => removeTag(index)}>&times;</span>
</button>
))}
<input
onKeyDown={handleKeyDown}
onBlur={handleBlur}
onChange={handleChange}
type="text"
placeholder={placeholder}
className="block w-full rounded-md bg-transparent p-1.5 text-sm focus:outline-none"
/>
</div>
</>
);
};