Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 5fd2c2b

Browse files
authored
fix(select): Improve async load of foundation (#2103)
1 parent c8519ec commit 5fd2c2b

File tree

5 files changed

+149
-117
lines changed

5 files changed

+149
-117
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { NgModule } from '@angular/core';
1+
import {NgModule} from '@angular/core';
22

3-
import { SharedModule } from '../../shared.module';
4-
import { RoutingModule, ROUTE_DECLARATIONS } from './routing.module';
3+
import {SharedModule} from '../../shared.module';
4+
import {RoutingModule, ROUTE_DECLARATIONS} from './routing.module';
5+
import {MDC_SELECT_DEFAULT_OPTIONS} from '@angular-mdc/web';
56

67
@NgModule({
78
imports: [
89
SharedModule,
910
RoutingModule
1011
],
11-
declarations: [ROUTE_DECLARATIONS]
12+
declarations: [ROUTE_DECLARATIONS],
13+
providers: [{ provide: MDC_SELECT_DEFAULT_OPTIONS, useValue: {outlined: true}}],
1214
})
13-
export class SelectModule { }
15+
export class SelectModule {}

packages/select/select-helper-text.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export class MDCSelectHelperText extends MDCComponent<MDCSelectHelperTextFoundat
4646
super(elementRef);
4747
}
4848

49+
get foundation(): MDCSelectHelperTextFoundation {
50+
return this._foundation;
51+
}
52+
4953
getDefaultFoundation() {
5054
const adapter: MDCSelectHelperTextAdapter = {
5155
addClass: (className: string) => this.elementRef.nativeElement.classList.add(className),

packages/select/select.ts

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let nextUniqueId = 0;
131131
encapsulation: ViewEncapsulation.None,
132132
changeDetection: ChangeDetectionStrategy.OnPush
133133
})
134-
export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit, AfterViewInit, DoCheck,
134+
export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoCheck,
135135
OnDestroy, ControlValueAccessor, CanUpdateErrorState, MDCRippleCapableSurface {
136136
/** Emits whenever the component is destroyed. */
137137
private _destroyed = new Subject<void>();
@@ -175,10 +175,8 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
175175
}
176176
set outlined(value: boolean) {
177177
const newValue = coerceBooleanProperty(value);
178-
if (newValue !== this._outlined) {
179-
this._outlined = newValue || (this._defaults && this._defaults.outlined) || false;
180-
this.layout();
181-
}
178+
this._outlined = newValue;
179+
this.layout();
182180
}
183181
private _outlined = false;
184182

@@ -204,13 +202,8 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
204202
}
205203
set valid(value: boolean | undefined) {
206204
const newValue = coerceBooleanProperty(value);
207-
if (newValue !== this._valid) {
208-
this._valid = newValue;
209-
210-
if (this._foundation && this._valid !== undefined) {
211-
this._foundation.setValid(this._valid);
212-
}
213-
}
205+
this._valid = newValue;
206+
this._foundation?.setValid(this._valid);
214207
}
215208
private _valid?: boolean;
216209

@@ -240,10 +233,8 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
240233
return this._helperText;
241234
}
242235
set helperText(helperText: MDCSelectHelperText | undefined) {
243-
if (this._helperText !== helperText) {
244-
this._helperText = helperText;
245-
this.helperText?.init();
246-
}
236+
this._helperText = helperText;
237+
this.helperText?.init();
247238
}
248239
private _helperText?: MDCSelectHelperText;
249240

@@ -359,7 +350,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
359350
/** Returns a map of all subcomponents to subfoundations.*/
360351
private _getFoundationMap(): Partial<MDCSelectFoundationMap> {
361352
return {
362-
helperText: this._helperText?.getDefaultFoundation()
353+
helperText: this._helperText?.foundation
363354
};
364355
}
365356

@@ -373,7 +364,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
373364
@Self() @Optional() public ngControl: NgControl,
374365
@Optional() _parentForm: NgForm,
375366
@Optional() _parentFormGroup: FormGroupDirective,
376-
@Optional() @Inject(MDC_SELECT_DEFAULT_OPTIONS) private _defaults: MdcSelectDefaultOptions) {
367+
@Optional() @Inject(MDC_SELECT_DEFAULT_OPTIONS) private _defaults?: MdcSelectDefaultOptions) {
377368
super(elementRef, _defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl);
378369

379370
this._root = this.elementRef.nativeElement;
@@ -390,18 +381,30 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
390381

391382
// Force setter to be called in case id was not specified.
392383
this.id = this.id;
393-
}
394384

