Skip to content

Commit c66bfc7

Browse files
authored
feat(module:tab): icon support valid image resoure url (#168)
1 parent 2c5826d commit c66bfc7

File tree

5 files changed

+49
-37
lines changed

5 files changed

+49
-37
lines changed

components/tab/index.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ module: TabModule
1111
----|------|-----|------
1212
heading | 选项卡名称 | `string` | -
1313
disabled | 是否禁用 | `boolean` | -
14-
icon | icon图标,支持HTML | `string` | -
15-
activeIcon | 激活时icon图标,支持HTML | `string` | -
14+
iconClassName | Icon类名,一般用于雪碧图 | `string` | -
15+
icon | icon图标,支持HTML、支持有效图像资源结尾的扩展名URL地址 | `string` | -
16+
activeIcon | 激活时icon图标,支持HTML、支持有效图像资源结尾的扩展名URL地址 | `string` | -
1617
badge | 徽章内容,支持数字或圆点 | `number,'dot'` | -
1718
active | 是否激活 | `boolean` | -
1819
select | 当tab激活时触发 | `EventEmitter<TabDirective>` | -

components/tab/tab.directive.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ import { BarComponent } from './bar.component';
1010
},
1111
})
1212
export class TabDirective implements OnDestroy, OnChanges {
13+
iconUrl = false;
1314
/** 选项卡名称 */
1415
@Input() heading: string;
1516
/** 是否禁用 */
1617
@Input() @InputBoolean() disabled: boolean;
17-
/** icon图标,支持HTML */
18+
/**
19+
* Icon类名,一般用于雪碧图
20+
*/
21+
@Input() iconClassName: string;
22+
/** icon图标,支持HTML、支持有效图像资源结尾的扩展名URL地址 */
1823
@Input() icon: string;
19-
/** 激活时icon图标,支持HTML */
24+
/** 激活时icon图标,支持HTML、支持有效图像资源结尾的扩展名URL地址 */
2025
@Input() activeIcon: string;
2126
/** 徽章内容,支持数字或圆点 */
2227
@Input() badge: number | 'dot';
@@ -59,6 +64,7 @@ export class TabDirective implements OnDestroy, OnChanges {
5964
}
6065

6166
ngOnChanges(): void {
67+
this.iconUrl = /(webp|svg|png|gif|jpg|jpeg|jfif|bmp|dpg)$/i.test(this.icon);
6268
if (!this.activeIcon) {
6369
this.activeIcon = this.icon;
6470
}

components/tab/tab.spec.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import { Component } from '@angular/core';
22
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
33
import { TabModule } from './tab.module';
44

5-
const TABS: any[] = [
5+
const TABS = [
66
{
77
heading: 'tab1',
88
content: 'tab1 content',
99
active: true,
10-
icon: '<img src="./assets/images/icon_tabbar.png">',
10+
iconClassName: '',
11+
icon: './assets/images/icon_tabbar.png',
1112
badge: 8,
1213
},
1314
{
1415
heading: 'tab2',
1516
content: 'tab2 content',
1617
active: false,
18+
iconClassName: '',
1719
icon: '<img src="./assets/images/icon_tabbar.png">',
1820
badge: 'dot',
1921
},
@@ -26,6 +28,7 @@ const navbar_html = `
2628
[heading]="item.heading"
2729
[disabled]="item.disabled"
2830
[active]="item.active"
31+
[iconClassName]="item.iconClassName"
2932
[icon]="item.icon"
3033
[badge]="item.badge"
3134
(select)="_select($event)"
@@ -39,6 +42,7 @@ const tabbar_html = `
3942
[heading]="item.heading"
4043
[disabled]="item.disabled"
4144
[active]="item.active"
45+
[iconClassName]="item.iconClassName"
4246
[icon]="item.icon"
4347
[badge]="item.badge"
4448
(select)="_select($event)"
@@ -51,8 +55,8 @@ function getItems(nativeEl: HTMLElement): NodeListOf<Element> {
5155
return nativeEl.querySelectorAll('.weui-navbar__item');
5256
}
5357

54-
function getItemsByTabbar(nativeEl: HTMLElement): NodeListOf<Element> {
55-
return nativeEl.querySelectorAll('.weui-tabbar__item');
58+
function getItemsByTabbar(nativeEl: HTMLElement, index: number): HTMLElement {
59+
return nativeEl.querySelectorAll('.weui-tabbar__item')[index] as HTMLElement;
5660
}
5761

5862
function getContents(nativeEl: HTMLElement): NodeListOf<Element> {
@@ -79,7 +83,7 @@ function expectActiveTabs(nativeEl: HTMLElement, action: boolean[]): void {
7983
describe('Component: Tabs', () => {
8084
let fixture: ComponentFixture<TestTabComponent>;
8185
let context: TestTabComponent;
82-
let el: any;
86+
let el: HTMLElement;
8387

8488
describe('[Navbar]', () => {
8589
beforeEach(fakeAsync(() => {
@@ -160,15 +164,27 @@ describe('Component: Tabs', () => {
160164
}));
161165

162166
it('should set tab has icon', () => {
163-
expect((getItemsByTabbar(el)[0] as HTMLElement).querySelector('.weui-tabbar__icon')).not.toBeNull();
167+
expect(getItemsByTabbar(el, 0).querySelector('.weui-tabbar__icon')).not.toBeNull();
164168
});
165169

166170
it('should set tab badge number value', () => {
167-
expect((getItemsByTabbar(el)[0] as HTMLElement).querySelector('.weui-badge')!.innerHTML).toBe('8');
171+
expect(getItemsByTabbar(el, 0).querySelector('.weui-badge')!.innerHTML).toBe('8');
168172
});
169173

170174
it('should set tab badge dot value', () => {
171-
expect((getItemsByTabbar(el)[1] as HTMLElement).querySelector('.weui-badge')!.classList).toContain('weui-badge_dot');
175+
expect(getItemsByTabbar(el, 1).querySelector('.weui-badge')!.classList).toContain('weui-badge_dot');
176+
});
177+
178+
it('should be support valid image url', () => {
179+
context.tabs = [{ icon: '1.jpg' }];
180+
fixture.detectChanges();
181+
expect(getItemsByTabbar(el, 0).querySelector('.weui-tabbar__icon')).not.toBeNull();
182+
});
183+
184+
it('#iconClassName', () => {
185+
context.tabs = [{ iconClassName: 'aaa' }];
186+
fixture.detectChanges();
187+
expect(getItemsByTabbar(el, 0).querySelector('.weui-tabbar__icon')!.classList).toContain('aaa');
172188
});
173189
});
174190
});

components/tab/tabbar.component.html

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
<div class="weui-tab__panel"><ng-content></ng-content></div>
22
<div class="weui-tabbar">
3-
<div
4-
class="weui-tabbar__item"
5-
[ngClass]="{ 'weui-bar__item_on': item.active }"
6-
*ngFor="let item of tabs"
7-
(click)="setActive(item)"
8-
>
9-
<div style="display: inline-block;position: relative;">
10-
<div class="weui-tabbar__icon" [innerHTML]="item.active ? item.activeIcon : item.icon"></div>
11-
<span
12-
class="weui-badge"
13-
style="position: absolute;top: -2px;right: -13px;"
14-
*ngIf="item.badge && item.badge !== 'dot'"
15-
>{{ item.badge }}</span
16-
>
17-
<span
18-
class="weui-badge weui-badge_dot"
19-
style="position: absolute;top: 0;right: -6px;"
20-
*ngIf="item.badge && item.badge === 'dot'"
21-
></span>
3+
<div class="weui-tabbar__item" *ngFor="let item of tabs" [ngClass]="{ 'weui-bar__item_on': item.active }" (click)="setActive(item)">
4+
<div style="position: relative; display: inline-block;">
5+
<div *ngIf="item.iconUrl" class="weui-tabbar__icon" [ngClass]="item.iconClassName">
6+
<img [src]="item.active ? item.activeIcon : item.icon" />
7+
</div>
8+
<div *ngIf="!item.iconUrl" class="weui-tabbar__icon" [innerHTML]="item.active ? item.activeIcon : item.icon" [ngClass]="item.iconClassName"></div>
9+
<span class="weui-badge" style="position: absolute; top: -2px; right: -13px;" *ngIf="item.badge && item.badge !== 'dot'">{{ item.badge }}</span>
10+
<span class="weui-badge weui-badge_dot" style="position: absolute; top: 0; right: -6px;" *ngIf="item.badge && item.badge === 'dot'"></span>
2211
</div>
2312
<p class="weui-tabbar__label">{{ item.heading }}</p>
2413
</div>

src/app/example/tabbar/tabbar.component.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import { timer } from 'rxjs';
1010
})
1111
export class DemoTabbarComponent {
1212
time: number;
13-
icon = `<img src=./assets/images/icon_tabbar.png>`;
14-
activeIcon = `<img src=./assets/images/momentloader.png>`;
15-
16-
onSelect(): void {
17-
this.time = new Date().getTime();
18-
}
13+
icon = `./assets/images/icon_tabbar.png`;
14+
activeIcon = `./assets/images/momentloader.png`;
1915

2016
items: any[] = Array(20)
2117
.fill(0)
2218
.map((_v: any, i: number) => i);
2319

20+
onSelect(): void {
21+
this.time = new Date().getTime();
22+
}
23+
2424
onLoadMore(comp: InfiniteLoaderComponent): void {
2525
timer(1500).subscribe(() => {
2626
this.items.push(

0 commit comments

Comments
 (0)