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

Conversation

kabilansakthivelu
Copy link
Contributor

@kabilansakthivelu kabilansakthivelu commented Feb 13, 2025

Description

  • In TaggedInput, added a check to avoid duplicate tag addition
  • Since in uncontrolled tagged input component, where the onChange will be called from useControllableState hook, wrapped the onTagChange for controlled component inside else branch to avoid getting called twice for uncontrolled component

Changes

Current behaviour:

  • In TaggedInput, the key prop is constructed for each tag using key={${tagName}-${tagIndex}}
  • In case if duplicate tags are added, then the key for each tag gets reset on removal which is causing the other duplicated tag (which gets previously deleted tag index) non visible as it gets isTagVisible prop value as false and few other duplicated tags as non-removable
Screen.Recording.2025-02-13.at.16.47.21.mov

Changes made:

  • Added a check to avoid duplicate tag entry
  • Wrapped the onTagChange invocation part inside else branch to avoid getting called twice incase of uncontrolled tagged input as it will be called once through useControllableState hook
Screen.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

  • Update Component Status Page
  • Perform Manual Testing in Other Browsers
  • Add KitchenSink Story
  • Add Interaction Tests (if applicable)
  • Add changeset

Copy link

changeset-bot bot commented Feb 13, 2025

⚠️ No Changeset found

Latest commit: 47c816b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

github-actions bot commented Feb 13, 2025

✅ PR title follows Conventional Commits specification.

Copy link

codesandbox-ci bot commented Feb 13, 2025

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:

Sandbox Source
razorpay/blade: basic Configuration

@rzpcibot
Copy link
Collaborator

rzpcibot commented Feb 13, 2025

Bundle Size Report

Updated Components
Status Component Base Size (kb) Current Size (kb) Diff
TextArea 32.722 32.707 -0.015 KB
TextInput 34.262 34.244 -0.018 KB

Generated by 🚫 dangerJS against 47c816b

Comment on lines +82 to -85
} else {
onTagChange?.({ tags: getNewTagsArray(tagIndex) });
}
onTagChange?.({ tags: getNewTagsArray(tagIndex) });
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.

@@ -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);
Copy link
Member

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

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants