-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:divider): add divider component (#1029)
* wip(module:divider): add divider component * Use NzUpdateHostClassService Instead of class operation * fix doc * fix string and template invalid switched * fix tslint
- Loading branch information
Showing
12 changed files
with
155 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Component, DebugElement, ViewChild } from '@angular/core'; | ||
import { fakeAsync, tick, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { NzDividerComponent } from './nz-divider.component'; | ||
import { NzDividerModule } from './nz-divider.module'; | ||
|
||
describe('divider', () => { | ||
let fixture: ComponentFixture<TestDividerComponent>; | ||
let context: TestDividerComponent; | ||
let dl: DebugElement; | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ NzDividerModule ], | ||
declarations: [ TestDividerComponent, TestDividerTextTemplateComponent ] | ||
}).compileComponents(); | ||
fixture = TestBed.createComponent(TestDividerComponent); | ||
context = fixture.componentInstance; | ||
dl = fixture.debugElement; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
describe('#nzDashed', () => { | ||
for (const value of [ true, false ]) { | ||
it(`[${value}]`, () => { | ||
context.nzDashed = value; | ||
fixture.detectChanges(); | ||
expect(dl.query(By.css('.ant-divider-dashed')) != null).toBe(value); | ||
}); | ||
} | ||
}); | ||
|
||
describe('#nzType', () => { | ||
for (const value of [ 'horizontal', 'vertical' ]) { | ||
it(`[${value}]`, () => { | ||
context.nzType = value; | ||
fixture.detectChanges(); | ||
expect(dl.query(By.css(`.ant-divider-${value}`)) != null).toBe(true); | ||
}); | ||
} | ||
}); | ||
|
||
describe('#nzText', () => { | ||
for (const item of [ { text: 'with text', ret: true }, { text: undefined, ret: false } ]) { | ||
it(`[${item.text}]`, () => { | ||
context.nzText = item.text; | ||
fixture.detectChanges(); | ||
expect(dl.query(By.css('.ant-divider-inner-text')) != null).toBe(item.ret); | ||
}); | ||
} | ||
|
||
it('should be custom template', () => { | ||
let fixtureTemplate: ComponentFixture<TestDividerTextTemplateComponent>; | ||
fixtureTemplate = TestBed.createComponent(TestDividerTextTemplateComponent); | ||
fixtureTemplate.detectChanges(); | ||
expect(fixtureTemplate.debugElement.query(By.css('.anticon-plus')) != null).toBe(true); | ||
}); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
template: ` | ||
<nz-divider #comp | ||
[nzDashed]="nzDashed" | ||
[nzType]="nzType" | ||
[nzText]="nzText"></nz-divider> | ||
` | ||
}) | ||
class TestDividerComponent { | ||
@ViewChild('comp') comp: NzDividerComponent; | ||
nzDashed = false; | ||
nzType = 'horizontal'; | ||
nzText = 'with text'; | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<nz-divider nzDashed [nzText]="text"> | ||
<ng-template #text><i class="anticon anticon-plus"></i> Add</ng-template> | ||
</nz-divider> | ||
` | ||
}) | ||
class TestDividerTextTemplateComponent { | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './public-api'; |
This file contains 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 |
---|---|---|
@@ -1,51 +1,66 @@ | ||
import { Component, HostBinding, Input } from '@angular/core'; | ||
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Input, OnChanges, OnInit, SimpleChanges, TemplateRef } from '@angular/core'; | ||
|
||
import { NzUpdateHostClassService } from '../core/services/update-host-class.service'; | ||
import { toBoolean } from '../core/util/convert'; | ||
|
||
@Component({ | ||
selector : 'nz-divider', | ||
preserveWhitespaces: false, | ||
template : ` | ||
<span class="ant-divider-inner-text"> | ||
<ng-content></ng-content> | ||
selector: 'nz-divider', | ||
template: ` | ||
<span *ngIf="isText" class="ant-divider-inner-text"> | ||
<ng-container *ngIf="textStr; else textTpl">{{ textStr }}</ng-container> | ||
</span> | ||
`, | ||
host : { | ||
'[class.ant-divider]': 'true' | ||
} | ||
`, | ||
providers: [NzUpdateHostClassService], | ||
preserveWhitespaces: false, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class NzDividerComponent { | ||
_dashed = false; | ||
_text = false; | ||
export class NzDividerComponent implements OnChanges, OnInit { | ||
// region fields | ||
|
||
isText = false; | ||
textStr = ''; | ||
textTpl: TemplateRef<void>; | ||
@Input() | ||
set nzText(value: string | TemplateRef<void>) { | ||
if (value instanceof TemplateRef) { | ||
this.textStr = null; | ||
this.textTpl = value; | ||
} else { | ||
this.textStr = value; | ||
} | ||
this.isText = !!value; | ||
} | ||
|
||
@Input() nzType: 'horizontal' | 'vertical' = 'horizontal'; | ||
|
||
private _dashed = false; | ||
@Input() | ||
@HostBinding('class.ant-divider-dashed') | ||
set nzDashed(value: boolean) { | ||
this._dashed = toBoolean(value); | ||
} | ||
|
||
get nzDashed(): boolean { | ||
return this._dashed; | ||
} | ||
|
||
@Input() | ||
@HostBinding('class.ant-divider-with-text') | ||
set nzText(value: boolean) { | ||
this._text = toBoolean(value); | ||
// endregion | ||
private setClass(): void { | ||
const classMap = { | ||
['ant-divider']: true, | ||
[`ant-divider-${this.nzType}`]: true, | ||
[`ant-divider-with-text`]: this.isText, | ||
[`ant-divider-dashed`]: this.nzDashed | ||
}; | ||
this.updateHostClassService.updateHostClass(this.el.nativeElement, classMap); | ||
this.cd.detectChanges(); | ||
} | ||
|
||
get nzText(): boolean { | ||
return this._text; | ||
} | ||
constructor(private el: ElementRef, private cd: ChangeDetectorRef, private updateHostClassService: NzUpdateHostClassService) {} | ||
|
||
@HostBinding('class.ant-divider-horizontal') | ||
get isHorizontal(): boolean { | ||
return this.nzType === 'horizontal'; | ||
ngOnChanges(changes: SimpleChanges): void { | ||
this.setClass(); | ||
} | ||
|
||
@HostBinding('class.ant-divider-vertical') | ||
get isVertical(): boolean { | ||
return this.nzType === 'vertical'; | ||
ngOnInit(): void { | ||
this.setClass(); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './nz-divider.component'; | ||
export * from './nz-divider.module'; |
This file contains 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 |
---|---|---|
|
@@ -62,4 +62,4 @@ | |
border-style: dashed none none; | ||
} | ||
} | ||
} | ||
} |
This file contains 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