分段控制器

  • 用于展示多个选项并允许用户选择其中单个选项;

  • 当切换选中选项时,关联区域的内容会发生变化。

如何使用

非内置模块,需要额外在 json-schema 注册 withSegmentedWidget

代码演示

基础样例

最简单的用法。

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

import { YelonFormModule, SFSchema } from '@yelon/form';
import { SFSegmentedWidgetSchema, SegmentedWidget } from '@yelon/form/widgets/segmented';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzSegmentedOptions } from 'ng-zorro-antd/segmented';

@Component({
  selector: 'form-segmented-simple',
  template: `<sf [schema]="schema" (formSubmit)="submit($event)" />`,
  standalone: true,
  imports: [YelonFormModule]
})
export class FormSegmentedSimpleComponent {
  private readonly msg = inject(NzMessageService);
  schema: SFSchema = {
    properties: {
      base: {
        type: 'string',
        title: 'Base',
        default: 2,
        enum: ['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly'],
        ui: {
          widget: SegmentedWidget.KEY,
          valueChange: console.log
        } as SFSegmentedWidgetSchema
      },
      asyncData: {
        type: 'string',
        title: 'Async Data',
        ui: {
          widget: SegmentedWidget.KEY,
          asyncData: () =>
            of([
              { label: 'Label1', value: 'a' },
              { label: 'Label2', value: 'b' },
              { label: 'Label3', value: 'c', disabled: true }
            ] as NzSegmentedOptions).pipe(delay(1000)),
          valueChange: console.log
        } as SFSegmentedWidgetSchema
      }
    }
  };

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

API

ui属性

成员说明类型默认值
[block]将宽度调整为父元素宽度的选项booleanfalse
[asyncData]异步数据() => Observable<NzSegmentedOptions>-
(valueChange)当前选中项目变化时触发回调(data: { index: number; item: SFValue }) => void-
Loading...