component.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import AutoComplete from './autocomplete.js';
  2. import { CustomElement } from 'shared/components/element.js';
  3. import { FILTER_CONTAINS, FILTER_STARTSWITH } from './utils.js';
  4. import { api } from '@converse/headless/core';
  5. import { html } from 'lit-element';
  6. export default class AutoCompleteComponent extends CustomElement {
  7. static get properties () {
  8. return {
  9. 'getAutoCompleteList': { type: Function },
  10. 'auto_evaluate': { type: Boolean },
  11. 'auto_first': { type: Boolean }, // Should the first element be automatically selected?
  12. 'filter': { type: String },
  13. 'include_triggers': { type: String },
  14. 'min_chars': { type: Number },
  15. 'name': { type: String },
  16. 'placeholder': { type: String },
  17. 'triggers': { type: String }
  18. };
  19. }
  20. constructor () {
  21. super();
  22. this.auto_evaluate = true; // Should evaluation happen automatically without any particular key as trigger?
  23. this.auto_first = false; // Should the first element be automatically selected?
  24. this.filter = 'contains';
  25. this.include_triggers = ''; // Space separated chars which should be included in the returned value
  26. this.match_current_word = false; // Match only the current word, otherwise all input is matched
  27. this.max_items = 10;
  28. this.min_chars = 1;
  29. this.triggers = ''; // String of space separated chars
  30. }
  31. render () {
  32. return html`
  33. <div class="suggestion-box suggestion-box__name">
  34. <ul class="suggestion-box__results suggestion-box__results--above" hidden=""></ul>
  35. <input
  36. type="text"
  37. name="${this.name}"
  38. autocomplete="off"
  39. @keydown=${this.onKeyDown}
  40. @keyup=${this.onKeyUp}
  41. class="form-control suggestion-box__input"
  42. placeholder="${this.placeholder}"
  43. />
  44. <span
  45. class="suggestion-box__additions visually-hidden"
  46. role="status"
  47. aria-live="assertive"
  48. aria-relevant="additions"
  49. ></span>
  50. </div>
  51. `;
  52. }
  53. firstUpdated () {
  54. this.auto_complete = new AutoComplete(this.firstElementChild, {
  55. 'ac_triggers': this.triggers.split(' '),
  56. 'auto_evaluate': this.auto_evaluate,
  57. 'auto_first': this.auto_first,
  58. 'filter': this.filter == 'contains' ? FILTER_CONTAINS : FILTER_STARTSWITH,
  59. 'include_triggers': [],
  60. 'list': () => this.getAutoCompleteList(),
  61. 'match_current_word': true,
  62. 'max_items': this.max_items,
  63. 'min_chars': this.min_chars
  64. });
  65. this.auto_complete.on(
  66. 'suggestion-box-selectcomplete',
  67. () => (this.auto_completing = false)
  68. );
  69. }
  70. onKeyDown (ev) {
  71. this.auto_complete.onKeyDown(ev);
  72. }
  73. onKeyUp (ev) {
  74. this.auto_complete.evaluate(ev);
  75. }
  76. }
  77. api.elements.define('converse-autocomplete', AutoCompleteComponent);