From ddfb84dcca58e79be1aa7bfbd76ec9e6afc1353c Mon Sep 17 00:00:00 2001 From: Marc McIntosh Date: Tue, 13 Feb 2024 18:06:54 +0100 Subject: [PATCH] fix(combobox): when the user types command and deletes the first part of an argument the box should stay open --- src/components/ComboBox/ComboBox.test.tsx | 13 +++++++++++++ src/components/ComboBox/ComboBox.tsx | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/components/ComboBox/ComboBox.test.tsx b/src/components/ComboBox/ComboBox.test.tsx index 0a9addeb..4c6939e0 100644 --- a/src/components/ComboBox/ComboBox.test.tsx +++ b/src/components/ComboBox/ComboBox.test.tsx @@ -175,4 +175,17 @@ describe("ComboBox", () => { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access expect(event.target.value).toEqual("hello\n"); }); + + test("select command, type / and then delete", async () => { + const { user, ...app } = render(); + const textarea = app.getByRole("combobox"); + await user.type(textarea, "@fi{Enter}"); + expect(app.getByRole("combobox").textContent).toEqual("@file "); + await user.type(textarea, "/"); + expect(app.queryByText("/foo")).not.toBeNull(); + expect(app.queryByText("/bar")).not.toBeNull(); + await user.type(textarea, "{Backspace}"); + expect(app.queryByText("/foo")).not.toBeNull(); + expect(app.queryByText("/bar")).not.toBeNull(); + }); }); diff --git a/src/components/ComboBox/ComboBox.tsx b/src/components/ComboBox/ComboBox.tsx index fa8ce97b..91f7874c 100644 --- a/src/components/ComboBox/ComboBox.tsx +++ b/src/components/ComboBox/ComboBox.tsx @@ -175,11 +175,12 @@ export const ComboBox: React.FC = ({ const maybeCommand = detectCommand(ref.current); if (maybeCommand !== null) { - const [command, args] = maybeCommand.command.split(" "); + const maybeCommandWithArguments = maybeCommand.command.split(" "); + const [command, args] = maybeCommandWithArguments; if (!selectedCommand && args) { setSelectedCommand(command + " "); - } else if (selectedCommand && !args) { + } else if (selectedCommand && maybeCommandWithArguments.length < 2) { setSelectedCommand(""); }