Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 19560f1

Browse files
committed
Add fix for checkboxes not checking/unchecking with confirm
1 parent c920368 commit 19560f1

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

angular-confirm.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ angular.module('angular-confirm', ['ui.bootstrap.modal'])
7979
},
8080
link: function (scope, element, attrs) {
8181

82+
function onSuccess() {
83+
var rawEl = element[0];
84+
//Event is prevented from default action which causes checkboxes not to check
85+
if (typeof rawEl.checked != "undefined") {
86+
rawEl.checked = !rawEl.checked;
87+
}
88+
scope.ngClick();
89+
}
90+
8291
element.unbind("click").bind("click", function ($event) {
8392

8493
$event.preventDefault();
@@ -95,10 +104,10 @@ angular.module('angular-confirm', ['ui.bootstrap.modal'])
95104
if (scope.confirmCancel) {
96105
data.cancel = scope.confirmCancel;
97106
}
98-
$confirm(data, scope.confirmSettings || {}).then(scope.ngClick);
107+
$confirm(data, scope.confirmSettings || {}).then(onSuccess);
99108
} else {
100109

101-
scope.$apply(scope.ngClick);
110+
scope.$apply(onSuccess);
102111
}
103112
});
104113

test/unit/confirmSpec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,62 @@ describe('angular-confirm', function() {
239239
});
240240
});
241241

242+
243+
describe('with checkbox', function() {
244+
245+
beforeEach(angular.mock.inject(function($compile) {
246+
element = angular.element('<input type="checkbox" ng-click="click()" confirm="Are you sure?" />');
247+
$compile(element)($scope);
248+
$scope.$digest();
249+
}));
250+
251+
it("should call confirm on click and not call the function", function() {
252+
element.triggerHandler('click');
253+
expect($scope.click).not.toHaveBeenCalled();
254+
expect($confirm).toHaveBeenCalled();
255+
expect(element[0].checked).toBe(false);
256+
});
257+
258+
});
259+
260+
describe('with checkbox and confirm if false', function() {
261+
262+
beforeEach(angular.mock.inject(function($compile) {
263+
element = angular.element('<input type="checkbox" ng-click="click()" confirm="Are you sure?" confirm-if="truthy" />');
264+
$compile(element)($scope);
265+
$scope.$digest();
266+
}));
267+
268+
it("should set the checkbox to checked", function() {
269+
expect(element[0].checked).toBe(false);
270+
$scope.truthy = false;
271+
$scope.$apply();
272+
element.triggerHandler('click');
273+
expect($scope.click).toHaveBeenCalled();
274+
expect($confirm).not.toHaveBeenCalled();
275+
expect(element[0].checked).toBe(true);
276+
});
277+
});
278+
279+
describe('with checkbox already checked and confirm if false', function() {
280+
281+
beforeEach(angular.mock.inject(function($compile) {
282+
element = angular.element('<input type="checkbox" ng-click="click()" confirm="Are you sure?" confirm-if="truthy" checked />');
283+
$compile(element)($scope);
284+
$scope.$digest();
285+
}));
286+
287+
it("should set the checkbox to checked", function() {
288+
expect(element[0].checked).toBe(true);
289+
$scope.truthy = false;
290+
$scope.$apply();
291+
element.triggerHandler('click');
292+
expect($scope.click).toHaveBeenCalled();
293+
expect($confirm).not.toHaveBeenCalled();
294+
expect(element[0].checked).toBe(false);
295+
});
296+
});
297+
242298
});
243299

244300
});

0 commit comments

Comments
 (0)