Skip to content

Commit

Permalink
feat(form): add provideSFConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 12, 2023
1 parent 065316a commit 6e85870
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/form/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src/form';
export * from './src/index';
14 changes: 5 additions & 9 deletions packages/form/spec/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AlainThemeModule } from '@delon/theme';
import { deepCopy, deepGet } from '@delon/util/other';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

import { SFWidgetProvideConfig, provideSFConfig } from '../src';
import { SF_SEQ } from '../src/const';
import { SFButton } from '../src/interface';
import { FormProperty } from '../src/model/form.property';
Expand Down Expand Up @@ -73,17 +74,12 @@ export function builder(options?: {
};
}

export function configureSFTestSuite(options?: { imports?: NzSafeAny[] }): void {
export function configureSFTestSuite(options?: { imports?: NzSafeAny[]; widgets?: SFWidgetProvideConfig[] }): void {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule,
AlainThemeModule,
DelonFormModule.forRoot(),
HttpClientTestingModule,
...(options?.imports ?? [])
],
declarations: [TestFormComponent]
imports: [NoopAnimationsModule, AlainThemeModule, HttpClientTestingModule, ...(options?.imports ?? [])],
declarations: [TestFormComponent],
providers: [provideSFConfig({ widgets: options?.widgets })]
});
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/form/src/form.ts → packages/form/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './model/index';
export * from './widget';
export * from './widgets/index';
export * from './widget.factory';
export * from './provide';

// other
export * from './validator.factory';
Expand Down
45 changes: 45 additions & 0 deletions packages/form/src/provide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
ENVIRONMENT_INITIALIZER,
EnvironmentProviders,
NgZone,
Provider,
inject,
makeEnvironmentProviders
} from '@angular/core';

import { AlainConfigService } from '@delon/util/config';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';

import { AjvSchemaValidatorFactory, SchemaValidatorFactory } from './validator.factory';
import { WidgetRegistry } from './widget.factory';
import { NzWidgetRegistry } from './widgets/nz-widget.registry';

export interface SFWidgetProvideConfig {
KEY: string;
type: NzSafeAny;
}

/**
* Just only using Standalone widgets
*/
export function provideSFConfig(options?: { widgets?: SFWidgetProvideConfig[] }): EnvironmentProviders {
const provides: Array<Provider | EnvironmentProviders> = [
{
provide: SchemaValidatorFactory,
useClass: AjvSchemaValidatorFactory,
deps: [AlainConfigService, NgZone]
},
{ provide: WidgetRegistry, useClass: NzWidgetRegistry }
];
if (options?.widgets) {
provides.push({
provide: ENVIRONMENT_INITIALIZER,
multi: true,
useValue: () => {
const srv = inject(WidgetRegistry);
options?.widgets?.forEach(widget => srv.register(widget.KEY, widget.type));
}
});
}
return makeEnvironmentProviders(provides);
}
18 changes: 3 additions & 15 deletions packages/form/widgets/autocomplete/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { DelonFormModule, WidgetRegistry } from '@delon/form';
import { NzAutocompleteModule } from 'ng-zorro-antd/auto-complete';
import { NzInputModule } from 'ng-zorro-antd/input';
import type { SFWidgetProvideConfig } from '@delon/form';

import { AutoCompleteWidget } from './widget';

export * from './widget';
export * from './schema';

@NgModule({
imports: [FormsModule, DelonFormModule, CommonModule, NzInputModule, NzAutocompleteModule],
declarations: [AutoCompleteWidget]
})
export class AutoCompleteWidgetModule {
constructor(widgetRegistry: WidgetRegistry) {
widgetRegistry.register(AutoCompleteWidget.KEY, AutoCompleteWidget);
}
export function withAutoComplete(): SFWidgetProvideConfig {
return { KEY: AutoCompleteWidget.KEY, type: AutoCompleteWidget };
}
4 changes: 2 additions & 2 deletions packages/form/widgets/autocomplete/widget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createTestContext } from '@delon/testing';
import { AlainConfigService } from '@delon/util/config';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

