Skip to content

Commit fa03afe

Browse files
authored
impr(modals): missing/invalid inputs disable submit button (@fehmer) (monkeytypegame#6973)
1 parent 5fb5890 commit fa03afe

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

frontend/src/ts/utils/simple-modal.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,14 @@ export class SimpleModal {
171171
el.find(".submitButton").remove();
172172
} else {
173173
el.find(".submitButton").text(this.buttonText);
174+
this.updateSubmitButtonState();
174175
}
175176

176177
if ((this.text ?? "") === "") {
177178
el.find(".text").addClass("hidden");
178179
} else {
179180
el.find(".text").removeClass("hidden");
180181
}
181-
182-
// }
183182
}
184183

185184
initInputs(): void {
@@ -315,9 +314,12 @@ export class SimpleModal {
315314
"#" + attributes["id"]
316315
) as HTMLInputElement;
317316

318-
if (input.oninput !== undefined) {
319-
element.oninput = input.oninput;
320-
}
317+
const originalOnInput = element.oninput;
318+
element.oninput = (e) => {
319+
if (originalOnInput) originalOnInput.call(element, e);
320+
input.oninput?.(e);
321+
this.updateSubmitButtonState();
322+
};
321323

322324
input.currentValue = () => {
323325
if (element.type === "checkbox")
@@ -338,6 +340,8 @@ export class SimpleModal {
338340

339341
callback: (result: ValidationResult) => {
340342
input.hasError = result.status !== "success";
343+
344+
this.updateSubmitButtonState();
341345
},
342346
debounceDelay: input.validation.debounceDelay,
343347
};
@@ -351,16 +355,12 @@ export class SimpleModal {
351355

352356
exec(): void {
353357
if (!this.canClose) return;
354-
if (
355-
this.inputs
356-
.filter((i) => i.hidden !== true && i.optional !== true)
357-
.some((v) => v.currentValue() === undefined || v.currentValue() === "")
358-
) {
358+
if (this.hasMissingRequired()) {
359359
Notifications.add("Please fill in all fields", 0);
360360
return;
361361
}
362362

363-
if (this.inputs.some((i) => i.hasError === true)) {
363+
if (this.hasValidationErrors()) {
364364
Notifications.add("Please solve all validation errors", 0);
365365
return;
366366
}
@@ -429,6 +429,29 @@ export class SimpleModal {
429429
await modal.hide(hideOptions);
430430
}
431431
}
432+
433+
hasMissingRequired(): boolean {
434+
return this.inputs
435+
.filter((i) => i.hidden !== true && i.optional !== true)
436+
.some((v) => v.currentValue() === undefined || v.currentValue() === "");
437+
}
438+
439+
hasValidationErrors(): boolean {
440+
return this.inputs.some((i) => i.hasError === true);
441+
}
442+
443+
updateSubmitButtonState(): void {
444+
const button = this.element.querySelector(
445+
".submitButton"
446+
) as HTMLButtonElement;
447+
if (button === null) return;
448+
449+
if (this.hasMissingRequired() || this.hasValidationErrors()) {
450+
button.disabled = true;
451+
} else {
452+
button.disabled = false;
453+
}
454+
}
432455
}
433456

434457
function hide(): void {

0 commit comments

Comments
 (0)