Skip to content

Commit 23053f3

Browse files
committed
refactor: abstract findNearestFormGroup function
1 parent b7a43a1 commit 23053f3

File tree

3 files changed

+50
-56
lines changed

3 files changed

+50
-56
lines changed

packages/ecc-utils-design/src/components/form/formGroup.ts

+8-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { property, state } from "lit/decorators.js";
33
import { repeat } from "lit/directives/repeat.js";
44
import { unsafeHTML } from "lit/directives/unsafe-html.js";
55
import * as _ from "lodash-es";
6-
import { noKeyWarning, renderInTooltip, generateUniqueKey } from "./utils.js";
6+
import {
7+
noKeyWarning,
8+
renderInTooltip,
9+
generateUniqueKey,
10+
findNearestFormGroup,
11+
} from "./utils.js";
712
import "@shoelace-style/shoelace/dist/components/details/details.js";
813
import "@shoelace-style/shoelace/dist/components/button/button.js";
914
import formStyles from "./form.styles.js";
@@ -35,7 +40,7 @@ export default class EccUtilsDesignFormGroup extends LitElement {
3540
}> = [];
3641

3742
@state() private content = "";
38-
@state() private path = "";
43+
@state() private path: string | null = "";
3944

4045
declare setHTMLUnsafe: (htmlString: string) => void;
4146
protected firstUpdated(): void {
@@ -58,30 +63,7 @@ export default class EccUtilsDesignFormGroup extends LitElement {
5863
this.key = _.camelCase(this.label);
5964
}
6065

61-
this.findNearestFormGroup();
62-
}
63-
64-
private findNearestFormGroup(element: HTMLElement | null = this): void {
65-
if (!element) return;
66-
67-
if (element.matches("ecc-d-form")) {
68-
return;
69-
}
70-
71-
const { parentElement } = element;
72-
if (!parentElement) return;
73-
74-
const specialAttributes = ["ecc-array", "ecc-group", "ecc-form"];
75-
const hasSpecialAttribute = specialAttributes.some((attr) =>
76-
parentElement.hasAttribute(attr)
77-
);
78-
79-
if (hasSpecialAttribute) {
80-
const parentPath = parentElement.getAttribute("path");
81-
this.path = parentPath ? `${parentPath}.${this.key}` : this.key;
82-
}
83-
84-
this.findNearestFormGroup(parentElement);
66+
this.path = findNearestFormGroup(this.key, this, true);
8567
}
8668

8769
private renderGroupTemplate(): TemplateResult {

packages/ecc-utils-design/src/components/form/formInput.ts

+8-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as _ from "lodash-es";
22
import { html, LitElement, TemplateResult } from "lit";
33
import { property, state, query } from "lit/decorators.js";
44
import { repeat } from "lit/directives/repeat.js";
5-
import { renderInTooltip, noKeyWarning } from "./utils.js";
5+
import {
6+
renderInTooltip,
7+
noKeyWarning,
8+
findNearestFormGroup,
9+
} from "./utils.js";
610
import "@shoelace-style/shoelace/dist/components/alert/alert.js";
711
import "@shoelace-style/shoelace/dist/components/icon/icon.js";
812
import "@shoelace-style/shoelace/dist/components/input/input.js";
@@ -54,7 +58,7 @@ export default class EccUtilsDesignFormInput extends LitElement {
5458
@state() private alertText = "Something went wrong";
5559
@state() private alertType: AlertType = "info";
5660
@state() private showAlert = false;
57-
@state() path = "";
61+
@state() path: string | null = "";
5862

5963
@query("sl-input") input!: HTMLInputElement;
6064

@@ -63,38 +67,14 @@ export default class EccUtilsDesignFormInput extends LitElement {
6367
if (!this.key) {
6468
noKeyWarning("ecc-d-form-group", this.label);
6569
this.key = _.camelCase(this.label);
70+
this.setAttribute("key", this.key);
6671
}
6772

68-
this.findNearestFormGroup();
73+
this.path = findNearestFormGroup(this.key, this);
6974

7075
if (this.value) {
7176
this.handleFireChangeEvent();
7277
}
73-
74-
// console.log("just connected");
75-
}
76-
77-
private findNearestFormGroup(element: HTMLElement | null = this): void {
78-
if (!element) return;
79-
80-
if (element.matches("ecc-d-form") || element.matches("ecc-d-form-group")) {
81-
return;
82-
}
83-
84-
const { parentElement } = element;
85-
if (!parentElement) return;
86-
87-
const specialAttributes = ["ecc-array", "ecc-group", "ecc-form"];
88-
const hasSpecialAttribute = specialAttributes.some((attr) =>
89-
parentElement.hasAttribute(attr)
90-
);
91-
92-
if (hasSpecialAttribute) {
93-
const parentPath = parentElement.getAttribute("path");
94-
this.path = parentPath ? `${parentPath}.${this.key}` : this.key;
95-
}
96-
97-
this.findNearestFormGroup(parentElement);
9878
}
9979

10080
private handleDismissAlert() {
@@ -140,8 +120,6 @@ export default class EccUtilsDesignFormInput extends LitElement {
140120
const target = e.target as HTMLInputElement;
141121
this.value = this.type === "switch" ? target.checked : target.value;
142122

143-
console.log("this.value", this.value, target);
144-
145123
this.handleFireChangeEvent();
146124
this.requestUpdate();
147125
}

packages/ecc-utils-design/src/components/form/utils.ts

+34
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,37 @@ export function noKeyWarning(Element: string, label: string): void {
3030
export function generateUniqueKey() {
3131
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
3232
}
33+
34+
export const findNearestFormGroup = (
35+
key: string,
36+
element: HTMLElement | null,
37+
isGroup = false
38+
): string | null => {
39+
if (!element) return null;
40+
41+
if (
42+
element.matches("ecc-d-form") ||
43+
(!isGroup && element.matches("ecc-d-form-group"))
44+
) {
45+
return null;
46+
}
47+
48+
const { parentElement } = element;
49+
if (!parentElement) return null;
50+
51+
const specialAttributes = ["ecc-array", "ecc-group", "ecc-form"];
52+
const hasSpecialAttribute = specialAttributes.some((attr) =>
53+
parentElement.hasAttribute(attr)
54+
);
55+
56+
if (hasSpecialAttribute) {
57+
const parentPath = parentElement.getAttribute("path");
58+
const path = parentPath ? `${parentPath}.${key}` : key;
59+
60+
console.log("the key", key);
61+
62+
return path;
63+
}
64+
65+
return findNearestFormGroup(key, parentElement, isGroup);
66+
};

0 commit comments

Comments
 (0)