Mention提及

    用于在输入中提及某人或某事,常用于发布、聊天或评论功能。

    基本使用

    基本使用

    1. @Component({
    2. selector: 'nz-demo-mention-basic',
    3. encapsulation: ViewEncapsulation.None,
    4. template: `
    5. <nz-mention [nzSuggestions]="suggestions" (nzOnSelect)="onSelect($event)">
    6. <input placeholder="input here" nzMentionTrigger nz-input [(ngModel)]="inputValue" (ngModelChange)="onChange($event)" />
    7. </nz-mention>
    8. `
    9. })
    10. export class NzDemoMentionBasicComponent {
    11. inputValue: string = '@afc163';
    12. suggestions = ['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご'];
    13. onChange(value: string): void {
    14. console.log(value);
    15. }
    16. onSelect(suggestion: string): void {
    17. console.log(`onSelect ${suggestion}`);
    18. }
    19. }

    Mention提及 - 图2

    异步加载

    匹配内容列表为异步返回时。

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. import { MentionOnSearchTypes } from 'ng-zorro-antd/mention';
    3. @Component({
    4. selector: 'nz-demo-mention-async',
    5. encapsulation: ViewEncapsulation.None,
    6. template: `
    7. <nz-mention [nzSuggestions]="suggestions" [nzLoading]="loading" (nzOnSearchChange)="onSearchChange($event)">
    8. <input nzMentionTrigger nz-input [(ngModel)]="inputValue" />
    9. </nz-mention>
    10. `
    11. })
    12. export class NzDemoMentionAsyncComponent {
    13. inputValue?: string;
    14. loading = false;
    15. suggestions: string[] = [];
    16. onSearchChange({ value }: MentionOnSearchTypes): void {
    17. console.log(`search: ${value}`);
    18. this.loading = true;
    19. this.fetchSuggestions(value, suggestions => {
    20. console.log(suggestions);
    21. this.suggestions = suggestions;
    22. this.loading = false;
    23. });
    24. }
    25. fetchSuggestions(value: string, callback: (suggestions: string[]) => void): void {
    26. const users = ['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai'];
    27. setTimeout(() => {
    28. return callback(users.filter(item => item.indexOf(value) !== -1));
    29. }, 500);
    30. }
    31. }

    头像

    自定义建议(含头像)

    注意,nzSuggestions 不为 string[] 时,需要提供 nzValueWith

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-mention-avatar',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <nz-mention [nzSuggestions]="webFrameworks" [nzValueWith]="valueWith" (nzOnSelect)="onSelect($event)">
    7. <input nz-input nzMentionTrigger [(ngModel)]="inputValue" />
    8. <ng-container *nzMentionSuggestion="let framework">
    9. <nz-avatar nzSize="small" [nzText]="framework.name" [nzSrc]="framework.icon"></nz-avatar>
    10. <span>{{ framework.name }} - {{ framework.type }}</span>
    11. </ng-container>
    12. </nz-mention>
    13. `,
    14. styles: [
    15. `
    16. .ant-avatar.ant-avatar-sm {
    17. width: 14px;
    18. height: 14px;
    19. margin-right: 8px;
    20. position: relative;
    21. }
    22. `
    23. ]
    24. })
    25. export class NzDemoMentionAvatarComponent {
    26. inputValue?: string;
    27. webFrameworks = [
    28. { name: 'React', type: 'JavaScript', icon: 'https://zos.alipayobjects.com/rmsportal/LFIeMPzdLcLnEUe.svg' },
    29. { name: 'Angular', type: 'JavaScript', icon: 'https://zos.alipayobjects.com/rmsportal/PJTbxSvzYWjDZnJ.png' },
    30. { name: 'Flask', type: 'Python', icon: 'https://zos.alipayobjects.com/rmsportal/xaypBUijfnpAlXE.png' }
    31. ];
    32. valueWith = (data: { name: string; type: string; icon: string }) => data.name;
    33. onSelect(value: string): void {
    34. console.log(value);
    35. }
    36. }

    多行

    多行模式。

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-mention-multilines',
    4. encapsulation: ViewEncapsulation.None,
    5. <nz-mention [nzSuggestions]="suggestions">
    6. <textarea nz-input [nzAutosize]="{ minRows: 4, maxRows: 4 }" [(ngModel)]="inputValue" nzMentionTrigger> </textarea>
    7. </nz-mention>
    8. `
    9. })
    10. export class NzDemoMentionMultilinesComponent {
    11. inputValue?: string;
    12. suggestions = ['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご'];
    13. }

    Mention提及 - 图5

    自定义触发字符

    通过 nzPrefix 属性自定义触发字符。默认为 @, 可以定义为数组。

    向上展开

    向上展开建议。

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-mention-placement',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <nz-mention nzPlacement="top" [nzSuggestions]="suggestions" (nzOnSelect)="onSelect($event)">
    7. <input nzMentionTrigger nz-input [(ngModel)]="inputValue" (ngModelChange)="onChange($event)" />
    8. </nz-mention>
    9. `
    10. })
    11. export class NzDemoMentionPlacementComponent {
    12. inputValue?: string;
    13. suggestions = ['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご'];
    14. onChange(value: string): void {
    15. console.log(value);
    16. }
    17. onSelect(suggestion: string): void {
    18. console.log(`onSelect ${suggestion}`);
    19. }
    20. }

    Mention提及 - 图7

    自定义建议

    自定义建议

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-mention-custom-tag',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <nz-mention [nzSuggestions]="webFrameworks" [nzValueWith]="valueWith" (nzOnSelect)="onSelect($event)">
    7. <input placeholder="@someone" nz-input nzMentionTrigger [(ngModel)]="inputValue" />
    8. <ng-container *nzMentionSuggestion="let framework">
    9. <span>{{ framework.name }} - {{ framework.type }}</span>
    10. </ng-container>
    11. </nz-mention>
    12. `
    13. })
    14. export class NzDemoMentionCustomTagComponent {
    15. inputValue?: string;
    16. webFrameworks = [
    17. { name: 'React', type: 'JavaScript' },
    18. { name: 'Angular', type: 'JavaScript' },
    19. { name: 'Laravel', type: 'PHP' },
    20. { name: 'Flask', type: 'Python' },
    21. { name: 'Django', type: 'Python' }
    22. ];
    23. valueWith = (data: { name: string; type: string }) => data.name;
    24. onSelect(value: string): void {
    25. console.log(value);
    26. }
    27. }

    配合 Form 使用

    受控模式,例如配合 Form 使用。

    1. import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
    2. import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
    3. import { NzMentionComponent } from 'ng-zorro-antd/mention';
    4. @Component({
    5. selector: 'nz-demo-mention-controlled',
    6. encapsulation: ViewEncapsulation.None,
    7. template: `
    8. <form nz-form [formGroup]="validateForm" (ngSubmit)="submitForm()">
    9. <nz-form-item>
    10. <nz-form-label [nzSm]="6" nzFor="mention">Top coders</nz-form-label>
    11. <nz-form-control [nzSm]="16" nzErrorTip="More than one must be selected!">
    12. <nz-mention #mentions [nzSuggestions]="suggestions">
    13. <input id="mention" placeholder="input here" formControlName="mention" nzMentionTrigger nz-input />
    14. </nz-mention>
    15. </nz-form-control>
    16. </nz-form-item>
    17. <nz-form-item nz-row style="margin-bottom:8px;">
    18. <button type="button" nz-button nzType="primary" (click)="submitForm()">Submit</button>
    19. &nbsp;&nbsp;&nbsp;
    20. <button type="button" nz-button (click)="resetForm()">Reset</button>
    21. </nz-form-control>
    22. </nz-form-item>
    23. </form>
    24. `
    25. })
    26. export class NzDemoMentionControlledComponent implements OnInit {
    27. suggestions = ['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご'];
    28. validateForm!: FormGroup;
    29. @ViewChild('mentions', { static: true }) mentionChild!: NzMentionComponent;
    30. get mention(): AbstractControl {
    31. return this.validateForm.get('mention')!;
    32. }
    33. constructor(private fb: FormBuilder) {}
    34. ngOnInit(): void {
    35. this.validateForm = this.fb.group({
    36. mention: ['@afc163 ', [Validators.required, this.mentionValidator]]
    37. });
    38. }
    39. mentionValidator = (control: FormControl): { [s: string]: boolean } => {
    40. if (!control.value) {
    41. return { required: true };
    42. } else if (this.mentionChild.getMentions().length < 2) {
    43. return { confirm: true, error: true };
    44. }
    45. return {};
    46. };
    47. submitForm(): void {
    48. this.mention.markAsDirty();
    49. this.mention.updateValueAndValidity();
    50. if (this.mention.valid) {
    51. console.log('Submit!!!', this.mention.value);
    52. console.log(this.mentionChild.getMentions());
    53. } else {
    54. console.log('Errors in form!!!');
    55. }
    56. }
    57. resetForm(): void {
    58. this.validateForm?.reset({
    59. mention: '@afc163 '
    60. });
    61. }
    62. }

    Mention提及 - 图9

    无效或只读

    通过 disabled 属性设置是否生效。通过 readOnly 属性设置是否只读。

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-mention-readonly',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <nz-mention [nzSuggestions]="suggestions">
    7. <input
    8. style="margin-bottom: 10px"
    9. placeholder="this is disabled Mention"
    10. nzMentionTrigger
    11. nz-input
    12. disabled
    13. [(ngModel)]="inputValue"
    14. />
    15. <input placeholder="this is readOnly Mention" nzMentionTrigger nz-input readOnly [(ngModel)]="inputValue" />
    16. </nz-mention>
    17. `
    18. })
    19. export class NzDemoMentionReadonlyComponent {
    20. inputValue?: string;
    21. suggestions = ['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご'];
    22. }

    预览

    渲染提及

    1. <nz-mention [nzSuggestions]="suggestions">
    2. <textarea
    3. nz-input
    4. [(ngModel)]="value"
    5. nzMentionTrigger>
    6. </textarea>
    7. </nz-mention>

    方法

    用于指定提及的触发元素

    1. <nz-mention>
    2. <textarea nzMentionTrigger></textarea>
    3. </nz-mention>
    1. <nz-mention>
    2. <input nzMentionTrigger>
    3. </nz-mention>

    自定义建议渲染模板

    1. <nz-mention
    2. [nzSuggestions]="items"
    3. [nzValueWith]="valueWith">
    4. <input nz-input nzMentionTrigger>
    5. <ng-container *nzMentionSuggestion="let item">
    6. <span>{{ item.label }} - {{ item.value }}</span>
    7. </ng-container>
    8. </nz-mention>