Skip to content

Commit

Permalink
feat(module:form): support form label wrap (#7892)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBeard30 committed Jul 16, 2023
1 parent 71c2138 commit 37391de
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 2 deletions.
14 changes: 14 additions & 0 deletions components/form/demo/label-wrap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 14
title:
zh-CN: 表单标签可换行
en-US: Label can wrap
---

## zh-CN

使用 `nzLabelWrap` 可以开启 `label` 换行。

## en-US

Turn on `nzLabelWrap` to wrap label if text is long.
46 changes: 46 additions & 0 deletions components/form/demo/label-wrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Component, OnInit } from '@angular/core';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';

@Component({
selector: 'nz-demo-form-label-wrap',
template: `
<form nz-form [formGroup]="validateForm" (ngSubmit)="submitForm()">
<nz-form-item>
<nz-form-label nzRequired nzFor="user" nzSpan="3"> Normal text label </nz-form-label>
<nz-form-control nzErrorTip="Please input your username!" nzSpan="8">
<input formControlName="userName" nz-input id="user" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label nzRequired nzFor="password" nzSpan="3" nzLabelWrap>
Long text label Long text label
</nz-form-label>
<nz-form-control nzErrorTip="Please input your Password!" nzSpan="8">
<input formControlName="password" nz-input type="password" id="password" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-control nzSpan="12" style="text-align: center">
<button nz-button nzType="primary">Log in</button>
</nz-form-control>
</nz-form-item>
</form>
`
})
export class NzDemoFormLabelWrapComponent implements OnInit {
validateForm!: UntypedFormGroup;

submitForm(): void {
console.log('submit', this.validateForm.value);
}

constructor(private fb: UntypedFormBuilder) {}

ngOnInit(): void {
this.validateForm = this.fb.group({
userName: [null, [Validators.required]],
password: [null, [Validators.required]],
remember: [true]
});
}
}
3 changes: 3 additions & 0 deletions components/form/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import { NzFormModule } from 'ng-zorro-antd/form';
| `[nzNoColon]`| Set default props `[nzNoColon]` value of `nz-form-label` | `boolean` | `false` ||
| `[nzTooltipIcon]`| Set default props `[nzTooltipIcon]` value of `nz-form-label` | `string \| { type: string; theme: ThemeType }` | `{ type: 'question-circle', theme: 'outline' }` ||
| `[nzLabelAlign]`| Set default props `[nzLabelAlign]` value of `nz-form-label` | `'left' \| 'right'` | `'right'` |
| `[nzLabelWrap]`| Set default props `[nzLabelWrap]` value of `nz-form-label` | `boolean` | `false` |


### nz-form-item

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


### nz-form-control
Expand Down
4 changes: 3 additions & 1 deletion components/form/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import { NzFormModule } from 'ng-zorro-antd/form';
| `[nzNoColon]`| 配置 `nz-form-label``[nzNoColon]` 的默认值 | `boolean` | `false` ||
| `[nzTooltipIcon]`| 配置 `nz-form-label``[nzTooltipIcon]` 的默认值 | `string \| { type: string; theme: ThemeType }` | `{ type: 'question-circle', theme: 'outline' }` ||
| `[nzLabelAlign]`| 配置 `nz-form-label``[nzLabelAlign]` 的默认值 | `'left' \| 'right'` | `'right'` |
| `[nzLabelWrap]`| 配置 `nz-form-label``[nzLabelWrap]` 的默认值 | `boolean` | `false` |


### nz-form-item

Expand All @@ -80,7 +82,7 @@ import { NzFormModule } from 'ng-zorro-antd/form';
| `[nzTooltipTitle]`| 配置提示信息 | `string \| TemplateRef<void>` | - |
| `[nzTooltipIcon]`| 配置提示信息的图标 | `string \| NzFormTooltipIcon` | - |
| `[nzLabelAlign]`| 标签文本对齐方式 | `'left' \| 'right'` | `'right'` |

