Skip to content

Commit d54b3b4

Browse files
authored
feat(module:form): support form label align (#7870)
1 parent 3dfa655 commit d54b3b4

7 files changed

Lines changed: 97 additions & 2 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
order: 15
3+
title:
4+
zh-CN: 表单标签文本对齐方式
5+
en-US: Text align of form label
6+
---
7+
8+
## zh-CN
9+
10+
表单标签文本对齐方式。
11+
12+
## en-US
13+
14+
text align of form label.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
3+
4+
@Component({
5+
selector: 'nz-demo-form-label-align',
6+
template: `
7+
<form nz-form [formGroup]="validateForm">
8+
<nz-form-item>
9+
<nz-form-label nzRequired nzLabelAlign="left" nzSpan="4">Left-aligned text label</nz-form-label>
10+
<nz-form-control nzErrorTip="Please input your username!" nzSpan="8">
11+
<input formControlName="userName" nz-input placeholder="Username" />
12+
</nz-form-control>
13+
</nz-form-item>
14+
<nz-form-item>
15+
<nz-form-label nzRequired nzLabelAlign="right" nzSpan="4">Right-aligned text label</nz-form-label>
16+
<nz-form-control nzErrorTip="Please input your Password!" nzSpan="8">
17+
<input formControlName="password" nz-input type="password" placeholder="Password" />
18+
</nz-form-control>
19+
</nz-form-item>
20+
</form>
21+
`
22+
})
23+
export class NzDemoFormLabelAlignComponent implements OnInit {
24+
validateForm!: UntypedFormGroup;
25+
26+
submitForm(): void {
27+
console.log('submit', this.validateForm.value);
28+
}
29+
30+
constructor(private fb: UntypedFormBuilder) {}
31+
32+
ngOnInit(): void {
33+
this.validateForm = this.fb.group({
34+
userName: [null, [Validators.required]],
35+
password: [null, [Validators.required]],
36+
remember: [true]
37+
});
38+
}
39+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { NzFormModule } from 'ng-zorro-antd/form';
5858
| `[nzDisableAutoTips]`| Set default props `[nzDisableAutoTip]` value of `nz-form-control` | `boolean` | `false` ||
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' }` ||
61+
| `[nzLabelAlign]`| Set default props `[nzLabelAlign]` value of `nz-form-label` | `'left' \| 'right'` | `'right'` |
6162

6263
### nz-form-item
6364

@@ -78,6 +79,8 @@ The label of the form item, optional.
7879
| `[nzFor]`| The `for` property of `label` | `string` | - |
7980
| `[nzTooltipTitle]`| Set tooltip info | `string \| TemplateRef<void>` | - |
8081
| `[nzTooltipIcon]`| Set icon of tooltip info | `string \| NzFormTooltipIcon` | - |
82+
| `[nzLabelAlign]`| The text align of label | `'left' \| 'right'` | `'right'` |
83+
8184

8285
### nz-form-control
8386
> Note:Due to the lack of partial Observable in [Angular Form](https://github.com/angular/angular/issues/10887), you have to notify `nz-form-control` to update its status with `updateValueAndValidity` when you update form status using methods like `markAsDirty`.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { NzFormModule } from 'ng-zorro-antd/form';
5858
| `[nzDisableAutoTips]`| 配置 `nz-form-control``[nzDisableAutoTips]` 的默认值 | `boolean` | `false` ||
5959
| `[nzNoColon]`| 配置 `nz-form-label``[nzNoColon]` 的默认值 | `boolean` | `false` ||
6060
| `[nzTooltipIcon]`| 配置 `nz-form-label``[nzTooltipIcon]` 的默认值 | `string \| { type: string; theme: ThemeType }` | `{ type: 'question-circle', theme: 'outline' }` ||
61+
| `[nzLabelAlign]`| 配置 `nz-form-label``[nzLabelAlign]` 的默认值 | `'left' \| 'right'` | `'right'` |
6162

6263
### nz-form-item
6364

@@ -78,6 +79,8 @@ import { NzFormModule } from 'ng-zorro-antd/form';
7879
| `[nzFor]`| label 标签的 for 属性 | `string` | - |
7980
| `[nzTooltipTitle]`| 配置提示信息 | `string \| TemplateRef<void>` | - |
8081
| `[nzTooltipIcon]`| 配置提示信息的图标 | `string \| NzFormTooltipIcon` | - |
82+
| `[nzLabelAlign]`| 标签文本对齐方式 | `'left' \| 'right'` | `'right'` |
83+
8184

8285
### nz-form-control
8386

components/form/form-label.component.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ThemeType } from '@ant-design/icons-angular';
2323
import { BooleanInput, NzTSType } from 'ng-zorro-antd/core/types';
2424
import { InputBoolean, toBoolean } from 'ng-zorro-antd/core/util';
2525

26-
import { DefaultTooltipIcon, NzFormDirective } from './form.directive';
26+
import { DefaultTooltipIcon, NzFormDirective, NzLabelAlignType } from './form.directive';
2727

2828
export interface NzFormTooltipIcon {
2929
type: NzTSType;
@@ -50,7 +50,10 @@ function toTooltipIcon(value: string | NzFormTooltipIcon): Required<NzFormToolti
5050
</ng-container>
5151
</span>
5252
</label>
53-
`
53+
`,
54+
host: {
55+
'[class.ant-form-item-label-left]': `nzLabelAlign === 'left'`
56+
}
5457
})
5558
export class NzFormLabelComponent implements OnDestroy {
5659
static ngAcceptInputType_nzRequired: BooleanInput;
@@ -81,6 +84,17 @@ export class NzFormLabelComponent implements OnDestroy {
8184
}
8285
private _tooltipIcon: NzFormTooltipIcon | 'default' = 'default';
8386

87+
@Input()
88+
set nzLabelAlign(value: NzLabelAlignType) {
89+
this.labelAlign = value;
90+
}
91+
92+
get nzLabelAlign(): NzLabelAlignType {
93+
return this.labelAlign !== 'default' ? this.labelAlign : this.nzFormDirective?.nzLabelAlign || 'right';
94+
}
95+
96+
private labelAlign: NzLabelAlignType | 'default' = 'default';
97+
8498
private destroy$ = new Subject();
8599

86100
constructor(
@@ -107,6 +121,14 @@ export class NzFormLabelComponent implements OnDestroy {
107121
takeUntil(this.destroy$)
108122
)
109123
.subscribe(() => this.cdr.markForCheck());
124+
125+
this.nzFormDirective
126+
.getInputObservable('nzLabelAlign')
127+
.pipe(
128+
filter(() => this.labelAlign === 'default'),
129+
takeUntil(this.destroy$)
130+
)
131+
.subscribe(() => this.cdr.markForCheck());
110132
}
111133
}
112134

components/form/form-label.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ describe('nz-form-label', () => {
5959
expect(label.nativeElement.querySelector('.ant-form-item-tooltip')).toBeDefined();
6060
expect(label.nativeElement.querySelector('.anticon-info-circle')).toBeDefined();
6161
});
62+
it('should label align work', () => {
63+
expect(label.nativeElement.classList).not.toContain('ant-form-item-label-left');
64+
65+
testComponent.align = 'left';
66+
67+
testBed.fixture.detectChanges();
68+
69+
expect(label.nativeElement.classList).toContain('ant-form-item-label-left');
70+
});
6271
});
6372
});
6473

