forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.ts
78 lines (70 loc) 路 2.48 KB
/
form.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { NzMentionComponent } from 'ng-zorro-antd/mention';
@Component({
selector: 'nz-demo-mention-form',
encapsulation: ViewEncapsulation.None,
template: `
<form nz-form [formGroup]="validateForm" (ngSubmit)="submitForm()">
<nz-form-item>
<nz-form-label [nzSm]="6" nzFor="mention">Top coders</nz-form-label>
<nz-form-control [nzSm]="16" nzErrorTip="More than one must be selected!">
<nz-mention #mentions [nzSuggestions]="suggestions">
<textarea
rows="1"
id="mention"
placeholder="input here"
formControlName="mention"
nzMentionTrigger
nz-input
></textarea>
</nz-mention>
</nz-form-control>
</nz-form-item>
<nz-form-item nz-row style="margin-bottom:8px;">
<nz-form-control [nzSpan]="14" [nzOffset]="6">
<button type="button" nz-button nzType="primary" (click)="submitForm()">Submit</button>
<button type="button" nz-button (click)="resetForm()">Reset</button>
</nz-form-control>
</nz-form-item>
</form>
`
})
export class NzDemoMentionFormComponent implements OnInit {
suggestions = ['afc163', 'benjycui', 'yiminghe', 'RaoHai', '涓枃', '銇伝銈撱仈'];
validateForm!: FormGroup;
@ViewChild('mentions', { static: true }) mentionChild!: NzMentionComponent;
get mention(): AbstractControl {
return this.validateForm.get('mention')!;
}
constructor(private fb: FormBuilder) {}
ngOnInit(): void {
this.validateForm = this.fb.group({
mention: ['@afc163 ', [Validators.required, this.mentionValidator]]
});
}
mentionValidator = (control: FormControl): { [s: string]: boolean } => {
if (!control.value) {
return { required: true };
} else if (this.mentionChild.getMentions().length < 2) {
return { confirm: true, error: true };
}
return {};
};
submitForm(): void {
this.mention.markAsDirty();
this.mention.updateValueAndValidity();
if (this.mention.valid) {
console.log('Submit!!!', this.mention.value);
console.log(this.mentionChild.getMentions());
} else {
console.log('Errors in form!!!');
}
}
resetForm(): void {
this.validateForm?.reset({
mention: '@afc163 '
});
}
}