Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding FindByPath method on dynamic-form.service #1086

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ describe("DynamicFormService test suite", () => {
return [
new DynamicInputModel({id: "testFormArrayGroupInput"}),
new DynamicFormArrayModel({
id: "testNestedFormArray", groupFactory: () => [
id: "testNestedFormArray",
initialCount: 3,
groupFactory: () => [
new DynamicInputModel({id: "testNestedFormArrayGroupInput"})
]
})
Expand Down Expand Up @@ -256,6 +258,30 @@ describe("DynamicFormService test suite", () => {
expect(service.findById("nestedTestInput", testModel) instanceof DynamicFormControlModel).toBe(true);
});

it("should find a dynamic form control model by path", () => {
expect(service.findByPath("testCheckbox", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testCheckboxGroup", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testDatepicker", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testFormArray", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testInput", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testRadioGroup", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testSelect", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testSlider", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testSwitch", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testTextArea", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testFileUpload", testModel) instanceof DynamicFormControlModel).toBe(true);
expect(service.findByPath("testEditor", testModel) instanceof DynamicEditorModel).toBe(true);
expect(service.findById("testTimePicker", testModel) instanceof DynamicTimePickerModel).toBe(true);
expect(service.findByPath("testRating", testModel) instanceof DynamicRatingModel ).toBe(true);
expect(service.findByPath("testColorPicker", testModel) instanceof DynamicColorPickerModel).toBe(true);
expect(service.findByPath("testFormGroup.nestedTestInput", testModel) instanceof DynamicInputModel).toBe(true);
expect(service.findByPath("testFormGroup.nestedTestTextArea",testModel) instanceof DynamicTextAreaModel).toBe(true);
expect(service.findByPath("testFormArray.0.testFormArrayGroupInput",testModel) instanceof DynamicInputModel).toBe(true);
expect(service.findByPath("testFormArray.1.testFormArrayGroupInput",testModel) instanceof DynamicInputModel).toBe(true);
expect(service.findByPath("testFormArray.0.testNestedFormArray.0.testNestedFormArrayGroupInput",testModel) instanceof DynamicInputModel).toBe(true);
expect(service.findByPath("testFormArray.1.testNestedFormArray.1.testNestedFormArrayGroupInput",testModel) instanceof DynamicInputModel).toBe(true);
});


it("should resolve array group path", () => {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,57 @@ export class DynamicFormService {
return result;
}

findByPath = (
fullPath: string,
formModel: DynamicFormControlModel[]
): DynamicFormControlModel | null => {
let result = null,
findByPathFn = (path: string, groupModel: any): void => {
let id: string = path.split(".")[0];
let pathArray: string[] = path.split(".");
pathArray.splice(0, 1);
path = pathArray.join(".");

for (let controlModel of groupModel) {
if (controlModel.id === id) {
if (path.length === 0) {
result = controlModel;
break;
} else if (
path.split(".").length == 1 &&
Number.isInteger(Number(path.split(".")[0])) &&
!controlModel["multiple"]
) {
result = (controlModel as DynamicFormArrayModel).groups[
path.split(".")[0]
];
break;
} else {
if (controlModel instanceof DynamicFormGroupModel) {
findByPathFn(path, controlModel.group);
} else if (controlModel instanceof DynamicFormArrayModel) {
id = path.split(".")[0];
pathArray = path.split(".");
pathArray.splice(0, 1);
path = pathArray.join(".");
if (
controlModel.groups &&
controlModel.groups[id] &&
controlModel.groups[id].group
) {
findByPathFn(path, controlModel.groups[id].group);
}
}
}
}
}
};

findByPathFn(fullPath, formModel);

return result;
};

findModelById<T extends DynamicFormControlModel>(id: string, formModel: DynamicFormModel): T | null {
return this.findById(id, formModel) as T;
}
Expand Down