Sha256: 5b89ea5f8d3a010f5cd49e511eec425549df21c81433ff862f49e0f2edab8c54

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

import { Controller } from "@hotwired/stimulus"
import debounce from "debounce"

export default class ComboboxController extends Controller {
  static values = { url: String, debounce: Number };

  connect(){
    this.searchValue = this.searchValue.bind(this);
    this.debounceValue = this.element.getAttribute("data-combobox-debounce-value")
    this.debouncedSearchValue = debounce(this.searchValue, this.debounceValue)
  }

  search(e) {
    if (this.debounceValue && Number(this.debounceValue) > 0){
      this.debouncedSearchValue(e);
    }else {
      this.searchValue(e);
    }
  }

  insertValue(e){
    this.element.querySelector('input').value = e.target.innerText
    this.element.querySelector("turbo-frame").innerHTML = ''
  }

  searchValue(){
    let input = this.element.querySelector('input')
    let query = input.value
    if (query.length < 2) {
      return
    }

    const url = this.element.getAttribute("data-combobox-url-value") + '?query=' + query;

    input.nextSibling.querySelector("turbo-frame").src = url;
  }
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ui_bibz-4.0.0.beta14 app/assets/javascripts/controllers/combobox_controller.js
ui_bibz-4.0.0.beta13 app/assets/javascripts/controllers/combobox_controller.js
ui_bibz-4.0.0.beta10 app/assets/javascripts/controllers/combobox_controller.js
ui_bibz-4.0.0.beta9 app/assets/javascripts/controllers/combobox_controller.js
ui_bibz-4.0.0.beta8 app/assets/javascripts/controllers/combobox_controller.js
ui_bibz-4.0.0.beta7 app/assets/javascripts/controllers/combobox_controller.js
ui_bibz-4.0.0.beta6 app/assets/javascripts/controllers/combobox_controller.js
ui_bibz-4.0.0.beta4 app/assets/javascripts/controllers/combobox_controller.js