From 2992e6fcb40d743b7149633db4e609bbc1f173ec Mon Sep 17 00:00:00 2001 From: Aryan Kaushik Date: Fri, 27 Sep 2024 20:07:56 +0000 Subject: [PATCH] fix: allow to reset rating input if not mandatory If a rating field is not mandatory and the user selects the same rating again then it should reset the rating by setting value to 0. Fixes #27922 --- frappe/public/js/frappe/form/controls/rating.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/frappe/public/js/frappe/form/controls/rating.js b/frappe/public/js/frappe/form/controls/rating.js index 1ae49c1feb..2ae44d8f45 100644 --- a/frappe/public/js/frappe/form/controls/rating.js +++ b/frappe/public/js/frappe/form/controls/rating.js @@ -47,12 +47,25 @@ frappe.ui.form.ControlRating = class ControlRating extends frappe.ui.form.Contro let star_value = el.data("rating"); let left_half = false; let cls = "star-click"; + let out_of_ratings = this.df.options || 5; if (!click) cls = "star-hover"; if (ev.pageX - el.offset().left < el.width() / 2) { left_half = true; star_value--; } + + if (click && !this.df.reqd) { + let fractional_star_value = star_value; + if (left_half) { + fractional_star_value += 0.5; + } + if (this.get_value() == fractional_star_value / out_of_ratings) { + star_value = 0; + left_half = false; + } + } + el.parent() .children("svg") .each(function (e) { @@ -67,7 +80,6 @@ frappe.ui.form.ControlRating = class ControlRating extends frappe.ui.form.Contro } }); if (click) { - let out_of_ratings = this.df.options || 5; star_value = star_value / out_of_ratings; this.validate_and_set_in_model(star_value, ev);