|
| 1 | +import { NgTemplateOutlet } from '@angular/common'; |
| 2 | +import { |
| 3 | + Component, |
| 4 | + computed, |
| 5 | + effect, |
| 6 | + inject, |
| 7 | + input, |
| 8 | + linkedSignal, |
| 9 | + model, |
| 10 | + viewChild, |
| 11 | +} from '@angular/core'; |
| 12 | +import { SelectorlessOutlet } from '@cyia/ngx-common/directive'; |
| 13 | +import { PurePipe } from '@cyia/ngx-common/pipe'; |
| 14 | +import { StrOrTemplateComponent } from '@piying-lib/angular-core'; |
| 15 | +import { CssPrefixPipe, MergeClassPipe } from '@piying-lib/angular-daisyui/pipe'; |
| 16 | +import { ThemeService, useTwClass } from '@piying-lib/angular-daisyui/service'; |
| 17 | +import { Size } from '@piying-lib/angular-core'; |
| 18 | +import { AttributesDirective, PiyingViewGroupBase } from '@piying/view-angular'; |
| 19 | +import { FieldLogicGroup } from '@piying/view-angular-core'; |
| 20 | +/** |
| 21 | + * 标签页组 |
| 22 | + * |
| 23 | + * 用于将表单字段组织成多标签页切换展示,支持多种标签样式和位置布局。 |
| 24 | + * 适合字段较多需要分类管理、多配置项分页展示的场景,如系统设置、表单分组等。 |
| 25 | + */ |
| 26 | +@Component({ |
| 27 | + selector: 'app-radio-fgc', |
| 28 | + templateUrl: './component.html', |
| 29 | + imports: [NgTemplateOutlet, AttributesDirective, CssPrefixPipe, MergeClassPipe, PurePipe], |
| 30 | +}) |
| 31 | +export class RadioFGC extends PiyingViewGroupBase { |
| 32 | + static __version = 2; |
| 33 | + |
| 34 | + static index = 0; |
| 35 | + templateRef = viewChild.required('templateRef'); |
| 36 | + readonly StrOrTemplateComponent = StrOrTemplateComponent; |
| 37 | + |
| 38 | + name = `pc-radio-${RadioFGC.index++}`; |
| 39 | + /** 当前激活的标签页索引 */ |
| 40 | + activatedIndex = model(0); |
| 41 | + radioClass = input(useTwClass('pb-4')); |
| 42 | + /** 切换前的回调函数 */ |
| 43 | + beforeChange = input<(index: number) => any>(); |
| 44 | + |
| 45 | + isUnion$$ = computed(() => { |
| 46 | + return ( |
| 47 | + this.field$$().form.control instanceof FieldLogicGroup && |
| 48 | + (this.field$$().form.control as FieldLogicGroup).type() === 'or' |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + labelInputs = (input: any) => { |
| 53 | + return { |
| 54 | + content: input, |
| 55 | + }; |
| 56 | + }; |
| 57 | + constructor() { |
| 58 | + super(); |
| 59 | + effect(() => { |
| 60 | + if (this.isUnion$$()) { |
| 61 | + const index = this.activatedIndex(); |
| 62 | + const control = this.field$$().form.control as FieldLogicGroup; |
| 63 | + control.activateIndex$.set(index); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + changeIndex(index: number) { |
| 68 | + if (this.beforeChange()) { |
| 69 | + this.beforeChange()!(index); |
| 70 | + } |
| 71 | + this.activatedIndex.set(index); |
| 72 | + } |
| 73 | +} |
0 commit comments