@@ -70,6 +79,7 @@ describe('nz-form-label', () => {
7079
[nzRequired]="required"
7180
[nzTooltipTitle]="tooltipTitle"
7281
[nzTooltipIcon]="tooltipIcon"
82+
[nzLabelAlign]="align"
7383
></nz-form-label>
7484
`
7585
})
@@ -79,4 +89,5 @@ export class NzTestFormLabelComponent {
7989
noColon = false;
8090
tooltipTitle?: string;
8191
tooltipIcon?: string | NzFormTooltipIcon;
92+
align = 'right';
8293
}

components/form/form.directive.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'form';
2828

2929
export type NzFormLayoutType = 'horizontal' | 'vertical' | 'inline';
3030

31+
export type NzLabelAlignType = 'left' | 'right';
32+
3133
export const DefaultTooltipIcon = {
3234
type: 'question-circle',
3335
theme: 'outline'
@@ -53,6 +55,7 @@ export class NzFormDirective implements OnChanges, OnDestroy, InputObservable {
5355
@Input() @WithConfig() nzAutoTips: Record<string, Record<string, string>> = {};
5456
@Input() @InputBoolean() nzDisableAutoTips = false;
5557
@Input() @WithConfig() nzTooltipIcon: string | { type: string; theme: ThemeType } = DefaultTooltipIcon;
58+
@Input() nzLabelAlign: NzLabelAlignType = 'right';
5659

5760
dir: Direction = 'ltr';
5861
destroy$ = new Subject();

0 commit comments

Comments
 (0)