395-
ngAfterContentInit(): void {
396385
this._setDefaultGlobalOptions();
397386
}
398387

399388
ngAfterViewInit(): void {
400-
this._selectBuilder();
389+
this._initialized = true;
390+
this._asyncBuildFoundation()
391+
.then(() => {
392+
this._selectBuilder();
393+
});
401394
}
402395

403396
ngOnDestroy(): void {
404-
this._destroy();
397+
this.destroy();
398+
}
399+
400+
/** Override MdcSelectBase destroy method */
401+
destroy(): void {
402+
this._destroyed.next();
403+
this._destroyed.complete();
404+
405+
this._lineRipple?.destroy();
406+
this._ripple.destroy();
407+
this._foundation.destroy();
405408
}
406409

407410
ngDoCheck(): void {
@@ -500,13 +503,8 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
500503

501504
/** Initialize Select internal state based on the environment state */
502505
private layout(): void {
503-
if (this._foundation) {
504-
this._destroy();
505-
}
506-
507506
if (this._initialized) {
508507
this._selectBuilder();
509-
this._changeDetectorRef.markForCheck();
510508

511509
if (this._outlined) {
512510
this._foundation.layout();
@@ -528,44 +526,42 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterContentInit,
528526

529527
/** Set the default options. */
530528
private _setDefaultGlobalOptions(): void {
531-
if (this._defaults?.outlined) {
532-
this._outlined = this._defaults.outlined;
529+
if (this._defaults) {
530+
if (this._defaults.outlined != null) {
531+
this.outlined = this._defaults.outlined;
532+
}
533533
}
534534
}
535535

536-
private _destroy(): void {
537-
this._destroyed.next();
538-
this._destroyed.complete();
539-
540-
this._lineRipple?.destroy();
541-
this._ripple?.destroy();
536+
async _asyncBuildFoundation(): Promise<void> {
537+
this._foundation = this.getDefaultFoundation();
542538
}
543539

544540
async _asyncInitFoundation(): Promise<void> {
545-
this._foundation = this.getDefaultFoundation();
541+
this._foundation.init();
546542
}
547543

548544
private _selectBuilder(): void {
549545
this._changeDetectorRef.detectChanges();
550546

551-
// initialize after running a detectChanges()
552-
if (!this.outlined) {
553-
this._ripple = this._createRipple();
554-
this._ripple.init();
555-
}
556-
this._initializeSelection();
557-
this._initialized = true;
558-
559-
this._asyncInitFoundation().then(() => this._foundation.init());
560-
561-
this._menu.wrapFocus = false;
562-
this._menu.elementRef.nativeElement.setAttribute('role', 'listbox');
563-
this._menu.elementRef.nativeElement.classList.add('mdc-select__menu');
564-
565-
if (this._menu._list) {
566-
this._menu._list.useSelectedClass = true;
567-
this._menu._list.singleSelection = true;
568-
}
547+
this._asyncInitFoundation()
548+
.then(() => {
549+
// initialize after running a detectChanges()
550+
if (!this.outlined) {
551+
this._ripple = this._createRipple();
552+
this._ripple.init();
553+
}
554+
this._initializeSelection();
555+
556+
this._menu.wrapFocus = false;
557+
this._menu.elementRef.nativeElement.setAttribute('role', 'listbox');
558+
this._menu.elementRef.nativeElement.classList.add('mdc-select__menu');
559+
560+
if (this._menu._list) {
561+
this._menu._list.useSelectedClass = true;
562+
this._menu._list.singleSelection = true;
563+
}
564+
});
569565
this._subscribeToMenuEvents();
570566
}
571567

packages/textfield/text-field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
658658
}
659659
}
660660

661-
/** Override MDCComponent destroy */
661+
/** Override MdcTextFieldBase destroy method */
662662
destroy(): void {
663663
this._lineRipple?.destroy();
664664
this._ripple?.destroy();

0 commit comments

Comments
 (0)