Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rating form item #397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/modules/form/assets/css/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ display: none;

.form-item-rating{
border: unset;
background-color: unset;
}

.stars-rating {
Expand All @@ -147,22 +148,18 @@ display: none;
cursor: pointer;
color: #ccc;
transition: color 0.2s;
font-size: 30px;
}

/* Fill the stars on hover */
.stars-rating label:hover,
.stars-rating label:hover ~ label {
}

.stars-rating label:hover,
.stars-rating label:hover ~ label {
color: #f5b301;
}

/* Fill the stars on selection */
.stars-rating input:checked ~ label {
}

.stars-rating input:checked ~ label {
color: #f5b301;
}

/* Ensure all previous stars are filled when a star is selected */
.stars-rating input:checked ~ label,
.stars-rating input:checked ~ label ~ label {
}

.stars-rating input:checked ~ label,
.stars-rating input:checked ~ label ~ label {
color: #f5b301;
}
}
35 changes: 35 additions & 0 deletions src/modules/form/assets/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,46 @@ $(document).bind('shadow_open', function() {
});

$(document).ready(function() {

// Attach the submit handler to the form
$('.ajxa-form').on('submit', function(event) {
submitFormViaAjax(event, this);
});

// Handle change event for radio buttons
$('.stars-rating input').on('change', function() {
fillStars($(this));
});
// Handle click event for labels
$('.stars-rating label').on('click', function() {
var $input = $(this).prev('input');
$input.prop('checked', true).trigger('change');
});

// Initialize star ratings based on checked input
$('.stars-rating input:checked').each(function() {
fillStars($(this));
});

});

// Function to handle star filling
function fillStars($element) {
var $parent = $element.closest('.stars-rating');
var selectedValue = $element.val();

$parent.find('label').each(function() {
var $label = $(this);
var labelValue = $label.prev('input').val();

if (labelValue <= selectedValue) {
$label.css('color', '#f5b301');
} else {
$label.css('color', '#ccc');
}
});
}

function submitFormViaAjax(e,form) {
e.preventDefault(); // Prevent the default form submission
formId = `#${$(form).attr('id')}`;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/form/classes/Qtags/FormItemRating.qtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function render() {
default:
for ($i=$max; $i >=1 ; $i--) {
$checked = ($value == $i) ? 'checked' : '';
$html_body .= "<input type=\"radio\" id=\"star$i\" name=\"$form_item_name\" value=\"$i\" $checked><label for=\"star$i\" title=\"$i stars\">★</label>"; }
$html_body .= "<input type=\"radio\" id=\"$form_item_name-star$i\" name=\"$form_item_name\" value=\"$i\" $checked><label for=\"star$i\" title=\"$i stars\">★</label>"; }
break;
}
$html_body .= '</div>';
Expand Down