import { AutoCompleteWidgetModule } from './index';
import { withAutoComplete } from './index';
import { SFAutoCompleteWidgetSchema } from './schema';
import { AutoCompleteWidget } from './widget';
import { configureSFTestSuite, SFPage, TestFormComponent } from '../../spec/base.spec';
Expand All @@ -19,7 +19,7 @@ describe('form: widget: autocomplete', () => {
let page: SFPage;
const widget = 'autocomplete';

configureSFTestSuite({ imports: [AutoCompleteWidgetModule] });
configureSFTestSuite({ widgets: [withAutoComplete()] });

beforeEach(() => {
({ fixture, dl, context } = createTestContext(TestFormComponent));
Expand Down
16 changes: 11 additions & 5 deletions packages/form/widgets/autocomplete/widget.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { AsyncPipe } from '@angular/common';
import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
import { NgModel } from '@angular/forms';
import { FormsModule, NgModel } from '@angular/forms';
import { Observable, of, debounceTime, map, mergeMap, startWith, takeUntil } from 'rxjs';

import { ControlUIWidget, SFSchemaEnum, SFValue, getCopyEnum, getEnum, toBool } from '@delon/form';
import { NzAutocompleteOptionComponent } from 'ng-zorro-antd/auto-complete';
import { ControlUIWidget, DelonFormModule, SFSchemaEnum, SFValue, getCopyEnum, getEnum, toBool } from '@delon/form';
import { NzAutocompleteModule, NzAutocompleteOptionComponent } from 'ng-zorro-antd/auto-complete';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzInputModule } from 'ng-zorro-antd/input';

import type { SFAutoCompleteWidgetSchema } from './schema';

Expand Down Expand Up @@ -41,11 +43,15 @@ import type { SFAutoCompleteWidgetSchema } from './schema';
[compareWith]="i.compareWith"
(selectionChange)="updateValue($event)"
>
<nz-auto-option *ngFor="let i of list | async" [nzValue]="i" [nzLabel]="i.label"> {{ i.label }} </nz-auto-option>
@for (i of list | async; track i) {
<nz-auto-option [nzValue]="i" [nzLabel]="i.label"> {{ i.label }} </nz-auto-option>
}
</nz-autocomplete>
</sf-item-wrap>`,
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
standalone: true,
imports: [AsyncPipe, FormsModule, DelonFormModule, NzInputModule, NzAutocompleteModule]
})
export class AutoCompleteWidget extends ControlUIWidget<SFAutoCompleteWidgetSchema> {
static readonly KEY = 'autocomplete';
Expand Down
16 changes: 3 additions & 13 deletions packages/form/widgets/cascader/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { DelonFormModule, WidgetRegistry } from '@delon/form';
import { NzCascaderModule } from 'ng-zorro-antd/cascader';
import type { SFWidgetProvideConfig } from '@delon/form';

import { CascaderWidget } from './widget';

export * from './widget';
export * from './schema';

@NgModule({
imports: [FormsModule, DelonFormModule, NzCascaderModule],
declarations: [CascaderWidget]
})
export class CascaderWidgetModule {
constructor(widgetRegistry: WidgetRegistry) {
widgetRegistry.register(CascaderWidget.KEY, CascaderWidget);
}
export function withCascader(): SFWidgetProvideConfig {
return { KEY: CascaderWidget.KEY, type: CascaderWidget };
}
4 changes: 2 additions & 2 deletions packages/form/widgets/cascader/widget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ComponentFixture, fakeAsync } from '@angular/core/testing';

import { createTestContext } from '@delon/testing';

import { CascaderWidgetModule } from './index';
import { withCascader } from './index';
import { CascaderWidget } from './widget';
import { configureSFTestSuite, SFPage, TestFormComponent } from '../../spec/base.spec';

Expand All @@ -14,7 +14,7 @@ describe('form: widget: cascader', () => {
let page: SFPage;
const widget = 'cascader';

configureSFTestSuite({ imports: [CascaderWidgetModule] });
configureSFTestSuite({ widgets: [withCascader()] });

beforeEach(() => {
({ fixture, dl, context } = createTestContext(TestFormComponent));
Expand Down
9 changes: 6 additions & 3 deletions packages/form/widgets/cascader/widget.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ControlUIWidget, SFSchemaEnum, SFValue, getData, toBool } from '@delon/form';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
import { ControlUIWidget, DelonFormModule, SFSchemaEnum, SFValue, getData, toBool } from '@delon/form';
import { NzCascaderModule, NzCascaderOption } from 'ng-zorro-antd/cascader';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';

import type { SFCascaderWidgetSchema } from './schema';
Expand Down Expand Up @@ -44,7 +45,9 @@ import type { SFCascaderWidgetSchema } from './schema';
/>
</sf-item-wrap>`,
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
standalone: true,
imports: [FormsModule, DelonFormModule, NzCascaderModule]
})
export class CascaderWidget extends ControlUIWidget<SFCascaderWidgetSchema> implements OnInit {
static readonly KEY = 'cascader';
Expand Down
6 changes: 6 additions & 0 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { provideTinymce } from 'ngx-tinymce';

import { provideCellWidgets } from '@delon/abc/cell';
import { provideSTWidgets } from '@delon/abc/st';
import { provideSFConfig } from '@delon/form';
import { withAutoComplete } from '@delon/form/widgets/autocomplete';
import { withCascader } from '@delon/form/widgets/cascader';
import { mockInterceptor, provideDelonMockConfig } from '@delon/mock';
import { ALAIN_I18N_TOKEN, provideAlain } from '@delon/theme';
import { AlainConfig } from '@delon/util/config';
Expand Down Expand Up @@ -112,6 +115,9 @@ export const appConfig: ApplicationConfig = {
}),
provideCellWidgets(...CELL_WIDGETS),
provideSTWidgets(...ST_WIDGETS),
provideSFConfig({
widgets: [withAutoComplete(), withCascader()]
}),
importProvidersFrom(
JsonSchemaModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })
Expand Down
4 changes: 0 additions & 4 deletions src/app/shared/json-schema/json-schema.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { NgModule } from '@angular/core';

import { DelonFormModule } from '@delon/form';
import { AutoCompleteWidgetModule } from '@delon/form/widgets/autocomplete';
import { CascaderWidgetModule } from '@delon/form/widgets/cascader';
import { ColorWidgetModule } from '@delon/form/widgets/color';
import { MentionWidgetModule } from '@delon/form/widgets/mention';
import { QrCodeWidgetModule } from '@delon/form/widgets/qr-code';
Expand All @@ -20,8 +18,6 @@ import { TinymceWidgetModule } from '@delon/form/widgets-third/tinymce';
@NgModule({
imports: [
DelonFormModule.forRoot(),
AutoCompleteWidgetModule,
CascaderWidgetModule,
TransferWidgetModule,
MentionWidgetModule,
RateWidgetModule,
Expand Down

0 comments on commit 6e85870

Please sign in to comment.