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

Commit ad82012

Browse files
devversionkara
authored andcommitted
feat(list): add class to disable proxy elements. (#9470)
* This allows developers to disable proxy elements inside of an list item. Closes #9423,
1 parent fc7e9b3 commit ad82012

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/components/list/list.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,18 @@ function mdListDirective($mdTheming) {
116116
* - `md-menu` (Open)
117117
*
118118
* This means, when using a supported proxy item inside of `md-list-item`, the list item will
119-
* become clickable and executes the associated action of the proxy element on click.
119+
* automatically become clickable and executes the associated action of the proxy element on click.
120+
*
121+
* It is possible to disable this behavior by applying the `md-no-proxy` class to the list item.
122+
*
123+
* <hljs lang="html">
124+
* <md-list-item class="md-no-proxy">
125+
* <span>No Proxy List</span>
126+
* <md-checkbox class="md-secondary"></md-checkbox>
127+
* </md-list-item>
128+
* </hljs>
129+
*
130+
* Here are a few examples of proxy elements inside of a list item.
120131
*
121132
* <hljs lang="html">
122133
* <md-list-item>
@@ -233,18 +244,21 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
233244

234245
if (tAttrs.ngClick || tAttrs.ngDblclick || tAttrs.ngHref || tAttrs.href || tAttrs.uiSref || tAttrs.ngAttrUiSref) {
235246
wrapIn('button');
236-
} else {
247+
} else if (!tEl.hasClass('md-no-proxy')) {
248+
237249
for (var i = 0, type; type = proxiedTypes[i]; ++i) {
238250
if (proxyElement = tEl[0].querySelector(type)) {
239251
hasProxiedElement = true;
240252
break;
241253
}
242254
}
255+
243256
if (hasProxiedElement) {
244257
wrapIn('div');
245-
} else if (!tEl[0].querySelector('md-button:not(.md-secondary):not(.md-exclude)')) {
258+
} else {
246259
tEl.addClass('md-no-proxy');
247260
}
261+
248262
}
249263

250264
wrapSecondaryItems();
@@ -430,12 +444,13 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
430444
firstElement = $element[0].firstElementChild,
431445
isButtonWrap = $element.hasClass('_md-button-wrap'),
432446
clickChild = isButtonWrap ? firstElement.firstElementChild : firstElement,
433-
hasClick = clickChild && hasClickEvent(clickChild);
447+
hasClick = clickChild && hasClickEvent(clickChild),
448+
noProxies = $element.hasClass('md-no-proxy');
434449

435450
computeProxies();
436451
computeClickable();
437452

438-
if ($element.hasClass('md-proxy-focus') && proxies.length) {
453+
if (proxies.length) {
439454
angular.forEach(proxies, function(proxy) {
440455
proxy = angular.element(proxy);
441456

@@ -458,7 +473,8 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
458473

459474

460475
function computeProxies() {
461-
if (firstElement && firstElement.children && !hasClick) {
476+
477+
if (firstElement && firstElement.children && !hasClick && !noProxies) {
462478

463479
angular.forEach(proxiedTypes, function(type) {
464480

src/components/list/list.spec.js

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

67+
it('should not wrap when proxies are disabled', function() {
68+
var listItem = setup(
69+
'<md-list-item class="md-no-proxy">' +
70+
'<md-switch ng-model="modelVal"></md-switch>' +
71+
'</md-list-item>'
72+
);
73+
74+
var switchEl = listItem[0].querySelector('md-switch');
75+
76+
// If proxies are disabled, the list will not wrap anything.
77+
expect(switchEl.parentNode).toBe(listItem[0]);
78+
79+
listItem.triggerHandler('click');
80+
expect($rootScope.modelVal).toBeFalsy();
81+
82+
switchEl.click();
83+
expect($rootScope.modelVal).toBeTruthy();
84+
85+
expect(listItem).not.toHaveClass('md-clickable')
86+
});
87+
6788
it('should not trigger the proxy element, when clicking on a slider', function() {
6889
var listItem = setup(
6990
'<md-list-item>' +
@@ -362,17 +383,16 @@ describe('mdListItem directive', function() {
362383
expect(secondaryContainer.children()[1].nodeName).toBe('MD-BUTTON');
363384
});
364385

365-
it('should detect non-compiled md-buttons', function() {
386+
it('should not detect a normal button as a proxy element', function() {
366387
var listItem = setup('<md-list-item><md-button ng-click="sayHello()">Hello</md-button></md-list-item>');
367-
expect(listItem.hasClass('md-no-proxy')).toBeFalsy();
388+
expect(listItem.hasClass('md-no-proxy')).toBeTruthy();
368389
});
369390

370-
it('should not detect secondary or excluded md-buttons', function() {
391+
it('should not detect a secondary button as a proxy element', function() {
371392
var listItem = setup(
372393
'<md-list-item>' +
373394
' <div>Content Here</div>' +
374395
' <md-button class="md-secondary" ng-click="sayHello()">Hello</md-button>' +
375-
' <md-button class="md-exclude" ng-click="sayHello()">Hello</md-button>' +
376396
'</md-list-item>'
377397
);
378398
expect(listItem.hasClass('md-no-proxy')).toBeTruthy();

0 commit comments

Comments
 (0)