diff --git a/packages/form/src/widgets/select/select.widget.spec.ts b/packages/form/src/widgets/select/select.widget.spec.ts index c0c165a3b..0eda40435 100644 --- a/packages/form/src/widgets/select/select.widget.spec.ts +++ b/packages/form/src/widgets/select/select.widget.spec.ts @@ -1,5 +1,5 @@ import { DebugElement } from '@angular/core'; -import { ComponentFixture, fakeAsync } from '@angular/core/testing'; +import { ComponentFixture, fakeAsync, tick } from '@angular/core/testing'; import { createTestContext } from '@delon/testing'; @@ -154,4 +154,47 @@ describe('form: widget: select', () => { .checkValue('/a', undefined) .asyncEnd(); })); + + describe('#onSearch', () => { + it('should be first load when have value', fakeAsync(() => { + const onSearch = jasmine.createSpy().and.returnValue(Promise.resolve()); + const s: SFSchema = { + properties: { + a: { + type: 'string', + enum: [{ label: '待支付', value: 'WAIT_BUYER_PAY' }], + default: 'WAIT_BUYER_PAY', + ui: { + widget, + onSearch: onSearch + } + } + } + }; + page.newSchema(s); + tick(1000); + expect(onSearch).toHaveBeenCalled(); + page.asyncEnd(); + })); + + it('should be first load when value is empty', fakeAsync(() => { + const onSearch = jasmine.createSpy().and.returnValue(Promise.resolve()); + const s: SFSchema = { + properties: { + a: { + type: 'string', + enum: [{ label: '待支付', value: 'WAIT_BUYER_PAY' }], + ui: { + widget, + onSearch: onSearch + } + } + } + }; + page.newSchema(s); + tick(1000); + expect(onSearch).not.toHaveBeenCalled(); + page.asyncEnd(); + })); + }); }); diff --git a/packages/form/src/widgets/select/select.widget.ts b/packages/form/src/widgets/select/select.widget.ts index 974890733..9cf2b3a83 100644 --- a/packages/form/src/widgets/select/select.widget.ts +++ b/packages/form/src/widgets/select/select.widget.ts @@ -82,12 +82,14 @@ export class SelectWidget extends ControlUIWidget implemen } reset(value: SFValue): void { + const onSearch = this.ui.onSearch!; getData(this.schema, this.ui, value).subscribe(list => { this._value = value; - if (this.ui.onSearch == null) this.data = list; + if (onSearch == null) this.data = list; this.checkGroup(list); this.detectChanges(); }); + if (value && onSearch != null) this.search$.next(value); } change(values: SFValue): void {