Skip to content

Commit a376446

Browse files
authored
impr(validation): don't debounce if delay is zero (@fehmer, @Miodec) (monkeytypegame#6878)
after monkeytypegame#6866 set debounceDelay to 0 for email validation
1 parent 33e3acc commit a376446

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

frontend/src/ts/commandline/commandline-metadata.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,9 @@ export const commandlineConfigMetadata: CommandlineConfigMetadataObject = {
582582
fontSize: {
583583
input: {
584584
inputValueConvert: Number,
585+
validation: {
586+
isValid: async (number: number) => number < 100,
587+
},
585588
},
586589
},
587590
fontFamily: {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ export type Validation<T> = {
3636
/** Resets the value to the current config if empty */
3737
resetIfEmpty?: false;
3838
};
39+
40+
// oxlint-disable-next-line no-explicit-any
41+
export function debounceIfNeeded<T extends (...args: any[]) => any>(
42+
delay: number,
43+
callback: T
44+
): T | debounce<T> {
45+
if (delay === undefined || delay <= 0) {
46+
return callback;
47+
}
48+
return debounce(delay, callback);
49+
}
50+
3951
/**
4052
* Create input handler for validated input element.
4153
* the `callback` is called for each validation state change, including "checking".
@@ -51,7 +63,7 @@ export function createInputEventHandler<T>(
5163
): (e: Event) => Promise<void> {
5264
let callIsValid =
5365
validation.isValid !== undefined
54-
? debounce(
66+
? debounceIfNeeded(
5567
validation.debounceDelay ?? 100,
5668
async (
5769
originalInput: HTMLInputElement,
@@ -115,7 +127,7 @@ export function createInputEventHandler<T>(
115127
return;
116128
}
117129

118-
callIsValid(originalInput, currentValue, checkValue as T);
130+
await callIsValid(originalInput, currentValue, checkValue as T);
119131
//call original handler if defined
120132
originalInput.oninput?.(e);
121133
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const actionModals: Record<Action, SimpleModal> = {
2121
{
2222
placeholder: "tag name",
2323
type: "text",
24-
validation: { isValid: tagNameValidation },
24+
validation: { isValid: tagNameValidation, debounceDelay: 0 },
2525
},
2626
],
2727
onlineOnly: true,
@@ -63,7 +63,7 @@ const actionModals: Record<Action, SimpleModal> = {
6363
{
6464
placeholder: "tag name",
6565
type: "text",
66-
validation: { isValid: tagNameValidation },
66+
validation: { isValid: tagNameValidation, debounceDelay: 0 },
6767
},
6868
],
6969
onlineOnly: true,

frontend/src/ts/modals/simple-modals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ list.updateEmail = new SimpleModal({
247247
isValid: async (currentValue, thisPopup) =>
248248
currentValue === thisPopup.inputs?.[1]?.currentValue() ||
249249
"Emails don't match",
250+
debounceDelay: 0,
250251
},
251252
},
252253
],

0 commit comments

Comments
 (0)