Skip to content

Commit 0469389

Browse files
committed
feat: 添加 tag-picker
1 parent ecd9868 commit 0469389

26 files changed

+277
-102
lines changed

Diff for: package-lock.json

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/app/app.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule } from "@angular/core";
1+
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
22
import { BrowserModule } from "@angular/platform-browser";
33
import { FormsModule } from "@angular/forms";
44
import { HttpClientModule } from "@angular/common/http";
@@ -27,5 +27,6 @@ registerLocaleData(zh);
2727
],
2828
providers: [{ provide: NZ_I18N, useValue: zh_CN }],
2929
bootstrap: [AppComponent],
30+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
3031
})
3132
export class AppModule {}

Diff for: src/app/components/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./example/example.component";
2+
export * from "./tag-picker/tag-picker.component";
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<nz-tag
2+
*ngFor="let tag of innerValue; let i = index"
3+
[nzMode]="disabled ? 'default' : 'closeable'"
4+
(nzOnClose)="remove(tag)"
5+
>
6+
{{ tag }}
7+
</nz-tag>
8+
<nz-tag *ngIf="!disabled && !inputVisible" class="editable-tag" nzNoAnimation (click)="showInput()">
9+
<i nz-icon nzType="plus"></i>
10+
添加标签
11+
</nz-tag>
12+
<input
13+
#inputElement
14+
nz-input
15+
nzSize="small"
16+
type="text"
17+
style="width: 78px"
18+
*ngIf="inputVisible"
19+
[(ngModel)]="inputValue"
20+
(blur)="confirm()"
21+
(keydown.enter)="confirm()"
22+
/>

Diff for: src/app/components/tag-picker/tag-picker.component.less

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
2+
3+
import { TagPickerComponent } from "./tag-picker.component";
4+
5+
describe("TagPickerComponent", () => {
6+
let component: TagPickerComponent;
7+
let fixture: ComponentFixture<TagPickerComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [TagPickerComponent],
12+
}).compileComponents();
13+
});
14+
15+
beforeEach(() => {
16+
fixture = TestBed.createComponent(TagPickerComponent);
17+
component = fixture.componentInstance;
18+
fixture.detectChanges();
19+
});
20+
21+
it("should create", () => {
22+
expect(component).toBeTruthy();
23+
});
24+
});
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Component, ElementRef, ViewChild, forwardRef, Input } from "@angular/core";
2+
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms";
3+
4+
type ITags = string[];
5+
6+
@Component({
7+
// tslint:disable-next-line:component-selector
8+
selector: "tag-picker",
9+
templateUrl: "./tag-picker.component.html",
10+
styleUrls: ["./tag-picker.component.less"],
11+
providers: [
12+
{
13+
provide: NG_VALUE_ACCESSOR,
14+
useExisting: forwardRef(() => TagPickerComponent),
15+
multi: true,
16+
},
17+
],
18+
})
19+
export class TagPickerComponent implements ControlValueAccessor {
20+
innerValue: ITags = [];
21+
inputVisible = false;
22+
inputValue = "";
23+
@Input() disabled = false;
24+
@ViewChild("inputElement", { static: false }) inputElement?: ElementRef;
25+
26+
public get value(): ITags {
27+
return this.innerValue;
28+
}
29+
30+
public set value(v: ITags) {
31+
this.innerValue = v;
32+
this.onChange(v);
33+
}
34+
35+
onChange: any = () => {};
36+
37+
onTouched: any = () => {};
38+
39+
writeValue(v: any) {
40+
this.innerValue = v;
41+
}
42+
43+
registerOnChange(fn: any) {
44+
this.onChange = fn;
45+
}
46+
47+
registerOnTouched(fn: any) {
48+
this.onTouched = fn;
49+
}
50+
51+
showInput() {
52+
this.inputVisible = true;
53+
setTimeout(() => {
54+
this.inputElement?.nativeElement.focus();
55+
}, 10);
56+
}
57+
58+
remove(tag: string) {
59+
this.value = this.innerValue.filter((item) => item !== tag);
60+
}
61+
62+
confirm() {
63+
if (this.inputValue) {
64+
const idx = this.inputValue.indexOf(this.inputValue);
65+
if (idx !== -1) {
66+
this.value = [...this.innerValue, this.inputValue];
67+
}
68+
}
69+
this.inputValue = "";
70+
this.inputVisible = false;
71+
}
72+
}

