This repository was archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(select): Fix empty option stlying issue. #8907
Closed
topherfangio
wants to merge
1
commit into
angular:master
from
profoundry-us:fix-select-undefined-value-6851
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,32 @@ angular.module('material.components.select', [ | |
* By default, the select will display with an underline to match other form elements. This can be | ||
* disabled by applying the `md-no-underline` CSS class. | ||
* | ||
* ### Option Params | ||
* | ||
* When applied, `md-option-empty` will mark the option as "empty" allowing the option to clear the | ||
* select and put it back in it's default state. You may supply this attribute on any option you | ||
* wish, however, it is automatically applied to an option whose `value` or `ng-value` are not | ||
* defined. | ||
* | ||
* **Automatically Applied** | ||
* | ||
* - `<md-option>` | ||
* - `<md-option value>` | ||
* - `<md-option value="">` | ||
* - `<md-option ng-value>` | ||
* - `<md-option ng-value="">` | ||
* | ||
* **NOT Automatically Applied** | ||
* | ||
* - `<md-option ng-value="1">` | ||
* - `<md-option ng-value="''">` | ||
* - `<md-option ng-value="undefined">` | ||
* - `<md-option value="undefined">` (this evaluates to the string `"undefined"`) | ||
* - <code ng-non-bindable><md-option ng-value="{{someValueThatMightBeUndefined}}"></code> | ||
* | ||
* **Note:** A value of `undefined` ***is considered a valid value*** (and does not auto-apply this | ||
* attribute) since you may wish this to be your "Not Available" or "None" option. | ||
* | ||
* @param {expression} ng-model The model! | ||
* @param {boolean=} multiple Whether it's multiple. | ||
* @param {expression=} md-on-close Expression to be evaluated when the select is closed. | ||
|
@@ -723,6 +749,11 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) { | |
// Map the given element to its innerHTML string. If the element has a child ripple | ||
// container remove it from the HTML string, before returning the string. | ||
mapFn = function(el) { | ||
// If we do not have a `value` or `ng-value`, assume it is an empty option which clears the select | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment seems to be left over from old code. Needs updating or removal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed; good catch! |
||
if (el.hasAttribute('md-option-empty')) { | ||
return ''; | ||
} | ||
|
||
var html = el.innerHTML; | ||
|
||
// Remove the ripple container from the selected option, copying it would cause a CSP violation. | ||
|
@@ -856,9 +887,21 @@ function OptionDirective($mdButtonInkRipple, $mdUtil) { | |
element.append(angular.element('<div class="_md-text">').append(element.contents())); | ||
|
||
element.attr('tabindex', attr.tabindex || '0'); | ||
|
||
if (!hasDefinedValue(attr)) { | ||
element.attr('md-option-empty', ''); | ||
} | ||
|
||
return postLink; | ||
} | ||
|
||
function hasDefinedValue(attr) { | ||
var value = attr.value; | ||
var ngValue = attr.ngValue; | ||
|
||
return value || ngValue; | ||
} | ||
|
||
function postLink(scope, element, attr, ctrls) { | ||
var optionCtrl = ctrls[0]; | ||
var selectCtrl = ctrls[1]; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened here? Is this comment meant to be structured like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, unfortunately, the double curly braces were being interpreted by Angular and thus it was disappearing from the docs site 😆