Skip to content

Commit

Permalink
fix: Make submit button enabled when required fields are filled by de…
Browse files Browse the repository at this point in the history
…fault (#386)

Co-authored-by: Salihu <[email protected]>
  • Loading branch information
SalihuDickson and SalihuDickson authored Nov 25, 2024
1 parent 1395942 commit 05261a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-shoes-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@elixir-cloud/design": patch
---

remove disabled attr from submit button when required field has default data
23 changes: 19 additions & 4 deletions packages/ecc-utils-design/src/components/form/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,9 @@ export default class EccUtilsDesignForm extends LitElement {
const { value } = e.target as HTMLInputElement;
if (!value) {
_.unset(this.form, path);
if (field.fieldOptions?.returnIfEmpty) _.set(this.form, path, null);
} else {
_.set(this.form, path, value);
_.set(this.form, path, value.trim());
}
this.requestUpdate();
this.alertFieldChange(field.key, value);
Expand Down Expand Up @@ -548,9 +549,24 @@ export default class EccUtilsDesignForm extends LitElement {
return this.renderArrayTemplate(field, newPath);
}

if (field.fieldOptions?.required && !_.get(this.form, newPath)) {
this.requiredButEmpty.push(field.key);
if (field.fieldOptions?.required) {
if (
!_.get(this.form, newPath) &&
!this.requiredButEmpty.includes(field.key)
) {
// add to requiredButEmpty

// eslint-disable-next-line no-empty
if (!this.hasUpdated && field.fieldOptions.default) {
} else this.requiredButEmpty.push(field.key);
} else if (_.get(this.form, newPath)) {
// remove from requiredButEmpty
this.requiredButEmpty = this.requiredButEmpty.filter(
(key) => key !== field.key
);
}
}

if (field.type === "switch") {
return this.renderSwitchTemplate(field, newPath);
}
Expand Down Expand Up @@ -648,7 +664,6 @@ export default class EccUtilsDesignForm extends LitElement {
}

render() {
this.requiredButEmpty = [];
if (!this.fields || this.fields.length === 0) {
throw new Error("Fields is required & should not be empty array");
}
Expand Down

0 comments on commit 05261a1

Please sign in to comment.