对象

创建对象,只对 schema.type="object" 时有效。

代码演示

基础样例

最简单的用法。

expand code expand code
import { Component, inject } from '@angular/core';

import { YelonFormModule, SFSchema } from '@yelon/form';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'form-object-simple',
  template: `<sf [schema]="schema" (formSubmit)="submit($event)" />`,
  standalone: true,
  imports: [YelonFormModule]
})
export class FormObjectSimpleComponent {
  private readonly msg = inject(NzMessageService);
  schema: SFSchema = {
    properties: {
      name: { type: 'string' },
      age: { type: 'number' }
    },
    required: ['name', 'age'],
    ui: {
      // 指定 `label` 和 `control` 在一行中各占栅格数
      spanLabel: 4,
      spanControl: 5
    }
  };

  submit(value: {}): void {
    this.msg.success(JSON.stringify(value));
  }
}
卡片容器

包含标题、内容、操作区域。

expand code expand code
import { Component, OnInit, TemplateRef, ViewChild, inject } from '@angular/core';

import { YelonFormModule, SFObjectWidgetSchema, SFSchema } from '@yelon/form';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'form-object-card',
  template: `
    @if (schema) {
      <sf [schema]="schema" (formSubmit)="submit($event)" />
    }
    <ng-template #extra>
      <a (click)="msg.success('Success')">More</a>
    </ng-template>
  `,
  standalone: true,
  imports: [YelonFormModule]
})
export class FormObjectCardComponent implements OnInit {
  readonly msg = inject(NzMessageService);
  @ViewChild('extra', { static: true }) private extra!: TemplateRef<void>;
  schema?: SFSchema;

  ngOnInit(): void {
    this.schema = {
      properties: {
        name: { type: 'string' },
        age: { type: 'number' },
        address1: {
          title: '地址1',
          type: 'object',
          properties: {
            country: { type: 'string' },
            city: { type: 'string' },
            zone: { type: 'string' }
          },
          ui: {
            type: 'card',
            cardExtra: this.extra
          } as SFObjectWidgetSchema
        },
        address2: {
          title: '地址2',
          type: 'object',
          properties: {
            country: { type: 'string' },
            city: { type: 'string' },
            zone: { type: 'string' }
          },
          ui: {
            type: 'card',
            showExpand: false
          } as SFObjectWidgetSchema
        }
      },
      required: ['name', 'age'],
      ui: {
        spanLabelFixed: 150,
        grid: { span: 12, gutter: 16 }
      } as SFObjectWidgetSchema
    };
  }

  submit(value: {}): void {
    this.msg.success(JSON.stringify(value));
  }
}

API

schema属性

参数说明类型默认值
[properties]定义对象的属性{ [key: string]: SFSchema }-
[required]必填项属性string[]-
[maxProperties]最大属性个数,必须是非负整数number-
[minProperties]最小属性个数,必须是非负整数number-

ui属性

参数说明类型默认值
[showExpand]是否显示扩展,点击隐藏内容,限 type === 'card'booleantrue
[expand]展开状态,限 type === 'card'booleantrue
[showTitle]是否显示标题booleanfalse
[type]渲染类型card, defaultdefault
[cardSize]等同 nzSize 属性small, defaultsmall
[cardBodyStyle]等同 nzBodyStyle 属性{ [key: string]: string }-
[cardBorderless]等同 nzBorderless 属性booleanfalse
[cardExtra]等同 nzExtra 属性string, TemplateRef<void>-
[cardActions]等同 nzActions 属性Array<TemplateRef>-
Loading...