autocomplete.js 3.9 KB

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