Diff for: src/app/config/app-menu.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const APP_MENUS: IAppMenu = [
2222
{ level: 2, title: "500", link: "/general/500" },
2323
{ level: 2, title: "Maintenance", link: "/general/maintenance" },
2424
{ level: 2, title: "Ajax", link: "/general/ajax" },
25+
{ level: 2, title: "Custom Components", link: "/general/custom-components" },
2526
],
2627
},
2728
{

Diff for: src/app/pages/general/buttons/buttons.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<button nz-button nzType="dashed">Dashed Button</button>
66
<button nz-button nzType="text">Text Button</button>
77
<a nz-button nzType="link">Link Button</a>
8-
<code name="code">{{code1}}</code>
8+
<code name="code">{{ code1 }}</code>
99
</div>
1010
</nz-card>
+6-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
22

3-
import { ButtonsComponent } from './buttons.component';
3+
import { ButtonsComponent } from "./buttons.component";
44

5-
describe('ButtonsComponent', () => {
5+
describe("ButtonsComponent", () => {
66
let component: ButtonsComponent;
77
let fixture: ComponentFixture<ButtonsComponent>;
88

99
beforeEach(async () => {
1010
await TestBed.configureTestingModule({
11-
declarations: [ ButtonsComponent ]
12-
})
13-
.compileComponents();
11+
declarations: [ButtonsComponent],
12+
}).compileComponents();
1413
});
1514

1615
beforeEach(() => {
@@ -19,7 +18,7 @@ describe('ButtonsComponent', () => {
1918
fixture.detectChanges();
2019
});
2120

22-
it('should create', () => {
21+
it("should create", () => {
2322
expect(component).toBeTruthy();
2423
});
2524
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<nz-card nzTitle="TagPicker">
2+
tags: {{ tags }}
3+
<hr />
4+
<tag-picker [disabled]="false" [(ngModel)]="tags"></tag-picker>
5+
</nz-card>

Diff for: src/app/pages/general/custom-components/custom-components.component.less

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
2+
3+
import { CustomComponentsComponent } from "./custom-components.component";
4+
5+
describe("CustomComponentsComponent", () => {
6+
let component: CustomComponentsComponent;
7+
let fixture: ComponentFixture<CustomComponentsComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [CustomComponentsComponent],
12+
}).compileComponents();
13+
});
14+
15+
beforeEach(() => {
16+
fixture = TestBed.createComponent(CustomComponentsComponent);
17+
component = fixture.componentInstance;
18+
fixture.detectChanges();
19+
});
20+
21+
it("should create", () => {
22+
expect(component).toBeTruthy();
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Component, OnInit } from "@angular/core";
2+
3+
@Component({
4+
selector: "app-custom-components",
5+
templateUrl: "./custom-components.component.html",
6+
styleUrls: ["./custom-components.component.less"],
7+
})
8+
export class CustomComponentsComponent implements OnInit {
9+
tags = ["A", "B"];
10+
11+
constructor() {}
12+
13+
ngOnInit(): void {}
14+
}

Diff for: src/app/pages/general/e404/e404.component.spec.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
22

3-
import { E404Component } from './e404.component';
3+
import { E404Component } from "./e404.component";
44

5-
describe('E404Component', () => {
5+
describe("E404Component", () => {
66
let component: E404Component;
77
let fixture: ComponentFixture<E404Component>;
88

99
beforeEach(async () => {
1010
await TestBed.configureTestingModule({
11-
declarations: [ E404Component ]
12-
})
13-
.compileComponents();
11+
declarations: [E404Component],
12+
}).compileComponents();
1413
});
1514

1615
beforeEach(() => {
@@ -19,7 +18,7 @@ describe('E404Component', () => {
1918
fixture.detectChanges();
2019
});
2120

22-
it('should create', () => {
21+
it("should create", () => {
2322
expect(component).toBeTruthy();
2423
});
2524
});

Diff for: src/app/pages/general/e404/e404.component.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit } from "@angular/core";
22

33
@Component({
4-
selector: 'app-e404',
5-
templateUrl: './e404.component.html',
6-
styleUrls: ['./e404.component.less']
4+
selector: "app-e404",
5+
templateUrl: "./e404.component.html",
6+
styleUrls: ["./e404.component.less"],
77
})
88
export class E404Component implements OnInit {
9+
constructor() {}
910

10-
constructor() { }
11-
12-
ngOnInit(): void {
13-
}
14-
11+
ngOnInit(): void {}
1512
}

Diff for: src/app/pages/general/e500/e500.component.spec.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
22

3-
import { E500Component } from './e500.component';
3+
import { E500Component } from "./e500.component";
44

5-
describe('E500Component', () => {
5+
describe("E500Component", () => {
66
let component: E500Component;
77
let fixture: ComponentFixture<E500Component>;
88

99
beforeEach(async () => {
1010
await TestBed.configureTestingModule({
11-
declarations: [ E500Component ]
12-
})
13-
.compileComponents();
11+
declarations: [E500Component],
12+
}).compileComponents();
1413
});
1514

1615
beforeEach(() => {
@@ -19,7 +18,7 @@ describe('E500Component', () => {
1918
fixture.detectChanges();
2019
});
2120

22-
it('should create', () => {
21+
it("should create", () => {
2322
expect(component).toBeTruthy();
2423
});
2524
});

Diff for: src/app/pages/general/e500/e500.component.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit } from "@angular/core";
22

33
@Component({
4-
selector: 'app-e500',
5-
templateUrl: './e500.component.html',
6-
styleUrls: ['./e500.component.less']
4+
selector: "app-e500",
5+
templateUrl: "./e500.component.html",
6+
styleUrls: ["./e500.component.less"],
77
})
88
export class E500Component implements OnInit {
9+
constructor() {}
910

10-
constructor() { }
11-
12-
ngOnInit(): void {
13-
}
14-
11+
ngOnInit(): void {}
1512
}

Diff for: src/app/pages/general/general-routing.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { MaintenanceComponent } from "./maintenance/maintenance.component";
66
import { AjaxComponent } from "./ajax/ajax.component";
77
import { ButtonsComponent } from "./buttons/buttons.component";
88
import { TypographyComponent } from "./typography/typography.component";
9+
import { CustomComponentsComponent } from "./custom-components/custom-components.component";
910

1011
const routes: Routes = [
1112
{ path: "typography", component: TypographyComponent },
@@ -14,6 +15,7 @@ const routes: Routes = [
1415
{ path: "500", component: E500Component },
1516
{ path: "maintenance", component: MaintenanceComponent },
1617
{ path: "ajax", component: AjaxComponent },
18+
{ path: "custom-components", component: CustomComponentsComponent },
1719
];
1820

1921
@NgModule({

Diff for: src/app/pages/general/general.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { MaintenanceComponent } from "./maintenance/maintenance.component";
99
import { AjaxComponent } from "./ajax/ajax.component";
1010
import { ButtonsComponent } from "./buttons/buttons.component";
1111
import { TypographyComponent } from "./typography/typography.component";
12+
import { CustomComponentsComponent } from "./custom-components/custom-components.component";
1213

1314
@NgModule({
1415
declarations: [
@@ -18,6 +19,7 @@ import { TypographyComponent } from "./typography/typography.component";
1819
AjaxComponent,
1920
ButtonsComponent,
2021
TypographyComponent,
22+
CustomComponentsComponent,
2123
],
2224
imports: [CommonModule, SharedModule, GeneralRoutingModule],
2325
})

Diff for: src/app/pages/general/maintenance/maintenance.component.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<div class="error">
22
<div class="error__header">
3-
<img height="128" src="../../../../assets/images/maintenance.svg">
3+
<img height="128" src="../../../../assets/images/maintenance.svg" />
44
</div>
55
<div class="error__title">Temporarily down for maintenance</div>
6-
<div class="error__desc">Sorry for the inconvenience but we’re performing some maintenance at the moment. We’ll be back online shortly!</div>
6+
<div class="error__desc">
7+
Sorry for the inconvenience but we’re performing some maintenance at the moment. We’ll be back
8+
online shortly!
9+
</div>
710
<div class="error__action">
811
<button nz-button nzType="primary">
912
<i nz-icon nzType="left" nzTheme="outline"></i>Take me home

0 commit comments

Comments
 (0)