Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions components/input/demo/advance-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 18
title:
zh-CN: 定制计数能力
en-US: Custom count logic
---

## zh-CN

在某些场景下,需要定制计数能力(例如 emoji 长度以 1 计算),可以通过 `nzShowCount` 属性来实现。在该模式下,通过 `nzCountMax` / `nzCountStrategy` / `nzExceedFormatter` 属性来超出原生 `maxLength` 的限制。

## en-US

It is necessary to customize the counting ability in some scenarios (such as emoji length is counted as 1), which can be achieved through the `nzShowCount` attribute. Use `nzCountMax` / `nzCountStrategy` / `nzExceedFormatter` attribute exceeds the limit of the native `maxLength`.
94 changes: 94 additions & 0 deletions components/input/demo/advance-count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Component, inject } from '@angular/core';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';

import { NzFormModule } from 'ng-zorro-antd/form';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzInputModule } from 'ng-zorro-antd/input';

@Component({
selector: 'nz-demo-input-advance-count',
imports: [FormsModule, ReactiveFormsModule, NzFormModule, NzInputModule, NzIconModule],
template: `
<form nz-form [formGroup]="form" nzLayout="vertical">
<nz-form-item>
<nz-form-label><h4>ShowCount</h4></nz-form-label>
<nz-form-control>
<nz-input-wrapper nzAllowClear nzShowCount>
<input nz-input formControlName="test_0" />
</nz-input-wrapper>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label><h4>Exceed Max</h4></nz-form-label>
<nz-form-control>
<nz-input-wrapper nzShowCount [nzCountMax]="10">
<input nz-input formControlName="test_1" />
</nz-input-wrapper>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label><h4>Emoji count as length 1</h4></nz-form-label>
<nz-form-control>
<nz-input-wrapper [nzShowCount]="true" [nzCountMax]="6" [nzCountStrategy]="countStrategy">
<input nz-input formControlName="test_2" />
</nz-input-wrapper>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label><h4>Not exceed max</h4></nz-form-label>
<nz-form-control>
<nz-input-wrapper
nzShowCount
[nzCountMax]="10"
[nzCountStrategy]="countStrategy"
[nzExceedFormatter]="exceedFormatterFn"
>
<input nz-input formControlName="test_3" />
</nz-input-wrapper>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label><h4>nz-input-password</h4></nz-form-label>
<nz-form-control>
<nz-input-password
[nzVisibilityToggle]="false"
nzShowCount
[nzCountMax]="20"
[nzCountStrategy]="countStrategy"
>
<input nz-input formControlName="test_4" />
</nz-input-password>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label><h4>nz-input-search</h4></nz-form-label>
<nz-form-control>
<nz-input-search nzShowCount [nzCountMax]="20" [nzCountStrategy]="countStrategy">
<input nz-input formControlName="test_5" />
</nz-input-search>
</nz-form-control>
</nz-form-item>
</form>
`
})
export class NzDemoInputAdvanceCountComponent {
private fb = inject(FormBuilder);
form = this.fb.group({
test_0: ['Angular & NG-ZORRO'],
test_1: ['Angular & NG-ZORRO'],
test_2: ['🔥🔥🔥'],
test_3: ['AAA🔥🔥🔥'],
test_4: ['BBB'],
test_5: ['CCC']
});

countStrategy: (v: string) => number = (v: string) => runes(v).length;
exceedFormatterFn: (cur: string, max: number) => string = (v: string, m: number) => {
const result = runes(v).slice(0, m).join('');
return result;
};
}

function runes(str: string): string[] {
return [...str];
}
20 changes: 12 additions & 8 deletions components/input/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ You can get instance by `ViewChild`

Use when you need to add extra functionality to `[nz-input]`.

| Property | Description | Type | Default |
| ----------------- | --------------------------------------------------------------------- | ------------------------ | ------- |
| `[nzAddonBefore]` | The label text displayed before (on the left side of) the input field | `string` | - |
| `[nzAddonAfter]` | The label text displayed after (on the right side of) the input field | `string` | - |
| `[nzPrefix]` | The prefix icon for the Input | `string` | - |
| `[nzSuffix]` | The suffix icon for the Input | `string` | - |
| `[nzAllowClear]` | If allow to remove input content with clear icon | `boolean` | `false` |
| `(nzClear)` | Event emitted when the clear icon is clicked | `OutputEmitterRef<void>` | - |
| Property | Description | Type | Default |
| --------------------- | ---------------------------------------------------------------------------------------- | ---------------------------------- | ----------------------------- |
| `[nzAddonBefore]` | The label text displayed before (on the left side of) the input field | `string` | - |
| `[nzAddonAfter]` | The label text displayed after (on the right side of) the input field | `string` | - |
| `[nzPrefix]` | The prefix icon for the Input | `string` | - |
| `[nzSuffix]` | The suffix icon for the Input | `string` | - |
| `[nzAllowClear]` | If allow to remove input content with clear icon | `boolean` | `false` |
| `[nzShowCount]` | Should the character count be displayed | `boolean` | `false` |
| `[nzCountMax]` | Maximum character limit: exceeding will be highlighted in red but will not be truncated. | `number` | - |
| `[nzCountStrategy]` | Custom character counting strategy | `(v: string) => number` | `(v: string) => v.length` |
| `[nzExceedFormatter]` | Trimming logic when the number of characters exceeds `nzCountMax` | `(v: string, m: number) => string` | `(v: string, m: number) => v` |
| `(nzClear)` | Event emitted when the clear icon is clicked | `OutputEmitterRef<void>` | - |

### nz-input-password

Expand Down
20 changes: 12 additions & 8 deletions components/input/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ description: 通过鼠标或键盘输入内容,是最基础的表单域的包

当需要为 `[nz-input]` 添加额外功能时使用。

| 参数 | 说明 | 类型 | 默认值 |
| ----------------- | ---------------------------- | ------------------------ | ------- |
| `[nzAddonBefore]` | 带标签的 input,设置前置标签 | `string` | - |
| `[nzAddonAfter]` | 带标签的 input,设置后置标签 | `string` | - |
| `[nzPrefix]` | 带有前缀图标的 input | `string` | - |
| `[nzSuffix]` | 带有后缀图标的 input | `string` | - |
| `[nzAllowClear]` | 可以点击清除图标删除内容 | `boolean` | `false` |
| `(nzClear)` | 点击清除图标时触发 | `OutputEmitterRef<void>` | - |
| 参数 | 说明 | 类型 | 默认值 |
| --------------------- | ------------------------------------ | ---------------------------------- | ----------------------------- |
| `[nzAddonBefore]` | 带标签的 input,设置前置标签 | `string` | - |
| `[nzAddonAfter]` | 带标签的 input,设置后置标签 | `string` | - |
| `[nzPrefix]` | 带有前缀图标的 input | `string` | - |
| `[nzSuffix]` | 带有后缀图标的 input | `string` | - |
| `[nzAllowClear]` | 可以点击清除图标删除内容 | `boolean` | `false` |
| `[nzShowCount]` | 是否显示字符计数 | `boolean` | `false` |
| `[nzCountMax]` | 最大字符数,超出后标红但不会截断 | `number` | - |
| `[nzCountStrategy]` | 自定义字符计数策略 | `(v: string) => number` | `(v: string) => v.length` |
| `[nzExceedFormatter]` | 当字符数超出`nzCountMax`时的裁剪逻辑 | `(v: string, m: number) => string` | `(v: string, m: number) => v` |
| `(nzClear)` | 点击清除图标时触发 | `OutputEmitterRef<void>` | - |

### nz-input-password

Expand Down
Loading