Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(rating): Set rating to 0 when same value is selected
Browse files Browse the repository at this point in the history
- When same rating value is selected, change rating value to 0

Closes #3963
Fixes #3246
  • Loading branch information
RopoMen authored and wesleycho committed Jul 19, 2015
1 parent 86bfec1 commit dbceec7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/rating/rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ angular.module('ui.bootstrap.rating', [])

$scope.rate = function(value) {
if ( !$scope.readonly && value >= 0 && value <= $scope.range.length ) {
ngModelCtrl.$setViewValue(value);
ngModelCtrl.$setViewValue(ngModelCtrl.$viewValue === value ? 0 : value);
ngModelCtrl.$render();
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/rating/test/rating.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ describe('rating directive', function () {
expect(getState()).toEqual([true, true, true, true, true]);
expect($rootScope.rate).toBe(5);
expect(element.attr('aria-valuenow')).toBe('5');

getStar(5).click();
$rootScope.$digest();
expect(getState()).toEqual([false, false, false, false, false]);
expect($rootScope.rate).toBe(0);
expect(element.attr('aria-valuenow')).toBe('0');
});

it('handles correctly the hover event', function() {
Expand Down

2 comments on commit dbceec7

@Chatles
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want this change in my project.

@RopoMen
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to prevent user from doing this you can use $watch to set it back to normal, lest's assume that $scope.rating holds the given stars.

$scope.$watch('rating', function(newVal, oldVal) {
    if(newVal === 0) {
       $scope.rating = oldVal;
    }
});

Would this solve your problem with this functionality? If keyboard navigation wouldn't allow user to set rating to 0, I would have made different solution. Perhaps I'll add one attribute in to rating directive interface which allow control for this behaviour. Something like "allow-toggle" or "allow-zero"...

Please sign in to comment.