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

Tags get broken when clicking on label #1260

Closed
3 tasks done
ancc opened this issue Nov 6, 2023 · 7 comments
Closed
3 tasks done

Tags get broken when clicking on label #1260

ancc opened this issue Nov 6, 2023 · 7 comments

Comments

@ancc
Copy link

ancc commented Nov 6, 2023

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

💥 Demo Page

https://jsbin.com/hovosavawe/edit?html,js,output

Explanation

Start typing in input and hold down some key and the same time click on input label(still holding down the key).

  • What is the expected behavior?
    tags not get broken
  • What is happening instead?
    Tags get broken, result for example when holding down key "d"
    {"value":"{"value":"{\"value\":\"ddddddddddddddd\"}]dd"}]dddddddddddd"}]dddddddd
  • What error message are you getting?
    no errors
@AaronAsAChimp
Copy link

This is also an issue if the input is located inside the label. The focus is moved to the (usually) hidden input immediately upon focusing the Tagify component. This causes the dropdown to close immediately.

Demo: https://jsbin.com/laquxiqeyi/1/edit?html,js,output

@yairEO
Copy link
Owner

yairEO commented Jan 6, 2024

@AaronAsAChimp - the JSBIN you posted seems to lack any modifications which showcases an issue

@yairEO
Copy link
Owner

yairEO commented Jan 6, 2024

@ancc - simply remove the for attribute in your HTML: <label for="text">Text</label>

It shouldn't/can't co-exist with Tagify


This is somewhat related to #1219

@yairEO yairEO closed this as completed Jan 6, 2024
@AaronAsAChimp
Copy link

AaronAsAChimp commented Jan 10, 2024

@yairEO I'm not sure what happened there. This bin should show the issue (I doubled checked it 😁). https://jsbin.com/nanapotihe/edit?html,js,output

Edit: Here is the code that causes the issue in case anything weird happens to the jsbin.

  <label>
  <input placeholder="type tags">
  </label>
var input = document.querySelector('input')
var tagify = new Tagify(input)

@yairEO
Copy link
Owner

yairEO commented Jan 11, 2024

Tagify cannot work with inputs which reside inside <Label> unless you manipulate the label so it won't point to the input:

<label for=''>

@AaronAsAChimp
Copy link

Thank you, I found something that works. Adding the following to my above example makes it work as expected. Could this or something similar be integrated into Tagify? If not that's ok, but I like the usability that the label provides.

function redirectFocus(e) {
  e.preventDefault()
  const tagifyInput = e.target.querySelector('.tagify__input');
 
  if (tagifyInput) {
    tagifyInput.focus();
  }
}

input.labels.forEach((label) => {
  label.addEventListener('click', redirectFocus);
})

@yairEO
Copy link
Owner

yairEO commented Jan 11, 2024

please use event delegation and do not bind an event on each individual element.

ask bard or chatGPT to convert it for you to event-delegated click event on all <label> elements and ask it to teach you what that is :)

you can also just do this in your redirectFocus function:

e.target.querySelector('.tagify__input')?.focus()

I don't want Tagify to directly mess with HTML elements in the page and prefer to leave that for developers to solve.

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

No branches or pull requests

3 participants