-
Notifications
You must be signed in to change notification settings - Fork 150
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
base: master
Are you sure you want to change the base?
Conversation
|
✅ PR title follows Conventional Commits specification. |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 47c816b:
|
} else { | ||
onTagChange?.({ tags: getNewTagsArray(tagIndex) }); | ||
} | ||
onTagChange?.({ tags: getNewTagsArray(tagIndex) }); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@@ -146,20 +145,23 @@ const useTaggedInput = ({ | |||
const inputValue = isControlledValue ? value?.trim() : inputValueUncontrolled.trim(); | |||
if (e.key === 'Enter' || e.key === ',') { | |||
e.event.preventDefault?.(); // we don't want textarea to treat enter as line break in tagged inputs | |||
if (inputValue) { | |||
const isDuplicateTag = currentTags?.includes(inputValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consumers should handle this no? from UX, if our end-user tries to add duplicate tag it should not add that tag and show validation error with message something like "tag already exist" which should be handled on consumer-side 🤔 . It shouldn't magically not add tag if user is trying to add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
understood. thanks for the clarification. removing the duplicate tag check added
Description
TaggedInput
, added a check to avoid duplicate tag additiononChange
will be called fromuseControllableState
hook, wrapped theonTagChange
for controlled component insideelse
branch to avoid getting called twice for uncontrolled componentChanges
Current behaviour:
TaggedInput
, thekey
prop is constructed for eachtag
using key={${tagName}-${tagIndex}
}isTagVisible
prop value as false and few other duplicated tags as non-removableScreen.Recording.2025-02-13.at.16.47.21.mov
Changes made:
onTagChange
invocation part insideelse
branch to avoid getting called twice incase ofuncontrolled
tagged input as it will be called once throughuseControllableState
hookScreen.Recording.2025-02-13.at.16.51.52.mov
Kindly suggest whether we need to prevent the duplicate tag addition or do we need to remove the already existing tag of same input value on hitting [Enter]
Additional Information
Component Checklist