Skip to content

Commit

Permalink
task: delete email on backspace
Browse files Browse the repository at this point in the history
  • Loading branch information
finnar-bin committed Feb 1, 2024
1 parent 2d79387 commit ecf946f
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/shell/components/InviteMembersModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo, useState } from "react";
import { useMemo, useRef, useState } from "react";
import {
Autocomplete,
Button,
Expand Down Expand Up @@ -63,6 +63,8 @@ const InviteMembersModal = ({ onClose }: Props) => {
const [showRoleSelectModal, setShowRoleSelectModal] = useState(false);
const [sentEmails, setSentEmails] = useState([]);
const [sendingEmails, setSendingEmails] = useState(false);
const emailChipsRef = useRef([]);
const autocompleteRef = useRef(null);

const [createUserInvite, { isError: createUserInviteError }] =
useCreateUserInviteMutation();
Expand Down Expand Up @@ -158,6 +160,7 @@ const InviteMembersModal = ({ onClose }: Props) => {
sx={{
".MuiOutlinedInput-root ": { alignItems: "baseline" },
}}
ref={autocompleteRef}
multiline
rows={3}
error={emailError}
Expand All @@ -181,6 +184,18 @@ const InviteMembersModal = ({ onClose }: Props) => {
setEmailError(true);
}
}

if (event.key === "Backspace" && !inputValue) {
event.stopPropagation();

// HACK: Needed to prevent the default behavior of autocomplete which autodeletes the right most tag on backspace
setTimeout(() => {
emailChipsRef.current?.[
emailChipsRef.current?.filter((ref) => !!ref)?.length -
1
]?.focus({ visible: true });
}, 100);
}
}}
onChange={(event) => {
if (event.target.value?.split("").pop() === ",") return;
Expand All @@ -193,6 +208,7 @@ const InviteMembersModal = ({ onClose }: Props) => {
emails.map((email, index) => (
<Chip
{...getTagProps({ index })}
ref={(el) => (emailChipsRef.current[index] = el)}
size="small"
color="default"
clickable={false}
Expand All @@ -203,9 +219,20 @@ const InviteMembersModal = ({ onClose }: Props) => {
borderStyle: "solid",
}}
label={email}
onDelete={() =>
setEmails(emails.filter((_, i) => i !== index))
}
onDelete={() => {
setEmails(emails.filter((_, i) => i !== index));
}}
onKeyDown={(event) => {
if (event.key === "Backspace") {
// HACK: Needed to override the default behavior of autocomplete where it automatically selects the next tag after deleting a diff tag via backspace
setTimeout(() => {
setEmails(emails.filter((_, i) => i !== index));
autocompleteRef.current
?.querySelector("textarea")
?.focus();
}, 150);
}
}}
/>
))
}
Expand Down

0 comments on commit ecf946f

Please sign in to comment.