Skip to content

Commit da7b208

Browse files
committed
setup form submit
1 parent 8033aa1 commit da7b208

File tree

6 files changed

+56
-118
lines changed

6 files changed

+56
-118
lines changed

packages/ecc-utils-design/demo/form/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
@ecc-utils-submit=${async (e) => {
2424
try {
2525
console.log("form - submitted", e.detail);
26-
document.querySelector("ecc-utils-design-form").loading();
26+
document.querySelector("ecc-d-form").loading();
2727
await fetch("https://jsonplaceholder.typicode.com/posts");
2828
document
29-
.querySelector("ecc-utils-design-form")
29+
.querySelector("ecc-d-form")
3030
.success({ message: "Data fetched successfully" });
3131
} catch (error) {
3232
document
33-
.querySelector("ecc-utils-design-form")
33+
.querySelector("ecc-d-form")
3434
.error({ message: "Error fetching data" });
3535
}
3636
}}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const styles = css`
6868
.group-content {
6969
padding-top: var(--ecc-spacing-medium);
7070
}
71-
.group-item {
71+
.group {
7272
min-height: var(--ecc-input-height-3xlarge);
7373
}
7474
/* Array Styles */
@@ -78,12 +78,12 @@ const styles = css`
7878
font-weight: var(--ecc-input-font-weight);
7979
letter-spacing: var(--ecc-input-letter-spacing);
8080
}
81-
.array-item {
81+
.array {
8282
border-style: solid;
8383
border-width: 0px 0px var(--ecc-input-border-width) 0px;
8484
border-color: var(--ecc-input-border-color-disabled);
8585
}
86-
.array-item {
86+
.array {
8787
display: flex;
8888
flex-direction: row;
8989
align-items: center;

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

+22-39
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default class EccUtilsDesignForm extends LitElement {
8686

8787
@state() private form: object = {};
8888
@state() private formState: "idle" | "loading" | "error" | "success" = "idle";
89-
@state() private canSubmit = false;
89+
@state() private canSubmit = true;
9090
@state() private submitDisabledByUser = false;
9191
@state() private errorMessage = "Something went wrong";
9292
@state() private successMessage = "Form submitted successfully";
@@ -178,6 +178,7 @@ export default class EccUtilsDesignForm extends LitElement {
178178

179179
private handleSubmit(e: Event) {
180180
e.preventDefault();
181+
181182
const form = this.shadowRoot?.querySelector("form");
182183
const isValid = form?.reportValidity();
183184
if (!isValid) {
@@ -198,8 +199,26 @@ export default class EccUtilsDesignForm extends LitElement {
198199
}
199200

200201
render() {
202+
// const toggleButtonState = () => {
203+
// if (this.requiredButEmpty.length > 0) {
204+
// this.canSubmit = false;
205+
// } else {
206+
// this.canSubmit = true;
207+
// }
208+
209+
// return "";
210+
// };
211+
212+
if (this.formState === "success") {
213+
return html` ${this.renderSuccessTemplate()} `;
214+
}
215+
216+
if (this.formState === "error") {
217+
return html` ${this.renderErrorTemplate()} `;
218+
}
219+
201220
return html`
202-
<div ecc-form>
221+
<form ecc-form @submit=${this.handleSubmit}>
203222
${repeat(
204223
this.items,
205224
() => _.uniqueId("ecc-form-item-"),
@@ -218,43 +237,7 @@ export default class EccUtilsDesignForm extends LitElement {
218237
>
219238
Submit
220239
</sl-button>
221-
</div>
240+
</form>
222241
`;
223-
// if (!this.fields || this.fields.length === 0) {
224-
// throw new Error("Fields is required & should not be empty array");
225-
// }
226-
// if (this.formState === "success") {
227-
// return html` ${this.renderSuccessTemplate()} `;
228-
// }
229-
230-
// const toggleButtonState = () => {
231-
// if (this.requiredButEmpty.length > 0) {
232-
// this.canSubmit = false;
233-
// } else {
234-
// this.canSubmit = true;
235-
// }
236-
237-
// return "";
238-
// };
239-
240-
// return html`
241-
// <form data-testid="form" @submit=${this.handleSubmit}>
242-
// ${this.fields.map((field) => this.renderTemplate(field, "data"))}
243-
// ${this.renderErrorTemplate()} ${toggleButtonState()}
244-
245-
// <sl-button
246-
// type="submit"
247-
// data-testid="form-submit"
248-
// variant="primary"
249-
// class="submit-button"
250-
// ?loading=${this.formState === "loading"}
251-
// ?disabled=${this.submitDisabledByUser ||
252-
// !this.canSubmit ||
253-
// this.formState === "loading"}
254-
// >
255-
// Submit
256-
// </sl-button>
257-
// </form>
258-
// `;
259242
}
260243
}

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

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@ import { LitElement, html, TemplateResult } from "lit";
22
import { property, state } from "lit/decorators.js";
33
import { repeat } from "lit/directives/repeat.js";
44
import * as _ from "lodash-es";
5-
import { noKeyWarning, renderInTooltip, toCamelCase } from "./utils.js";
5+
import { noKeyWarning, renderInTooltip } from "./utils.js";
66
import "@shoelace-style/shoelace/dist/components/details/details.js";
77
import "@shoelace-style/shoelace/dist/components/button/button.js";
88
import formStyles from "./form.styles.js";
99

1010
export default class EccUtilsDesignFormGroup extends LitElement {
11-
static styles = [
12-
// primitiveStylesheet,
13-
// sholelaceStyles,
14-
// hostStyles,
15-
formStyles,
16-
];
11+
static styles = [formStyles];
1712

1813
// TODO
1914
// build required but empty functionality
@@ -23,14 +18,14 @@ export default class EccUtilsDesignFormGroup extends LitElement {
2318
@property({ type: Boolean, reflect: true }) required = "";
2419
@property({ type: String, reflect: true }) tooltip = "";
2520

26-
// array item options
21+
// array options
2722
@property({ type: Number, reflect: true })
2823
instances = 0;
2924

3025
@property({ type: Number, attribute: "max" }) maxInstances = "";
3126
@property({ type: Number, attribute: "min" }) minInstances = "";
3227

33-
// group item options
28+
// group options
3429
@property({ type: Boolean, reflect: true }) collapsible = false;
3530

3631
@state() private arrayInstances: Array<{
@@ -64,7 +59,7 @@ export default class EccUtilsDesignFormGroup extends LitElement {
6459
super.connectedCallback();
6560
if (!this.key) {
6661
noKeyWarning("ecc-d-form-group", this.label);
67-
this.key = toCamelCase(this.label);
62+
this.key = _.camelCase(this.label);
6863
}
6964

7065
this.findNearestFormGroup();

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

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import * as _ from "lodash-es";
12
import { html, LitElement, TemplateResult } from "lit";
2-
import { property, state } from "lit/decorators.js";
3+
import { property, state, query } from "lit/decorators.js";
34
import { repeat } from "lit/directives/repeat.js";
4-
import { renderInTooltip, toCamelCase, noKeyWarning } from "./utils.js";
5+
import { renderInTooltip, noKeyWarning } from "./utils.js";
56
import "@shoelace-style/shoelace/dist/components/alert/alert.js";
67
import "@shoelace-style/shoelace/dist/components/icon/icon.js";
78
import "@shoelace-style/shoelace/dist/components/input/input.js";
@@ -44,23 +45,24 @@ export default class EccUtilsDesignFormInput extends LitElement {
4445
@property({ type: String, reflect: true }) default = "";
4546
@property({ type: Boolean, reflect: true }) checked = false;
4647
@property({ type: Boolean, reflect: true }) multiple = false;
48+
@property({ type: String, reflect: true }) value: any;
4749
@property({ type: String, reflect: true }) accept = "*";
48-
@property({ type: String, attribute: "tus-endpoint" }) tusEndpoint = "";
50+
@property({ type: String, attribute: "endpoint" }) tusEndpoint = "";
4951
@property({ type: String, reflect: true }) protocol: "native" | "tus" =
5052
"native";
5153

52-
@property({ type: String, reflect: true }) value: any;
53-
5454
@state() private alertText = "Something went wrong";
5555
@state() private alertType: AlertType = "info";
5656
@state() private showAlert = false;
5757
@state() path = "";
5858

59+
@query("sl-input") input!: HTMLInputElement;
60+
5961
connectedCallback(): void {
6062
super.connectedCallback();
6163
if (!this.key) {
6264
noKeyWarning("ecc-d-form-group", this.label);
63-
this.key = toCamelCase(this.label);
65+
this.key = _.camelCase(this.label);
6466
}
6567

6668
this.findNearestFormGroup();
@@ -88,10 +90,12 @@ export default class EccUtilsDesignFormInput extends LitElement {
8890
const parentPath = parentElement.getAttribute("path");
8991
this.path = parentPath ? `${parentPath}.${this.key}` : this.key;
9092
}
93+
94+
this.findNearestFormGroup(parentElement);
9195
}
9296

9397
private handleDismissAlert() {
94-
this.alertText = ""; // reset error text
98+
this.alertText = "";
9599
this.showAlert = false;
96100
this.requestUpdate();
97101
}
@@ -103,6 +107,18 @@ export default class EccUtilsDesignFormInput extends LitElement {
103107
this.requestUpdate();
104108
}
105109

110+
validity() {
111+
return this.input.validity;
112+
}
113+
114+
checkValidity() {
115+
return this.input.checkValidity();
116+
}
117+
118+
reportValidity() {
119+
return this.input.reportValidity();
120+
}
121+
106122
private handleFireChangeEvent() {
107123
this.dispatchEvent(
108124
new CustomEvent("ecc-utils-change", {
@@ -121,10 +137,7 @@ export default class EccUtilsDesignFormInput extends LitElement {
121137
const target = e.target as HTMLInputElement;
122138
this.value = this.type === "switch" ? target.checked : target.value;
123139

124-
// fire change event
125140
this.handleFireChangeEvent();
126-
127-
// update
128141
this.requestUpdate();
129142
}
130143

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

-53
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ export function renderInTooltip(
1313
: content;
1414
}
1515

16-
// write a function that takes any string and converts it to a camelCase variable name
17-
export function toCamelCase(str: string): string {
18-
return str
19-
.replace(/[^a-zA-Z0-9\s]/g, "") // Remove special characters
20-
.split(/[\s_-]+/) // Split on spaces, underscores, and hyphens
21-
.map((word, index) => {
22-
if (index === 0) {
23-
return word.toLowerCase();
24-
}
25-
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
26-
})
27-
.join("");
28-
}
29-
3016
// write a function to console.warn a string that is sent as a param, only when app is in dev mode
3117
export function devWarn(message: string): void {
3218
// eslint-disable-next-line turbo/no-undeclared-env-vars
@@ -44,42 +30,3 @@ export function noKeyWarning(Element: string, label: string): void {
4430
export function isShadowElement(element: Element): boolean {
4531
return element.getRootNode() instanceof ShadowRoot;
4632
}
47-
48-
/**
49-
* Finds the nearest form group and returns its path.
50-
* @param element The starting element to search from.
51-
* @param key The key of the current element (if applicable).
52-
* @returns The path of the nearest form group, or null if not found.
53-
*/
54-
export function findNearestFormGroup(
55-
element: HTMLElement,
56-
key?: string,
57-
groupItem = false
58-
): string | null {
59-
if (!element) return null;
60-
61-
if (element.matches("ecc-d-form") || element.matches("ecc-d-form-group")) {
62-
return null;
63-
}
64-
65-
const { parentElement } = element;
66-
if (!parentElement) return null;
67-
68-
const specialAttributes = [
69-
"ecc-array-item",
70-
"ecc-form-item",
71-
"ecc-group-item",
72-
];
73-
const hasSpecialAttribute = specialAttributes.some((attr) =>
74-
parentElement.hasAttribute(attr)
75-
);
76-
77-
if (hasSpecialAttribute) {
78-
const parentPath = parentElement.getAttribute("path");
79-
return parentPath && key
80-
? `${parentPath}.${key}`
81-
: key || parentPath || null;
82-
}
83-
84-
return findNearestFormGroup(parentElement, key, groupItem);
85-
}

0 commit comments

Comments
 (0)