templates/commentaires/_form.html.twig line 1

  1. {{ form_start(form) }}
  2. <div class="w-full mt-4 border border-gray-200 rounded-lg bg-gray-50 dark:bg-gray-700 dark:border-gray-600">
  3.     <div class="px-4 py-2 bg-white rounded-t-lg dark:bg-gray-800">
  4.         <label for="comment" class="sr-only text-left">Votre commentaire</label>
  5.         {{ form_widget(form) }}
  6.     </div>
  7.     <div class="flex items-center justify-between px-3 py-2 border-t dark:border-gray-600">
  8.         <button type="submit" class="inline-flex items-center py-2.5 px-4 text-xs font-medium text-center text-white bg-prim-700 rounded-lg focus:ring-4 focus:ring-prim-200 dark:focus:ring-prim-900 hover:bg-prim-800">
  9.             Poster
  10.         </button>
  11.         <div class="flex items-center space-x-1">
  12.             {% for starValue in 1..5 %}
  13.                 <svg class="w-4 h-4 text-gray-300 star" data-value="{{ starValue }}" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewbox="0 0 22 20">
  14.                     <path d="M20.924 7.625a1.523 1.523 0 0 0-1.238-1.044l-5.051-.734-2.259-4.577a1.534 1.534 0 0 0-2.752 0L7.365 5.847l-5.051.734A1.535 1.535 0 0 0 1.463 9.2l3.656 3.563-.863 5.031a1.532 1.532 0 0 0 2.226 1.616L11 17.033l4.518 2.375a1.534 1.534 0 0 0 2.226-1.617l-.863-5.03L20.537 9.2a1.523 1.523 0 0 0 .387-1.575Z"/>
  15.                 </svg>
  16.             {% endfor %}
  17.         </div>
  18.     </div>
  19. </div>
  20. <input type="hidden" value="" id="rating" name="rating">
  21. {{ form_end(form) }}
  22. <script>
  23.     const stars = document.querySelectorAll('.star');
  24. let selectedStars = 0;
  25. stars.forEach(star => {
  26. star.addEventListener('click', () => {
  27. selectedStars = parseInt(star.getAttribute('data-value'));
  28. updateStars();
  29. });
  30. });
  31. function updateStars() {
  32. stars.forEach((star, index) => {
  33. if (index < selectedStars) {
  34. star.classList.add('text-yellow-300');
  35. star.classList.remove('text-gray-300');
  36. } else {
  37. star.classList.add('text-gray-300');
  38. star.classList.remove('text-yellow-300');
  39. }
  40. });
  41. calculateRating();
  42. }
  43. let nbRating = 0;
  44. const calculateRating = () => {
  45. nbRating = 0;
  46. stars.forEach(star => {
  47. console.log(star.classList.contains('text-yellow-300'));
  48. if (star.classList.contains('text-yellow-300')) {
  49. nbRating++;
  50. }
  51. document.querySelector('#rating').value = nbRating;
  52. })
  53. }
  54. </script>