Skip to content

Commit

Permalink
feat: preserve highlighting on tag escape keydown
Browse files Browse the repository at this point in the history
  • Loading branch information
sadmann7 committed Feb 22, 2025
1 parent 17df4d3 commit 86c9053
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/app/(lobby)/pg/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import { tricks } from "@/lib/data";
import TagsInputDemo from "@/registry/default/examples/tags-input-demo";
import {
Combobox,
ComboboxAnchor,
Expand All @@ -40,6 +41,7 @@ import { ChevronDown } from "lucide-react";
export default function PlaygroundPage() {
return (
<Shell>
<TagsInputDemo />
<Combobox className="w-[15rem]">
<ComboboxAnchor>
<ComboboxInput placeholder="Search tricks..." />
Expand Down
3 changes: 3 additions & 0 deletions packages/tags-input/src/tags-input-item-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function TagsInputEditableItemText() {
} else if (event.key === "Escape") {
setEditValue(itemContext.displayValue);
context.setEditingIndex(null);
context.setHighlightedIndex(itemContext.index);
context.inputRef.current?.focus();
}
event.stopPropagation();
Expand All @@ -78,6 +79,8 @@ function TagsInputEditableItemText() {
context.inputRef.current?.focus,
editValue,
itemContext.value,
context.setHighlightedIndex,
itemContext.index,
],
);

Expand Down
9 changes: 5 additions & 4 deletions packages/tags-input/src/tags-input-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -481,22 +481,23 @@ const TagsInputRoot = React.forwardRef<
break;
}
case "Escape": {
setHighlightedIndex(null);
setEditingIndex(null);
if (highlightedIndex !== null) setHighlightedIndex(null);
if (editingIndex !== null) setEditingIndex(null);
requestAnimationFrame(() => target.setSelectionRange(0, 0));
break;
}
}
},
[
dir,
editingIndex,
highlightedIndex,
value,
onItemRemove,
dir,
getEnabledItems,
editable,
disabled,
loop,
getEnabledItems,
],
);

Expand Down

0 comments on commit 86c9053

Please sign in to comment.