Skip to content

Commit 05261a1

Browse files
fix: Make submit button enabled when required fields are filled by default (#386)
Co-authored-by: Salihu <salihudickson@gmail.com>
1 parent 1395942 commit 05261a1

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

.changeset/new-shoes-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@elixir-cloud/design": patch
3+
---
4+
5+
remove disabled attr from submit button when required field has default data

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,9 @@ export default class EccUtilsDesignForm extends LitElement {
352352
const { value } = e.target as HTMLInputElement;
353353
if (!value) {
354354
_.unset(this.form, path);
355+
if (field.fieldOptions?.returnIfEmpty) _.set(this.form, path, null);
355356
} else {
356-
_.set(this.form, path, value);
357+
_.set(this.form, path, value.trim());
357358
}
358359
this.requestUpdate();
359360
this.alertFieldChange(field.key, value);
@@ -548,9 +549,24 @@ export default class EccUtilsDesignForm extends LitElement {
548549
return this.renderArrayTemplate(field, newPath);
549550
}
550551

551-
if (field.fieldOptions?.required && !_.get(this.form, newPath)) {
552-
this.requiredButEmpty.push(field.key);
552+
if (field.fieldOptions?.required) {
553+
if (
554+
!_.get(this.form, newPath) &&
555+
!this.requiredButEmpty.includes(field.key)
556+
) {
557+
// add to requiredButEmpty
558+
559+
// eslint-disable-next-line no-empty
560+
if (!this.hasUpdated && field.fieldOptions.default) {
561+
} else this.requiredButEmpty.push(field.key);
562+
} else if (_.get(this.form, newPath)) {
563+
// remove from requiredButEmpty
564+
this.requiredButEmpty = this.requiredButEmpty.filter(
565+
(key) => key !== field.key
566+
);
567+
}
553568
}
569+
554570
if (field.type === "switch") {
555571
return this.renderSwitchTemplate(field, newPath);
556572
}
@@ -648,7 +664,6 @@ export default class EccUtilsDesignForm extends LitElement {
648664
}
649665

650666
render() {
651-
this.requiredButEmpty = [];
652667
if (!this.fields || this.fields.length === 0) {
653668
throw new Error("Fields is required & should not be empty array");
654669
}

0 commit comments

Comments
 (0)