Skip to content

Commit de847fc

Browse files
authored
refactor(input validation): rework getting current validation status (@Miodec) (monkeytypegame#6988)
Rename to diffrentiate between the predicate `isValid` and the element `isValid` Return `ValidationResult` instead of just a boolean Update usage to the new format Move config specific `resetIfEmpty` to the `ConfigInputOptions` type
1 parent f025b12 commit de847fc

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

frontend/src/ts/elements/input-validation.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ export type Validation<T> = {
3232

3333
/** custom debounce delay for `isValid` call. defaults to 100 */
3434
debounceDelay?: number;
35-
36-
/** Resets the value to the current config if empty */
37-
resetIfEmpty?: false;
3835
};
3936

4037
// oxlint-disable-next-line no-explicit-any
@@ -144,7 +141,7 @@ export type ValidationOptions<T> = (T extends string
144141
};
145142

146143
export type ValidatedHtmlInputElement = HTMLInputElement & {
147-
isValid: () => boolean | undefined;
144+
getValidationResult: () => ValidationResult;
148145
setValue: (val: string | null) => void;
149146
triggerValidation: () => void;
150147
};
@@ -178,9 +175,11 @@ export function validateWithIndicator<T>(
178175
},
179176
});
180177

181-
let isValid: boolean | undefined = undefined;
178+
let currentStatus: ValidationResult = {
179+
status: "checking",
180+
};
182181
const callback = (result: ValidationResult): void => {
183-
isValid = result.status === "success" || result.status === "warning";
182+
currentStatus = result;
184183
if (result.status === "failed" || result.status === "warning") {
185184
indicator.show(result.status, result.errorMessage);
186185
} else {
@@ -198,11 +197,12 @@ export function validateWithIndicator<T>(
198197
inputElement.addEventListener("input", handler);
199198

200199
const result = inputElement as ValidatedHtmlInputElement;
201-
result.isValid = () => isValid;
200+
result.getValidationResult = () => {
201+
return currentStatus;
202+
};
202203
result.setValue = (val: string | null) => {
203204
inputElement.value = val ?? "";
204205
if (val === null) {
205-
isValid = undefined;
206206
indicator.hide();
207207
} else {
208208
inputElement.dispatchEvent(new Event("input"));
@@ -227,6 +227,8 @@ export type ConfigInputOptions<K extends ConfigKey, T = ConfigType[K]> = {
227227
schema: boolean;
228228
/** optional callback is called for each change of the validation result */
229229
validationCallback?: (result: ValidationResult) => void;
230+
/** Resets the value to the current config if empty */
231+
resetIfEmpty?: false;
230232
};
231233
};
232234

frontend/src/ts/modals/edit-preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ async function apply(): Promise<void> {
262262
return;
263263
}
264264

265-
if (presetNameEl?.isValid() === false) {
265+
if (presetNameEl?.getValidationResult().status === "failed") {
266266
Notifications.add("Preset name is not valid", 0);
267267
return;
268268
}

frontend/src/ts/modals/save-custom-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function save(): boolean {
8989
async function setup(modalEl: HTMLElement): Promise<void> {
9090
modalEl.addEventListener("submit", (e) => {
9191
e.preventDefault();
92-
if (validatedInput.isValid() === true && save()) {
92+
if (validatedInput.getValidationResult().status === "success" && save()) {
9393
void modal.hide();
9494
}
9595
});

frontend/src/ts/pages/login.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ validateWithIndicator(emailVerifyInputEl, {
170170
debounceDelay: 0,
171171
callback: (result) => {
172172
registerForm.email =
173-
emailInputEl.isValid() && result.status === "success"
173+
emailInputEl.getValidationResult().status === "success" &&
174+
result.status === "success"
174175
? emailInputEl.value
175176
: undefined;
176177
updateSignupButton();
@@ -204,7 +205,8 @@ validateWithIndicator(passwordVerifyInputEl, {
204205
debounceDelay: 0,
205206
callback: (result) => {
206207
registerForm.password =
207-
passwordInputEl.isValid() && result.status === "success"
208+
passwordInputEl.getValidationResult().status === "success" &&
209+
result.status === "success"
208210
? passwordInputEl.value
209211
: undefined;
210212
updateSignupButton();

0 commit comments

Comments
 (0)