Skip to content

Commit

Permalink
fix(select): server-side rendering error with preselected value (#6049)
Browse files Browse the repository at this point in the history
Fixes a server-side rendering error in `md-select` if it has a preselected value, in addition to one in the `md-option` component.

Fixes #6045.
  • Loading branch information
crisbeto authored and andrewseguin committed Jul 28, 2017
1 parent f8c5be8 commit 2388d91
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/lib/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ export class MdOption extends _MdOptionMixinBase implements CanDisableRipple {

/** Sets focus onto this option. */
focus(): void {
this._getHostElement().focus();
const element = this._getHostElement();

if ('focus' in element) {
element.focus();
}
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
// considers such imports as unused (https://github.com/Microsoft/TypeScript/issues/14953)
// tslint:disable-next-line:no-unused-variable
import {ScrollStrategy, RepositionScrollStrategy} from '../core/overlay/scroll';
import {Platform} from '@angular/cdk/platform';

/**
* The following style constants are necessary to save here in order
Expand Down Expand Up @@ -111,6 +112,13 @@ export const SELECT_PANEL_PADDING_Y = 16;
*/
export const SELECT_PANEL_VIEWPORT_PADDING = 8;

/**
* Default minimum width of the trigger based on the CSS.
* Used as a fallback for server-side rendering.
* @docs-private
*/
const SELECT_TRIGGER_MIN_WIDTH = 112;

/** Injection token that determines the scroll handling while a select is open. */
export const MD_SELECT_SCROLL_STRATEGY =
new InjectionToken<() => ScrollStrategy>('md-select-scroll-strategy');
Expand Down Expand Up @@ -367,6 +375,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
private _viewportRuler: ViewportRuler,
private _changeDetectorRef: ChangeDetectorRef,
private _overlay: Overlay,
private _platform: Platform,
renderer: Renderer2,
elementRef: ElementRef,
@Optional() private _dir: Directionality,
Expand Down Expand Up @@ -534,7 +543,9 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
* the overlay width to the trigger width.
*/
private _setTriggerWidth(): void {
this._triggerWidth = this._getTriggerRect().width;
this._triggerWidth = this._platform.isBrowser ? this._getTriggerRect().width :
SELECT_TRIGGER_MIN_WIDTH;

this._changeDetectorRef.markForCheck();
}

Expand Down
8 changes: 4 additions & 4 deletions src/universal-app/kitchen-sink/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ <h3>Standalone radios</h3>
<md-radio-button name="onions" disabled>Red</md-radio-button>

<h2>Select</h2>
<md-select>
<md-option>Glass</md-option>
<md-option>Ceramic</md-option>
<md-option>Steel</md-option>
<md-select value="ceramic">
<md-option value="glass">Glass</md-option>
<md-option value="ceramic">Ceramic</md-option>
<md-option value="steel">Steel</md-option>
</md-select>

<h2>Sidenav</h2>
Expand Down

0 comments on commit 2388d91

Please sign in to comment.