autocomplete.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. export function autocomplete (inputField, path) {
  2. const input = inputField.querySelector('input');
  3. const inputId = input.id;
  4. input.autocomplete = 'off';
  5. const results = document.createElement('ul');
  6. results.className="autocomplete-results";
  7. let selectedLi = null;
  8. let inputValue = '';
  9. let clickOccured = false;
  10. const leaveSelectedLi = () => {
  11. if (selectedLi !== null) {
  12. selectedLi.classList.remove('selected');
  13. selectedLi = null;
  14. }
  15. }
  16. const selectLi = (element) => {
  17. leaveSelectedLi();
  18. selectedLi = element;
  19. if (element !== null) {
  20. selectedLi.classList.add('selected');
  21. }
  22. return selectedLi;
  23. }
  24. /*input.addEventListener('blur', () => {
  25. setTimeout(() => {
  26. if (!clickOccured) {
  27. results.hidden = true;
  28. }
  29. },500);
  30. clickOccured = false;
  31. });*/
  32. document.addEventListener('click', () => {
  33. results.hidden = true;
  34. })
  35. input.addEventListener('keydown', (e) => {
  36. if (e.key === 'Enter') {
  37. e.preventDefault();
  38. if (selectedLi !== null) {
  39. selectedLi.click();
  40. }
  41. }
  42. const liste = results.querySelectorAll('li');
  43. if(liste.length === 0) {
  44. return;
  45. } else {
  46. results.hidden = false;
  47. }
  48. if (e.key === 'ArrowDown') {
  49. e.preventDefault();
  50. if (selectedLi === null) {
  51. input.value = selectLi(results.querySelector('li')).innerText;
  52. } else {
  53. selectLi(results.querySelector('#' + inputId + '-ui-' + (Number(selectedLi.dataset.id) + 1)));
  54. input.value = selectedLi !== null ? selectedLi.innerText : inputValue
  55. }
  56. }
  57. if(e.key === 'ArrowUp') {
  58. e.preventDefault();
  59. if (selectedLi === null) {
  60. input.value = selectLi(liste[liste.length - 1]).innerText;
  61. } else {
  62. selectLi(results.querySelector('#' + inputId + '-ui-' + (Number(selectedLi.dataset.id) - 1)));
  63. input.value = selectedLi !== null ? selectedLi.innerText : inputValue
  64. }
  65. }
  66. })
  67. const clearResults = () => {
  68. results.innerText = '';
  69. results.hidden = true;
  70. }
  71. input.addEventListener('input', (e) => {
  72. const term = input.value.trim();
  73. if (term.length === 0) {
  74. // Ne pas envoyer de requête si le champ est vide
  75. clearResults();
  76. return;
  77. }
  78. // Envoyer une requête Fetch
  79. fetch(path+ '?query=' + encodeURIComponent(term), {
  80. headers: {
  81. 'X-Requested-With': 'XMLHttpRequest'
  82. }
  83. })
  84. .then(response => {
  85. if (!response.ok) {
  86. throw new Error('Erreur lors de la récupération des suggestions');
  87. }
  88. return response.json();
  89. })
  90. .then(data => {
  91. showResults(data);
  92. return true;
  93. })
  94. .catch(error => {
  95. console.error(error);
  96. return false;
  97. });
  98. });
  99. const showResults = (data) => {
  100. inputValue = input.value;
  101. results.hidden = false;
  102. results.innerHTML = '';
  103. let i = 0;
  104. leaveSelectedLi();
  105. data.forEach(element => {
  106. const li = document.createElement('li');
  107. li.innerText = element;
  108. li.classList.add('autocomplete-item');
  109. li.id = inputId + '-ui-' + (++i);
  110. li.dataset.id = i;
  111. results.appendChild(li);
  112. li.addEventListener('mouseover', (e) => {
  113. selectLi(e.target);
  114. });
  115. li.addEventListener('click', (e) => {
  116. clickOccured = true;
  117. leaveSelectedLi();
  118. input.value = e.target.innerText;
  119. clearResults();
  120. },true)
  121. })
  122. if (data.length > 0) {
  123. input.after(results);
  124. }
  125. }
  126. }