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

Delete key, focus/blur and dropdown improvements for select mode with userInput #1410

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
4 changes: 2 additions & 2 deletions src/parts/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export default {
case 'Backspace': {
if( !_s.readonly && !this.state.editing ) {
this.removeTags(focusedElm);
(nextTag ? nextTag : this.DOM.input).focus()
setTimeout(() => (nextTag ? nextTag : this.DOM.input).focus(), 0)
}

break;
Expand Down Expand Up @@ -723,7 +723,7 @@ export default {
this.state.hasFocus = false

// do not hide the dropdown if a click was initiated within it and that dropdown belongs to this Tagify instance
if( e.target.closest('.tagify__dropdown') && e.target.closest('.tagify__dropdown').__tagify != this )
if( e.target.closest('.tagify__dropdown') == null || e.target.closest('.tagify__dropdown').__tagify != this )
this.dropdown.hide()
}
},
Expand Down
22 changes: 16 additions & 6 deletions src/parts/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export default {
this.state.autoCompleteData = selectedElmData;
this.input.autocomplete.set.call(this, value)
return false
} else if( e.key == 'Tab' && !_s.autoComplete.tabKey && isSelectMode && _s.userInput ) { // do not forget to hide the dropdown in select mode if Tab is pressed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

#1414 depends on this

this.dropdown.hide()
return false
}

return true
}
case 'Enter' : {
Expand All @@ -112,7 +116,7 @@ export default {
if( selectedElm ){
this.dropdown.selectOption(selectedElm)
// highlight next option
selectedElm = this.dropdown.getNextOrPrevOption(selectedElm, !actionUp)
if(!isSelectMode) selectedElm = this.dropdown.getNextOrPrevOption(selectedElm, !actionUp)
this.dropdown.highlightOption(selectedElm)
return
}
Expand Down Expand Up @@ -272,7 +276,8 @@ export default {
*/
selectOption( elm, event ){
var _s = this.settings,
{clearOnSelect, closeOnSelect} = _s.dropdown;
{clearOnSelect, closeOnSelect} = _s.dropdown,
includeSelectedTags = _s.dropdown.includeSelectedTags || _s.mode == 'select';

if( !elm ) {
this.addTags(this.state.inputText, true)
Expand All @@ -288,7 +293,8 @@ export default {
var value = elm.getAttribute('value'),
isNoMatch = value == 'noMatch',
isMixMode = _s.mode == 'mix',
tagData = this.suggestedListItems.find(item => (item.value ?? item) == value)
tagData = this.suggestedListItems.find(item => (item.value ?? item) == value),
tagTextElem = this.DOM.scope.querySelector('.' + _s.classNames.tagText)

// The below event must be triggered, regardless of anything else which might go wrong
this.trigger('dropdown:select', {data:tagData, elm, event})
Expand Down Expand Up @@ -318,10 +324,14 @@ export default {
this.toggleFocusClass(true)
})

// select mode: reset the dropdown to show all options again to the user
if(_s.mode == 'select' && !this.state.composing && this.userInput)
setTimeout(() => tagTextElem && tagTextElem.focus(), 0) //set the focus back to input on each select to ensure consistent behavior

closeOnSelect && setTimeout(this.dropdown.hide.bind(this))

// execute these tasks once a suggestion has been selected
elm.addEventListener('transitionend', () => {
!includeSelectedTags && elm.addEventListener('transitionend', () => {
this.dropdown.fillHeaderFooter()
setTimeout(() => {
elm.remove()
Expand All @@ -330,7 +340,7 @@ export default {
}, {once: true})

// hide selected suggestion
elm.classList.add(this.settings.classNames.dropdownItemHidden)
!includeSelectedTags && elm.classList.add(this.settings.classNames.dropdownItemHidden)
},

// adds all the suggested items, including the ones which are not currently rendered,
Expand Down Expand Up @@ -494,4 +504,4 @@ export default {
return this.settings.templates.dropdownItem.apply(this, [{...suggestion, mappedValue}, this])
}).join("")
}
}
}
1 change: 0 additions & 1 deletion src/tagify.js
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed to prevent the dropdown from closing even when closeOnSelect is false

Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ Tagify.prototype = {
this.removeTags(tagElm)

this.trigger("edit:updated", eventData)
this.dropdown.hide()

// check if any of the current tags which might have been marked as "duplicate" should be now un-marked
if( this.settings.keepInvalidTags )
Expand Down