se 编辑·续1
import { Component } from '@angular/core';import { FormBuilder, FormGroup, Validators } from '@angular/forms';import { SEErrorRefresh } from '@delon/abc/se';import { NzMessageService } from 'ng-zorro-antd/message';@Component({ selector: 'components-se-reset-errors', template: ` <h3>Operating</h3> <div class="mb-md"> <button nz-button (click)="resetErrors()">Reset all errors</button> </div> <form nz-form #f="ngForm" se-container [errors]="ngModelErrors" gutter="32"> <se label="App Key" [error]="{ required: '请填写', pattern: '只能包含a-z, 0-9之间' }"> <input type="text" nz-input [(ngModel)]="i.ak" name="ak" required pattern="^[a-z0-9]*$" placeholder="必填项,且只能包含a-z, 0-9之间" /> </se> <ng-template #appSecretRequired> 请填写,密钥<a (click)="msg.success('success')">生成</a>地址。 </ng-template> <se label="App Secret" [error]="{ required: appSecretRequired, pattern: '只能包含0-9之间' }"> <input type="text" nz-input [(ngModel)]="i.sk" name="sk" required maxlength="32" pattern="^[0-9]*$" placeholder="必填项,且只能包含0-9之间" /> </se> <se> <button nz-button nzType="primary" [disabled]="f.invalid">Save</button> </se> </form> <h3>Reactive</h3> <form nz-form [formGroup]="validateForm" se-container gutter="32" [errors]="reactiveErrors"> <se label="App Key" [error]="{ required: 'Please input your username!', pattern: 'Incorrect format, muse be A' }"> <input formControlName="userName" nz-input placeholder="Username" /> </se> <se label="App Secret" error="Please input your Password!"> <input formControlName="password" nz-input type="password" placeholder="Password" /> </se> <se> <button nz-button nzType="primary" [disabled]="!validateForm.valid">Log in</button> </se> </form> `,})export class ComponentsSeResetErrorsComponent { validateForm: FormGroup; i: any = {}; ngModelErrors: SEErrorRefresh[] = []; reactiveErrors: SEErrorRefresh[] = []; constructor(fb: FormBuilder, public msg: NzMessageService) { this.validateForm = fb.group({ userName: [null, [Validators.required, Validators.pattern(/A/)]], password: [null, [Validators.required]], remember: [true], }); } resetErrors(): void { this.ngModelErrors = [{ name: 'ak', error: 'Required field, and can only contain a-z, 0-9' }]; this.reactiveErrors = [ { name: 'userName', error: 'Required username' }, { name: 'password', error: 'Required password' }, ]; }}
不规则布局
利用 col 可以构建复杂不规则布局。
import { Component } from '@angular/core';@Component({ selector: 'components-se-complex', template: ` <form nz-form #f="ngForm" se-container size="compact" gutter="24"> <se-title>Title 1</se-title> <se label="ID" col="1" [optionalHelp]="optionalHelpTpl"> 1000 <ng-template #optionalHelpTpl> Via by ng-template </ng-template> </se> <se label="Name" required col="3"> <input type="text" nz-input [(ngModel)]="i.user_name" name="user_name" required /> </se> <se label="Age" required col="3"> <nz-select [(ngModel)]="i.user_age" name="user_age" nzAllowClear nzPlaceHolder="Choose"> <nz-option [nzValue]="1" nzLabel="1"></nz-option> <nz-option [nzValue]="2" nzLabel="2"></nz-option> <nz-option [nzValue]="3" nzLabel="3"></nz-option> <nz-option [nzValue]="4" nzLabel="4"></nz-option> <nz-option [nzValue]="5" nzLabel="5"></nz-option> </nz-select> </se> <se label="Brithday" required col="3"> <nz-date-picker [(ngModel)]="i.user_birthday" name="user_birthday" nzShowTime></nz-date-picker> </se> <se label="App Key" required> <input type="text" nz-input [(ngModel)]="i.ak" name="ak" required /> </se> <se label="App Secret" required> <input type="text" nz-input [(ngModel)]="i.sk" name="sk" required maxlength="32" /> </se> <se label="Phone Number" required> <nz-input-group [nzAddOnBefore]="addOnBeforeTemplate"> <ng-template #addOnBeforeTemplate> <nz-select [(ngModel)]="i.phoneNumberPrefix" name="phoneNumberPrefix" style="width: 70px;"> <nz-option nzLabel="+86" nzValue="+86"></nz-option> <nz-option nzLabel="+87" nzValue="+87"></nz-option> </nz-select> </ng-template> <input type="text" nz-input [(ngModel)]="i.phoneNumber" name="phoneNumber" required maxlength="32" /> </nz-input-group> </se> <se> <label nz-checkbox [(ngModel)]="i.agree" name="agree"> <span>I have read the <a>agreement</a></span> </label> </se> <se-title>Title 2</se-title> <se label="Long Long Long Long Long Long Label" col="1"> <textarea [(ngModel)]="i.comment" name="comment" nz-input rows="2" placeholder="write any thing"></textarea> </se> <se col="1"> <button nz-button nzType="primary" [disabled]="f.invalid">Save</button> </se> </form> `,})export class ComponentsSeComplexComponent { i: any = { phoneNumberPrefix: '+86', agree: true, user_age: 3, };}
API
se-container
| [gutter] |
间距,当 nzLayout:horizontal 时有效 |
number |
32 |
✅ |
|
|
|
|
|
| [se-container] |
指定表单元素最多分几列展示,最终一行几列由 col 配置结合响应式规则决定 |
‘1’,’2’,’3’,’4’,’5’,’6’ |
- |
|
|
|
|
|
|
| [col] |
指定表单元素最多分几列展示,最终一行几列由 col 配置结合响应式规则决定 |
‘1’,’2’,’3’,’4’,’5’,’6’ |
- |
✅ |
|
|
|
|
|
| [labelWidth] |
表单元素默认标签文本宽度,单位:px |
number |
150 |
✅ |
|
|
|
|
|
| [nzLayout] |
表单布局,当 inline 时强制大小为 compact |
‘horizontal’,’vertical’,’inline’ |
‘horizontal’ |
✅ |
|
|
|
|
|
| [size] |
大小 compact 紧凑型,强制忽略 error、extra 展示 |
‘default’,’compact’ |
‘default’ |
✅ |
|
|
|
|
|
| [firstVisual] |
是否立即呈现错误视觉 |
boolean |
false |
✅ |
|
|
|
|
|
| [ingoreDirty] |
是否忽略 dirty 校验 |
boolean |
false |
✅ |
|
|
|
|
|
| [title] |
标题 |
string,TemplateRef<void> |
- |
|
|
|
|
|
|
| [errors] |
批量修改 se 错误消息描述 |
SEErrorRefresh[] |
- |
|
|
|
|
|
|
se
| [col] |
指定表单元素最多分几列展示,最终一行几列由 col 配置结合响应式规则决定(继承于 se-container) |
‘1’,’2’,’3’,’4’,’5’,’6’ |
- |
|
|
|
|
| [label] |
标签文本 |
string, TemplateRef<void> |
- |
|
|
|
|
| [labelWidth] |
标签文本宽度,单位:px(继承于 se-container) |
number |
- |
|
|
|
|
| [optional] |
标签可选信息 |
string, TemplateRef<void> |
- |
|
|
|
|
| [optionalHelp] |
标签可选帮助 |
string, TemplateRef<void> |
- |
|
|
|
|
| [error] |
错误描述 |
string, TemplateRef<void>, { [key: string]: string, TemplateRef } |
- |
|
|
|
|
| [extra] |
额外提示信息 |
string, TemplateRef<void> |
- |
|
|
|
|
| [required] |
是否必填项标识符,若不设置自动根据表单元素是否有 RequiredValidator 校验来设置值 |
string |
- |
|
|
|
|
| [controlClass] |
控件区域样式名 |
string |
- |
|
|
|
|
| [line] |
分隔线(继承于 se-container) |
boolean |
- |
|
|
|
|
se-title
用于展示标题,单独一行。
常见问题
什么时候自定义组件id
点击表单的 Label 会将光标定位至相应组件下,而 ng-edit 会自动根据 ngModel 状态合理的设定 id,绝大多数情况下你无须关心 id 的绑定状态,当然若你手动指定 id 值则优先级更高但同时你需要自己维护组件对应的 id 值。