import { Component, ViewChild, inject } from '@angular/core';
import { of, delay } from 'rxjs';
import { YelonFormModule, SFComponent, SFSchema, SFSelectWidgetSchema } from '@yelon/form';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
selector: 'form-select-simple',
template: `
<sf #sf [schema]="schema" (formSubmit)="submit($event)" />
<button nz-button (click)="updateStatus()">Update Status</button>
`,
standalone: true,
imports: [YelonFormModule, NzButtonModule]
})
export class FormSelectSimpleComponent {
private readonly msg = inject(NzMessageService);
@ViewChild('sf', { static: false }) private sf!: SFComponent;
schema: SFSchema = {
properties: {
status: {
type: 'string',
title: '状态',
enum: [
{ label: '待支付', value: 'WAIT_BUYER_PAY', otherData: 1 },
{ label: '已支付', value: 'TRADE_SUCCESS' },
{ label: '交易完成', value: 'TRADE_FINISHED' }
],
default: 'WAIT_BUYER_PAY',
ui: {
widget: 'select',
change: (value, orgData) => console.log(value, orgData)
} as SFSelectWidgetSchema
},
// 标签
tags: {
type: 'string',
title: '标签',
enum: [
{ label: '待支付', value: 'WAIT_BUYER_PAY' },
{ label: '已支付', value: 'TRADE_SUCCESS' },
{ label: '交易完成', value: 'TRADE_FINISHED' }
],
ui: {
widget: 'select',
mode: 'tags'
} as SFSelectWidgetSchema,
default: null
},
// 异步数据
async: {
type: 'string',
title: 'Async',
default: 'WAIT_BUYER_PAY',
ui: {
widget: 'select',
asyncData: () =>
of([
{
label: '订单状态',
group: true,
children: [
{ label: '待支付', value: 'WAIT_BUYER_PAY' },
{ label: '已支付', value: 'TRADE_SUCCESS' },
{ label: '交易完成', value: 'TRADE_FINISHED' }
]
}
]).pipe(delay(1200))
} as SFSelectWidgetSchema
},
hide: {
type: 'string',
title: '隐藏项',
enum: [
{ label: '第1个', value: 1 },
{ label: '第2个', value: 2, hide: true },
{ label: '第3个(第2个被隐藏了)', value: 3 }
],
default: 1
}
}
};
submit(value: {}): void {
this.msg.success(JSON.stringify(value));
}
updateStatus(): void {
const statusProperty = this.sf.getProperty('/status')!;
statusProperty.schema.enum = ['1', '2', '3'];
statusProperty.widget.reset('2');
}
}