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

Commit

Permalink
feat(select): add support for disabled
Browse files Browse the repository at this point in the history
closes #3518
  • Loading branch information
rschmukler committed Jun 30, 2015
1 parent 44fda3d commit adcee7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/components/select/select-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ md-select.md-THEME_NAME-theme {
}

md-select-menu.md-THEME_NAME-theme {
md-option[disabled] {
color: '{{foreground-3}}';
}

md-optgroup {
color: '{{foreground-2}}';
Expand Down
38 changes: 28 additions & 10 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
var option = $mdUtil.getClosest(ev.target, 'md-option');
var optionCtrl = option && angular.element(option).data('$mdOptionController');
if (!option || !optionCtrl) return;
if (option.hasAttribute('disabled')) {
ev.stopImmediatePropagation();
return false;
}

var optionHashKey = selectCtrl.hashGetter(optionCtrl.value);
var isSelected = angular.isDefined(selectCtrl.selected[optionHashKey]);
Expand Down Expand Up @@ -645,6 +649,14 @@ function OptionDirective($mdButtonInkRipple, $mdUtil) {
scope.$watch(function() { return element.text(); }, setOptionValue);
}

attr.$observe('disabled', function(disabled) {
if (disabled) {
element.attr('tabindex', '-1');
} else {
element.attr('tabindex', '0');
}
});

scope.$$postDigest(function() {
attr.$observe('selected', function(selected) {
if (!angular.isDefined(selected)) return;
Expand Down Expand Up @@ -877,16 +889,22 @@ function SelectProvider($$interimElementProvider) {
var optionsArray = $mdUtil.nodesToArray(optionNodes);
var index = optionsArray.indexOf(opts.focusedNode);

if (index === -1) {
// We lost the previously focused element, reset to first option
index = 0;
} else if (direction === 'next' && index < optionsArray.length - 1) {
index++;
} else if (direction === 'prev' && index > 0) {
index--;
}
var newOption = opts.focusedNode = optionsArray[index];
var newOption;

do {
if (index === -1) {
// We lost the previously focused element, reset to first option
index = 0;
} else if (direction === 'next' && index < optionsArray.length - 1) {
index++;
} else if (direction === 'prev' && index > 0) {
index--;
}
newOption = optionsArray[index];
if (newOption.hasAttribute('disabled')) newOption = undefined;
} while (!newOption && index < optionsArray.length - 1 && index > 0)
newOption && newOption.focus();
opts.focusedNode = newOption;
}
function focusNextOption() {
focusOption('next');
Expand Down Expand Up @@ -1069,7 +1087,7 @@ function SelectProvider($$interimElementProvider) {
$$rAF(function() {
element.addClass('md-active');
selectNode.style[$mdConstant.CSS.TRANSFORM] = '';
if (focusedNode) {
if (focusedNode && !focusedNode.hasAttribute('disabled')) {
opts.focusedNode = focusedNode;
focusedNode.focus();
}
Expand Down

0 comments on commit adcee7d

Please sign in to comment.