Skip to content

Commit

Permalink
AAE-24992 Form rules for fields (#10245)
Browse files Browse the repository at this point in the history
* AAE-24992 Add an ability to call a process on a form event

* extract filter events

* tmp

* add unit
  • Loading branch information
BSekula authored Sep 30, 2024
1 parent 0139273 commit dfbf08e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/core/src/lib/form/services/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ValidateFormFieldEvent } from '../events/validate-form-field.event';
import { FormValidationService } from './form-validation-service.interface';
import { FormRulesEvent } from '../events/form-rules.event';
import { FormSpinnerEvent } from '../events';
import { FormFieldModel } from '../components/widgets';

@Injectable({
providedIn: 'root'
Expand All @@ -46,6 +47,8 @@ export class FormService implements FormValidationService {
formContentClicked = new Subject<ContentLinkModel>();
toggleFormSpinner = new Subject<FormSpinnerEvent>();

onFormVariableChanged = new Subject<{ field: FormFieldModel; data?: any }>();

validateForm = new Subject<ValidateFormEvent>();
validateFormField = new Subject<ValidateFormFieldEvent>();
validateDynamicTableRow = new Subject<FormFieldEvent>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,5 +1056,24 @@ describe('DropdownCloudWidgetComponent', () => {
const failedErrorMsgElement = fixture.debugElement.query(By.css('.adf-dropdown-failed-message'));
expect(failedErrorMsgElement).toBeNull();
});

it('should update options when form variable changes', async () => {
const field = getVariableDropdownWidget('json-form-variable', 'countries', 'id', 'name', undefined, mockFormVariableWithJson);

widget.field = field;
fixture.detectChanges();

field.form.variables[0]['value']['countries'] = [{ id: 'NEW', name: 'New Country' }];

formService.onFormVariableChanged.next({ field });

const dropdown = await loader.getHarness(MatSelectHarness.with({ selector: '.adf-select' }));
await dropdown.open();

expect(widget.field.options.length).toEqual(1);
const allOptions = await dropdown.getOptions();
expect(await allOptions[0].getText()).toEqual('New Country');
expect(allOptions.length).toEqual(1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
}

ngOnInit() {
this.setPreviewState();

this.checkFieldOptionsSource();
this.updateOptions();
this.setupDropdown();

this.initFormControl();
this.initFilter();
this.formService.onFormVariableChanged.subscribe(({ field }) => {
if (field.id === this.field.id) {
this.setupDropdown();
}
});
}

ngOnDestroy() {
Expand Down Expand Up @@ -164,6 +164,16 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
this.onFieldChanged(field);
}

private setupDropdown(): void {
this.setPreviewState();

this.checkFieldOptionsSource();
this.updateOptions();

this.initFormControl();
this.initFilter();
}

private initFormControl(): void {
if (this.field?.required) {
this.dropdownControl.addValidators([Validators.required]);
Expand Down

0 comments on commit dfbf08e

Please sign in to comment.