orchidjs/tom-select

[Bug]: Enter key won't add item when custom `option_create` is used

ocv-robert opened this issue · 1 comments

Bug description

I create a TomSelect with the following options:

  plugins: {
    remove_button: {
      title: 'Remove'
    }
  },

  persist: false,
  copyClassesToDropdown: false,
  closeAfterSelect: false,
  addPrecedence: true,

  controlInput: '<input type="text" autocomplete="off" tabindex="0" class="tom-select-input w-full">',

  render: {
    option: (data, escape) => {
      return `<div class="p-2.5 aria-selected:bg-indigo-600 aria-selected:text-white cursor-pointer">${escape(data.text)}</div>`
    },
    option_create: (data, escape) => {
      return `<div class="p-2.5 aria-selected:bg-indigo-600 aria-selected:text-white cursor-pointer">Add <span class="font-semibold">${escape(data.input)}</span></div>`
    },
    item: (data, escape) => {
      return `<div class="rounded-md ml-2.5 p-2.5 py-1 text-xs whitespace-nowrap bg-indigo-600 text-white">${escape(data.text)}</div>`
    },
    no_results: () => {
      return '<div class="p-2.5">No results found.</div>'
    }
  }

And seemingly just by providing a custom option_create function, it breaks the functionality where Enter adds the item to the field. Tab still works (with createOnBlur enabled):

CleanShot 2024-02-29 at 12 19 05


Removing the custom option_create function restores the functionality, but looks bad due to the lack of styling:

CleanShot 2024-02-29 at 12 28 26

Expected behavior

Enter with the custom option_create function should still work.

Steps to reproduce

https://jsfiddle.net/ag3ob62s/1/

Additional context

  • OS: macOS Sonoma 14.3
  • Browser: Chrome 122.0.6261.69
  • Device: 14" MacBook Pro M2

Wow, apparently it's required that the returned element has the create class applied to it? 🤯

if( option.classList.contains('create') ){
self.createItem(null, () => {
if (self.settings.closeAfterSelect) {
self.close();
}
});