Skip to content

Commit

Permalink
fixes #1219 - Tagify within a <label> element can't get focused as …
Browse files Browse the repository at this point in the history
…the focus it shifted to the hidden original field
  • Loading branch information
yairEO committed Feb 7, 2024
1 parent edd8378 commit 859fca4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -691,7 +691,7 @@ required | <pre lang=html>`<input required>`</pre> | Adds a `required`
## Caveats
- `<input>` wrapped in a `<label>` doesn't work - [#1219](https://github.com/yairEO/tagify/issues/1219)
- `<input>` wrapped in a `<label>` doesn't work - [#1219](https://github.com/yairEO/tagify/issues/1219) and so Tagify internally sets the label's `for` attribute to an empty string, so clicking the Tagify component will not blur it and re-focus on the hidden input/textarea element Tagify is "connected" to
## FAQ
List of questions & scenarios which might come up during development with Tagify:
Expand Down
10 changes: 9 additions & 1 deletion src/tagify.js
Expand Up @@ -263,7 +263,8 @@ Tagify.prototype = {
* @param {Object} input [DOM element which would be "transformed" into "Tags"]
*/
build( input ){
var DOM = this.DOM;
var DOM = this.DOM,
labelWrapper = input.closest('label');

if( this.settings.mixMode.integrated ){
DOM.originalInput = null;
Expand All @@ -279,6 +280,10 @@ Tagify.prototype = {
input.parentNode.insertBefore(DOM.scope, input)
input.tabIndex = -1; // do not allow focus or typing directly, once tagified
}

// fixes tagify nested inside a <label> tag from getting focus when clicked on
if( labelWrapper )
labelWrapper.setAttribute('for', '')
},

/**
Expand All @@ -287,6 +292,9 @@ Tagify.prototype = {
destroy(){
this.events.unbindGlobal.call(this)
this.DOM.scope.parentNode.removeChild(this.DOM.scope)



this.DOM.originalInput.tabIndex = this.DOM.originalInput_tabIndex
delete this.DOM.originalInput.__tagify
this.dropdown.hide(true)
Expand Down

0 comments on commit 859fca4

Please sign in to comment.