Skip to content

Commit 37391de

Browse files
authored
feat(module:form): support form label wrap (#7892)
1 parent 71c2138 commit 37391de

File tree

7 files changed

+101
-2
lines changed

7 files changed

+101
-2
lines changed

components/form/demo/label-wrap.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
order: 14
3+
title:
4+
zh-CN: 表单标签可换行
5+
en-US: Label can wrap
6+
---
7+
8+
## zh-CN
9+
10+
使用 `nzLabelWrap` 可以开启 `label` 换行。
11+
12+
## en-US
13+
14+
Turn on `nzLabelWrap` to wrap label if text is long.

components/form/demo/label-wrap.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
3+
4+
@Component({
5+
selector: 'nz-demo-form-label-wrap',
6+
template: `
7+
<form nz-form [formGroup]="validateForm" (ngSubmit)="submitForm()">
8+
<nz-form-item>
9+
<nz-form-label nzRequired nzFor="user" nzSpan="3"> Normal text label </nz-form-label>
10+
<nz-form-control nzErrorTip="Please input your username!" nzSpan="8">
11+
<input formControlName="userName" nz-input id="user" />
12+
</nz-form-control>
13+
</nz-form-item>
14+
<nz-form-item>
15+
<nz-form-label nzRequired nzFor="password" nzSpan="3" nzLabelWrap>
16+
Long text label Long text label
17+
</nz-form-label>
18+
<nz-form-control nzErrorTip="Please input your Password!" nzSpan="8">
19+
<input formControlName="password" nz-input type="password" id="password" />
20+
</nz-form-control>
21+
</nz-form-item>
22+
<nz-form-item>
23+
<nz-form-control nzSpan="12" style="text-align: center">
24+
<button nz-button nzType="primary">Log in</button>
25+
</nz-form-control>
26+
</nz-form-item>
27+
</form>
28+
`
29+
})
30+
export class NzDemoFormLabelWrapComponent implements OnInit {
31+
validateForm!: UntypedFormGroup;
32+
33+
submitForm(): void {
34+
console.log('submit', this.validateForm.value);
35+
}
36+
37+
constructor(private fb: UntypedFormBuilder) {}
38+
39+
ngOnInit(): void {
40+
this.validateForm = this.fb.group({
41+
userName: [null, [Validators.required]],
42+
password: [null, [Validators.required]],
43+
remember: [true]
44+
});
45+
}
46+
}

components/form/doc/index.en-US.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import { NzFormModule } from 'ng-zorro-antd/form';
5959
| `[nzNoColon]`| Set default props `[nzNoColon]` value of `nz-form-label` | `boolean` | `false` ||
6060
| `[nzTooltipIcon]`| Set default props `[nzTooltipIcon]` value of `nz-form-label` | `string \| { type: string; theme: ThemeType }` | `{ type: 'question-circle', theme: 'outline' }` ||
6161
| `[nzLabelAlign]`| Set default props `[nzLabelAlign]` value of `nz-form-label` | `'left' \| 'right'` | `'right'` |
62+
| `[nzLabelWrap]`| Set default props `[nzLabelWrap]` value of `nz-form-label` | `boolean` | `false` |
63+
6264

6365
### nz-form-item
6466

@@ -80,6 +82,7 @@ The label of the form item, optional.
8082
| `[nzTooltipTitle]`| Set tooltip info | `string \| TemplateRef<void>` | - |
8183
| `[nzTooltipIcon]`| Set icon of tooltip info | `string \| NzFormTooltipIcon` | - |
8284
| `[nzLabelAlign]`| The text align of label | `'left' \| 'right'` | `'right'` |
85+
| `[nzLabelWrap]`| whether label can be wrap | `boolean` | `false` |
8386

8487

8588
### nz-form-control

components/form/doc/index.zh-CN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import { NzFormModule } from 'ng-zorro-antd/form';
5959
| `[nzNoColon]`| 配置 `nz-form-label``[nzNoColon]` 的默认值 | `boolean` | `false` ||
6060
| `[nzTooltipIcon]`| 配置 `nz-form-label``[nzTooltipIcon]` 的默认值 | `string \| { type: string; theme: ThemeType }` | `{ type: 'question-circle', theme: 'outline' }` ||
6161
| `[nzLabelAlign]`| 配置 `nz-form-label``[nzLabelAlign]` 的默认值 | `'left' \| 'right'` | `'right'` |
62+
| `[nzLabelWrap]`| 配置 `nz-form-label``[nzLabelWrap]` 的默认值 | `boolean` | `false` |
63+
6264

6365
### nz-form-item
6466

@@ -80,7 +82,7 @@ import { NzFormModule } from 'ng-zorro-antd/form';
8082
| `[nzTooltipTitle]`| 配置提示信息 | `string \| TemplateRef<void>` | - |
8183
| `[nzTooltipIcon]`| 配置提示信息的图标 | `string \| NzFormTooltipIcon` | - |
8284
| `[nzLabelAlign]`| 标签文本对齐方式 | `'left' \| 'right'` | `'right'` |
83-
85+
| `[nzLabelWrap]`| label 标签的文本换行方式 | `boolean` | `false` |
8486

8587
### nz-form-control
8688

components/form/form-label.component.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ function toTooltipIcon(value: string | NzFormTooltipIcon): Required<NzFormToolti
5151
`,
5252
host: {
5353
class: 'ant-form-item-label',
54-
'[class.ant-form-item-label-left]': `nzLabelAlign === 'left'`
54+
'[class.ant-form-item-label-left]': `nzLabelAlign === 'left'`,
55+
'[class.ant-form-item-label-wrap]': `nzLabelWrap`
5556
}
5657
})
5758
export class NzFormLabelComponent implements OnDestroy {
5859
static ngAcceptInputType_nzRequired: BooleanInput;
5960
static ngAcceptInputType_nzNoColon: BooleanInput;
61+
static ngAcceptInputType_nzLabelWrap: BooleanInput;
6062

6163
@Input() nzFor?: string;
6264
@Input() @InputBoolean() nzRequired = false;
@@ -94,6 +96,17 @@ export class NzFormLabelComponent implements OnDestroy {
9496

9597
private labelAlign: NzLabelAlignType | 'default' = 'default';
9698

99+
@Input()
100+
set nzLabelWrap(value: boolean) {
101+
this.labelWrap = toBoolean(value);
102+
}
103+
104+
get nzLabelWrap(): boolean {
105+
return this.labelWrap !== 'default' ? this.labelWrap : this.nzFormDirective?.nzLabelWrap;
106+
}
107+
108+
private labelWrap: boolean | 'default' = 'default';
109+
97110
private destroy$ = new Subject<boolean>();
98111

99112
constructor(private cdr: ChangeDetectorRef, @Optional() @SkipSelf() private nzFormDirective: NzFormDirective) {
@@ -121,6 +134,14 @@ export class NzFormLabelComponent implements OnDestroy {
121134
takeUntil(this.destroy$)
122135
)
123136
.subscribe(() => this.cdr.markForCheck());
137+
138+
this.nzFormDirective
139+
.getInputObservable('nzLabelWrap')
140+
.pipe(
141+
filter(() => this.labelWrap === 'default'),
142+
takeUntil(this.destroy$)
143+
)
144+
.subscribe(() => this.cdr.markForCheck());
124145
}
125146
}
126147

components/form/form-label.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ describe('nz-form-label', () => {
6868

6969
expect(label.nativeElement.classList).toContain('ant-form-item-label-left');
7070
});
71+
72+
it('should label wrap work', () => {
73+
expect(label.nativeElement.classList).not.toContain('ant-form-item-label-wrap');
74+
75+
testComponent.labelWrap = true;
76+
testBed.fixture.detectChanges();
77+
78+
expect(label.nativeElement.classList).toContain('ant-form-item-label-wrap');
79+
});
7180
});
7281
});
7382

@@ -80,6 +89,7 @@ describe('nz-form-label', () => {
8089
[nzTooltipTitle]="tooltipTitle"
8190
[nzTooltipIcon]="tooltipIcon"
8291
[nzLabelAlign]="align"
92+
[nzLabelWrap]="labelWrap"
8393
></nz-form-label>
8494
`
8595
})
@@ -90,4 +100,5 @@ export class NzTestFormLabelComponent {
90100
tooltipTitle?: string;
91101
tooltipIcon?: string | NzFormTooltipIcon;
92102
align = 'right';
103+
labelWrap = false;
93104
}

components/form/form.directive.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ export class NzFormDirective implements OnChanges, OnDestroy, InputObservable {
4040
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
4141
static ngAcceptInputType_nzNoColon: BooleanInput;
4242
static ngAcceptInputType_nzDisableAutoTips: BooleanInput;
43+
static ngAcceptInputType_nzLabelWrap: BooleanInput;
4344

4445
@Input() nzLayout: NzFormLayoutType = 'horizontal';
4546
@Input() @WithConfig() @InputBoolean() nzNoColon: boolean = false;
4647
@Input() @WithConfig() nzAutoTips: Record<string, Record<string, string>> = {};
4748
@Input() @InputBoolean() nzDisableAutoTips = false;
4849
@Input() @WithConfig() nzTooltipIcon: string | { type: string; theme: ThemeType } = DefaultTooltipIcon;
4950
@Input() nzLabelAlign: NzLabelAlignType = 'right';
51+
@Input() @WithConfig() @InputBoolean() nzLabelWrap: boolean = false;
5052

5153
dir: Direction = 'ltr';
5254
destroy$ = new Subject<boolean>();

0 commit comments

Comments
 (0)