Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent duplicate tag addition #2540

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import type { BaseInputProps } from './BaseInput';
import type { BladeElementRefWithValue } from '~utils/types';
import type { FormInputOnEvent, FormInputOnKeyDownEvent } from '~components/Form/FormTypes';
// import { isReactNative } from '~utils';
import { getTagsGroup } from '~components/Tag/getTagsGroup';
import { isReactNative } from '~utils';
import { useControllableState } from '~utils/useControllable';
Expand Down Expand Up @@ -78,11 +77,11 @@ const useTaggedInput = ({
activeTagIndex,
isDisabled,
onDismiss: ({ tagIndex }) => {
console.log('dismiss', { tagIndex });
if (!isTagsControlled) {
setTagsValue(() => getNewTagsArray(tagIndex));
} else {
onTagChange?.({ tags: getNewTagsArray(tagIndex) });
}
onTagChange?.({ tags: getNewTagsArray(tagIndex) });
Comment on lines +82 to -85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this mean that onTagChange won't be called when tags are uncontrolled? we still want to call onTagChange in those scenarios right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have onTagChange passed as callback for onChange into useControllableState hook [ref].

And this is getting invoked inside updateValue function when we call setTagsValue incase of uncontrolled input. Hence moved the onTagChange into else branch to prevent getting called twice for uncontrolled input.

},
});
},
Expand All @@ -104,9 +103,9 @@ const useTaggedInput = ({

if (!isTagsControlled) {
setTagsValue(() => []);
} else {
onTagChange?.({ tags: [] });
}

onTagChange?.({ tags: [] });
};

const clearInput = (): void => {
Expand Down Expand Up @@ -149,17 +148,19 @@ const useTaggedInput = ({
if (inputValue) {
if (!isTagsControlled) {
setTagsValue(() => [...currentTags, inputValue]);
} else {
onTagChange?.({ tags: [...currentTags, inputValue] });
}
onTagChange?.({ tags: [...currentTags, inputValue] });
clearInput();
setActiveTagIndex(-1);
}
}
if (e.key === 'Backspace' && !inputValue && activeTagIndex < 0 && currentTags.length > 0) {
if (!isTagsControlled) {
setTagsValue(() => currentTags.slice(0, -1));
} else {
onTagChange?.({ tags: currentTags.slice(0, -1) });
}
onTagChange?.({ tags: currentTags.slice(0, -1) });
}
};

Expand Down
Loading