| `[nzLabelWrap]`| label 标签的文本换行方式 | `boolean` | `false` |

### nz-form-control

Expand Down
23 changes: 22 additions & 1 deletion components/form/form-label.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ function toTooltipIcon(value: string | NzFormTooltipIcon): Required<NzFormToolti
`,
host: {
class: 'ant-form-item-label',
'[class.ant-form-item-label-left]': `nzLabelAlign === 'left'`
'[class.ant-form-item-label-left]': `nzLabelAlign === 'left'`,
'[class.ant-form-item-label-wrap]': `nzLabelWrap`
}
})
export class NzFormLabelComponent implements OnDestroy {
static ngAcceptInputType_nzRequired: BooleanInput;
static ngAcceptInputType_nzNoColon: BooleanInput;
static ngAcceptInputType_nzLabelWrap: BooleanInput;

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

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

@Input()
set nzLabelWrap(value: boolean) {
this.labelWrap = toBoolean(value);
}

get nzLabelWrap(): boolean {
return this.labelWrap !== 'default' ? this.labelWrap : this.nzFormDirective?.nzLabelWrap;
}

private labelWrap: boolean | 'default' = 'default';

private destroy$ = new Subject<boolean>();

constructor(private cdr: ChangeDetectorRef, @Optional() @SkipSelf() private nzFormDirective: NzFormDirective) {
Expand Down Expand Up @@ -121,6 +134,14 @@ export class NzFormLabelComponent implements OnDestroy {
takeUntil(this.destroy$)
)
.subscribe(() => this.cdr.markForCheck());

this.nzFormDirective
.getInputObservable('nzLabelWrap')
.pipe(
filter(() => this.labelWrap === 'default'),
takeUntil(this.destroy$)
)
.subscribe(() => this.cdr.markForCheck());
}
}

Expand Down
11 changes: 11 additions & 0 deletions components/form/form-label.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ describe('nz-form-label', () => {

expect(label.nativeElement.classList).toContain('ant-form-item-label-left');
});

it('should label wrap work', () => {
expect(label.nativeElement.classList).not.toContain('ant-form-item-label-wrap');

testComponent.labelWrap = true;
testBed.fixture.detectChanges();

expect(label.nativeElement.classList).toContain('ant-form-item-label-wrap');
});
});
});

Expand All @@ -80,6 +89,7 @@ describe('nz-form-label', () => {
[nzTooltipTitle]="tooltipTitle"
[nzTooltipIcon]="tooltipIcon"
[nzLabelAlign]="align"
[nzLabelWrap]="labelWrap"
></nz-form-label>
`
})
Expand All @@ -90,4 +100,5 @@ export class NzTestFormLabelComponent {
tooltipTitle?: string;
tooltipIcon?: string | NzFormTooltipIcon;
align = 'right';
labelWrap = false;
}
2 changes: 2 additions & 0 deletions components/form/form.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ export class NzFormDirective implements OnChanges, OnDestroy, InputObservable {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
static ngAcceptInputType_nzNoColon: BooleanInput;
static ngAcceptInputType_nzDisableAutoTips: BooleanInput;
static ngAcceptInputType_nzLabelWrap: BooleanInput;

@Input() nzLayout: NzFormLayoutType = 'horizontal';
@Input() @WithConfig() @InputBoolean() nzNoColon: boolean = false;
@Input() @WithConfig() nzAutoTips: Record<string, Record<string, string>> = {};
@Input() @InputBoolean() nzDisableAutoTips = false;
@Input() @WithConfig() nzTooltipIcon: string | { type: string; theme: ThemeType } = DefaultTooltipIcon;
@Input() nzLabelAlign: NzLabelAlignType = 'right';
@Input() @WithConfig() @InputBoolean() nzLabelWrap: boolean = false;

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

0 comments on commit 37391de

Please sign in to comment.