Skip to content

Commit

Permalink
[@mantine/core] Fix onChange on TagsInput firing twice
Browse files Browse the repository at this point in the history
Previously, when you selected an option from the dropdown with the arrow keys + Enter key, it would cause the onChange to fire twice if there was a search on the input.
  • Loading branch information
Knamer95 committed Jun 19, 2024
1 parent 29f24e3 commit 8ca7f50
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/@mantine/core/src/components/TagsInput/TagsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ export const TagsInput = factory<TagsInputFactory>((_props, ref) => {

if (event.key === 'Enter' && length > 0 && !event.nativeEvent.isComposing) {
event.preventDefault();

const hasActiveSelection = !!document.querySelector<HTMLDivElement>(
`#${combobox.listId} [data-combobox-option][data-combobox-selected]`
);

if (hasActiveSelection) {
return;
}

const isDuplicate = _value.some((tag) => tag.toLowerCase() === inputValue.toLowerCase());

if (isDuplicate) {
Expand Down Expand Up @@ -353,6 +362,8 @@ export const TagsInput = factory<TagsInputFactory>((_props, ref) => {
onOptionSubmit?.(val);
setSearchValue('');
_value.length < maxTags! && setValue([..._value, optionsLockup[val].label]);

combobox.resetSelectedOption();
}}
{...comboboxProps}
>
Expand Down

0 comments on commit 8ca7f50

Please sign in to comment.