Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 97e30d8

Browse files
devversionThomasBurleson
authored andcommitted
fix(list): proxy elements should be not triggered by other controls
* The list item should not trigger the proxied element, when clicking on some specified components The current forbidden controls are now: - `md-slider` Fixes #7937. Closes #8180
1 parent 1f01d26 commit 97e30d8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/components/list/list.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
330330
// Append the button wrap before our list-item content, because it will overlay in relative.
331331
itemContainer.prepend(buttonWrap);
332332
itemContainer.children().eq(1).append(tEl.contents());
333-
333+
334334
tEl.addClass('_md-button-wrap');
335335
}
336336

@@ -425,7 +425,7 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
425425

426426
function postLink($scope, $element, $attr, ctrl) {
427427
$element.addClass('_md'); // private md component indicator for styling
428-
428+
429429
var proxies = [],
430430
firstElement = $element[0].firstElementChild,
431431
isButtonWrap = $element.hasClass('_md-button-wrap'),
@@ -482,6 +482,25 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
482482
}
483483
}
484484

485+
function isEventFromControl(event) {
486+
var forbiddenControls = ['md-slider'];
487+
488+
// If there is no path property in the event, then we can assume that the event was not bubbled.
489+
if (!event.path) {
490+
return forbiddenControls.indexOf(event.target.tagName.toLowerCase()) !== -1;
491+
}
492+
493+
// We iterate the event path up and check for a possible component.
494+
// Our maximum index to search, is the list item root.
495+
var maxPath = event.path.indexOf($element.children()[0]);
496+
497+
for (var i = 0; i < maxPath; i++) {
498+
if (forbiddenControls.indexOf(event.path[i].tagName.toLowerCase()) !== -1) {
499+
return true;
500+
}
501+
}
502+
}
503+
485504
var clickChildKeypressListener = function(e) {
486505
if (e.target.nodeName != 'INPUT' && e.target.nodeName != 'TEXTAREA' && !e.target.isContentEditable) {
487506
var keyCode = e.which || e.keyCode;
@@ -504,6 +523,10 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
504523

505524
if (proxies.length == 1 && clickChild) {
506525
$element.children().eq(0).on('click', function(e) {
526+
// When the event is coming from an control and it should not trigger the proxied element
527+
// then we are skipping.
528+
if (isEventFromControl(e)) return;
529+
507530
var parentButton = $mdUtil.getClosest(e.target, 'BUTTON');
508531
if (!parentButton && clickChild.contains(e.target)) {
509532
angular.forEach(proxies, function(proxy) {

src/components/list/list.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ describe('mdListItem directive', function() {
6464
expect($rootScope.modelVal).toBe(false);
6565
});
6666

67+
it('should not trigger the proxy element, when clicking on a slider', function() {
68+
var listItem = setup(
69+
'<md-list-item>' +
70+
'<md-slider></md-slider>' +
71+
'<md-switch ng-model="modelVal"></md-switch>' +
72+
'</md-list-item>');
73+
74+
var slider = listItem.find('md-slider')[0];
75+
76+
slider.click();
77+
78+
expect($rootScope.modelVal).toBeFalsy();
79+
});
80+
6781
it('should convert spacebar keypress events as clicks', inject(function($mdConstant) {
6882
var listItem = setup(
6983
'<md-list-item>' +

0 commit comments

Comments
 (0)