Skip to content

Commit

Permalink
fix(form:select): correct trigger onSearch in reset when set value
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Oct 19, 2023
1 parent 5edaa97 commit ae2195d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 44 additions & 1 deletion packages/form/src/widgets/select/select.widget.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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();
}));
});
});
4 changes: 3 additions & 1 deletion packages/form/src/widgets/select/select.widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ export class SelectWidget extends ControlUIWidget<SFSelectWidgetSchema> 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 {
Expand Down

0 comments on commit ae2195d

Please sign in to comment.