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

Commit

Permalink
fix(select): when nothing selected, focus first option
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Feb 26, 2015
1 parent b8da17a commit 50b5d92
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ function SelectProvider($$interimElementProvider) {
configureAria();

element.removeClass('md-leave');

var optionNodes = opts.selectEl[0].getElementsByTagName('md-option');

if (opts.loadingAsync && opts.loadingAsync.then) {
Expand Down Expand Up @@ -611,11 +611,11 @@ function SelectProvider($$interimElementProvider) {
// Escape to close
opts.selectEl.on('keydown', function(ev) {
switch (ev.keyCode) {
case $mdConstant.KEY_CODE.SPACE:
case $mdConstant.KEY_CODE.ENTER:
case $mdConstant.KEY_CODE.SPACE:
case $mdConstant.KEY_CODE.ENTER:
var option = $mdUtil.getClosest(ev.target, 'md-option');
if (option) {
opts.selectEl.triggerHandler({
opts.selectEl.triggerHandler({
type: 'click',
target: option
});
Expand Down Expand Up @@ -726,20 +726,20 @@ function SelectProvider($$interimElementProvider) {
maxWidth = parentRect.width - SELECT_EDGE_MARGIN * 2,
isScrollable = contentNode.scrollHeight > contentNode.offsetHeight,
selectedNode = selectNode.querySelector('md-option[selected]'),
optionNodes = nodesToArray(selectNode.getElementsByTagName('md-option')),
optionNodes = selectNode.getElementsByTagName('md-option'),
optgroupNodes = selectNode.getElementsByTagName('md-optgroup');

var centeredNode;
// If a selected node, center around that
if (selectedNode) {
centeredNode = selectedNode;
// If there are option groups, center around the first option
// If there are option groups, center around the first option group
} else if (optgroupNodes.length) {
centeredNode = optionNodes[0];
// Otherwise, lets center on the middle optionNode
centeredNode = optgroupNodes[0];
// Otherwise, center around the first optionNode
} else if (optionNodes.length){
centeredNode = optionNodes[Math.floor(optionNodes.length / 2 )];
// In case there are no options, center on whatevers in there... (such as a progress indicator)
centeredNode = optionNodes[0];
// In case there are no options, center on whatever's in there... (eg progress indicator)
} else {
centeredNode = contentNode.firstElementChild || contentNode;
}
Expand All @@ -766,7 +766,10 @@ function SelectProvider($$interimElementProvider) {
centeredRect.paddingRight = parseInt(centeredStyle['padding-right'], 10);
}

var focusedNode = centeredNode || optionNodes[0];
var focusedNode = centeredNode;
if ((focusedNode.tagName || '').toUpperCase() === 'MD-OPTGROUP') {
focusedNode = optionNodes[0] || contentNode.firstElementChild || contentNode;
}
if (focusedNode) {
opts.focusedNode = focusedNode;
focusedNode.focus();
Expand Down Expand Up @@ -802,9 +805,12 @@ function SelectProvider($$interimElementProvider) {
left = targetRect.left + centeredRect.left - centeredRect.paddingLeft;
top = targetRect.top + targetRect.height / 2 - centeredRect.height / 2 -
centeredRect.top + contentNode.scrollTop;

transformOrigin = (centeredRect.left + targetRect.width / 2) + 'px ' +
(centeredRect.top + centeredRect.height / 2 - contentNode.scrollTop) + 'px 0px';
containerNode.style['min-width'] = targetRect.width + centeredRect.paddingLeft + centeredRect.paddingRight + 'px';

containerNode.style['min-width'] = targetRect.width + centeredRect.paddingLeft +
centeredRect.paddingRight + 'px';
}

// Keep left and top within the window
Expand Down

0 comments on commit 50b5d92

Please sign in to comment.