Skip to content

Commit

Permalink
fix(list): modify key of defaultActive, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
luxiaobei committed Dec 25, 2019
1 parent 912262b commit c9c99a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
[thyMultiple]="selectionModel.multiple"
[thyLayout]="selectionModel.isLayoutGrid ? 'grid' : 'list'"
(thySelectionChange)="selectionChange($event)"
thyFirstItemDefaultActive="true"
>
<thy-list-option *ngFor="let item of items" [thyDisabled]="item.id === 2" [thyValue]="item?.id">
<div class="text-center" *ngIf="selectionModel.isLayoutGrid">
Expand Down
31 changes: 27 additions & 4 deletions src/list/selection/selection-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, DebugElement } from '@angular/core';
import { Component, DebugElement, ViewChild } from '@angular/core';
import { ThySelectionListChange } from './selection.interface';
import { async, ComponentFixture, fakeAsync, TestBed, tick, flush } from '@angular/core/testing';
import { ThyListModule } from '../list.module';
Expand Down Expand Up @@ -89,12 +89,29 @@ describe('MatSelectionList without forms', () => {
expect(selectionList.nativeElement.classList).toContain('thy-grid-list');
expect(listOptions[0].nativeElement.classList).toContain('thy-grid-option');
});

it(`should hover first when thyFirstItemDefaultActive is true`, () => {
fixture.detectChanges();
expect(listOptions[0].nativeElement.classList).toContain('hover');
});

it(`should not hover first when thyFirstItemDefaultActive is false`, () => {
const defaultFixture = TestBed.createComponent(SelectionListWithListOptionsDefaultComponent);
defaultFixture.detectChanges();
const defaultListOptions = defaultFixture.debugElement.queryAll(By.directive(ThyListOptionComponent));
expect(defaultListOptions[0].nativeElement.classList).not.toContain('hover');
});
});
});

@Component({
template: `
<thy-selection-list id="selection-list-1" [thyLayout]="layout" (thySelectionChange)="onValueChange($event)">
<thy-selection-list
id="selection-list-1"
[thyLayout]="layout"
(thySelectionChange)="onValueChange($event)"
[thyFirstItemDefaultActive]="true"
>
<thy-list-option thyValue="inbox">
Inbox (disabled selection-option)
</thy-list-option>
Expand All @@ -120,7 +137,11 @@ class SelectionListWithListOptionsComponent {

@Component({
template: `
<thy-selection-list id="selection-list-1" (thySelectionChange)="onValueChange($event)">
<thy-selection-list
id="selection-list-1"
[thyFirstItemDefaultActive]="false"
(thySelectionChange)="onValueChange($event)"
>
<thy-list-option thyValue="inbox">
Inbox (disabled selection-option)
</thy-list-option>
Expand All @@ -130,12 +151,14 @@ class SelectionListWithListOptionsComponent {
<thy-list-option thyValue="sent-mail">
Sent Mail
</thy-list-option>
<thy-list-option thyValue="drafts" *ngIf="showLastOption">
<thy-list-option thyValue="drafts">
Drafts
</thy-list-option>
</thy-selection-list>
`
})
class SelectionListWithListOptionsDefaultComponent {
@ViewChild(ThySelectionListComponent) thySelectionListComponent: ThySelectionListComponent;

onValueChange(_change: ThySelectionListChange) {}
}
7 changes: 4 additions & 3 deletions src/list/selection/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ThySelectionListChange } from './selection.interface';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { ScrollToService } from '../../core';
import { UpdateHostClassService } from '../../shared/update-host-class.service';
import { startWith } from 'rxjs/operators';

export type ThyListSize = 'sm' | 'md' | 'lg';

Expand Down Expand Up @@ -106,7 +107,7 @@ export class ThySelectionListComponent
this.isLayoutGrid = value === 'grid';
}

@Input() thyDefaultHover: boolean;
@Input() thyFirstItemDefaultActive: boolean;

@Input() set thySize(value: ThyListSize) {
this._setListSize(value);
Expand Down Expand Up @@ -382,8 +383,8 @@ export class ThySelectionListComponent

ngAfterContentInit(): void {
this._initializeFocusKeyManager();
this.options.changes.subscribe(() => {
if (this.thyDefaultHover) {
this.options.changes.pipe(startWith(true)).subscribe(() => {
if (this.thyFirstItemDefaultActive) {
if (!this._keyManager.activeItem || this.options.toArray().indexOf(this._keyManager.activeItem) < -1) {
this._keyManager.setFirstItemActive();
}
Expand Down

0 comments on commit c9c99a2

Please sign in to comment.