Skip to content

Commit 56506d5

Browse files
committed
Typescript fixes
1 parent f04460e commit 56506d5

File tree

5 files changed

+25
-36
lines changed

5 files changed

+25
-36
lines changed

packages/formula/src/lib/form/aria.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function setAriaRole(el: FormElement, elements: FormElement[]): void {
5252
default:
5353
return `input-${el.type}`;
5454
}
55-
})()
55+
})(),
5656
);
5757
}
5858
}
@@ -72,9 +72,9 @@ export function setAriaStates(el: FormElement): void {
7272
export function setAriaValue(element: FormElement, elGroup: FormElement[]): void {
7373
if (element.type === 'radio') {
7474
elGroup.forEach((el) => el.removeAttribute('aria-checked'));
75-
element.setAttribute('aria-checked', element.checked ? 'true' : 'false');
75+
element.setAttribute('aria-checked', (element as HTMLInputElement).checked ? 'true' : 'false');
7676
} else if (element.type === 'checkbox') {
77-
element.setAttribute('aria-checked', element.checked ? 'true' : 'false');
77+
element.setAttribute('aria-checked', (element as HTMLInputElement).checked ? 'true' : 'false');
7878
}
7979
}
8080

packages/formula/src/lib/form/errors.mts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ interface ValidationCheckOptions {
99
/**
1010
* Extracts validity errors from the element and merges with custom errors.
1111
*/
12-
function extractErrors(el: FormElement, custom?: Record<string, boolean>): Record<string, boolean> {
13-
const output: Record<string, boolean> = {};
12+
function extractErrors(el: FormElement, custom?: Record<string, string>): Record<string, string> {
13+
const output: Record<string, string> = {};
1414
for (const key in el.validity) {
1515
if (key !== 'valid' && el.validity[key as keyof ValidityState]) {
16-
output[key] = true;
16+
output[key] = el.validationMessage || key;
1717
}
1818
}
1919
return { ...output, ...custom };
@@ -22,23 +22,17 @@ function extractErrors(el: FormElement, custom?: Record<string, boolean>): Recor
2222
/**
2323
* Gets the result of any custom validations available on the fields.
2424
*/
25-
function getCustomValidations(
26-
value: unknown,
27-
values: Record<string, unknown>,
28-
validations: Record<string, ValidatorFn> = {}
29-
): [Record<string, string>, Record<string, boolean>] {
30-
const messages: Record<string, string> = {};
31-
const errors: Record<string, boolean> = {};
25+
function getCustomValidations(value: unknown, values: Record<string, unknown>, validations: Record<string, ValidatorFn> = {}): Record<string, string> {
26+
const errors: Record<string, string> = {};
3227

3328
Object.entries(validations).forEach(([key, validation]) => {
3429
const message = validation(value, values);
3530
if (message !== null) {
36-
messages[key] = message;
37-
errors[key] = true;
31+
errors[key] = message;
3832
}
3933
});
4034

41-
return [messages, errors];
35+
return errors;
4236
}
4337

4438
/**
@@ -48,7 +42,7 @@ export function createValidationChecker(
4842
inputGroup: string,
4943
elementGroup: FormElement[],
5044
values: Record<string, unknown>,
51-
options?: ValidationCheckOptions
45+
options?: ValidationCheckOptions,
5246
): (el: FormElement, elValue: unknown) => FieldValidity {
5347
return (el: FormElement, elValue: unknown): FieldValidity => {
5448
// Reset the validity
@@ -78,26 +72,20 @@ export function createValidationChecker(
7872
};
7973

8074
// Check for any custom validations
81-
const [messages, customErrors] = getCustomValidations(
82-
elValue,
83-
values,
84-
options?.validators?.[inputGroup]
85-
);
75+
const customErrors = getCustomValidations(elValue, values, options?.validators?.[inputGroup]);
8676

8777
const errors = extractErrors(el, customErrors);
8878
const errorKeys = Object.keys(errors);
8979

9080
if (el.checkValidity()) {
9181
if (errorKeys.length > 0) {
92-
el.setCustomValidity(messages[errorKeys[0]]);
82+
el.setCustomValidity(errors[errorKeys[0]]);
9383
}
9484
} else {
9585
if (customMessages[errorKeys[0]]) {
9686
el.setCustomValidity(customMessages[errorKeys[0]]);
9787
}
9888
}
99-
100-
// Recheck validity and show any messages
10189
const valid = el.checkValidity();
10290
if (!valid) {
10391
el.setAttribute('data-formula-invalid', 'true');

packages/formula/src/lib/form/event.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function valueUpdate(
6060
}
6161

6262
stores.errors.set({ ...stores.errors.get(), [name]: validity });
63-
stores.formValid.set(Object.values(stores.errors.get()).every((v) => v.valid));
63+
stores.formValid.set(Object.values(stores.errors.get()).every((v: FieldValidity) => v.valid));
6464

6565
if (options?.formValidators) {
6666
formValidation(options.formValidators, stores);
@@ -79,15 +79,15 @@ export function valueUpdate(
7979
* Creates an event handler for the passed element with its data handler
8080
*/
8181
function createHandlerForData(
82-
extractor: (el: FormElement) => FieldExtractResult,
82+
extractor: (el: FormElement, isInit: boolean, isReset: boolean) => FieldExtractResult,
8383
stores: FormulaStores,
8484
options: EventOptions | undefined,
8585
hiddenFields: Map<string, FormElement[]>,
8686
enrich?: (value: unknown) => Record<string, unknown>
8787
): (event: Event) => void {
8888
return (event: Event) => {
8989
const el = (event?.currentTarget ?? event?.target) as FormElement;
90-
const extracted = extractor(el);
90+
const extracted = extractor(el, false, false);
9191
if (typeof options?.preChanges === 'function') {
9292
options.preChanges(extracted);
9393
}

packages/formula/src/lib/form/extract.mts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function setElementValue(
4242
const valueArray = Array.isArray(value) ? value : [];
4343
elementGroup.forEach((el, i) => {
4444
if (el.type === 'checkbox') {
45-
el.checked = valueArray.includes(el.value);
45+
(el as HTMLInputElement).checked = valueArray.includes(el.value);
4646
} else {
4747
el.value = String(valueArray[i] ?? '');
4848
}
@@ -54,11 +54,11 @@ function setElementValue(
5454
el.selected = valueArray.includes(el.value);
5555
});
5656
} else if (element.type === 'checkbox') {
57-
element.checked = Boolean(value);
57+
(element as HTMLInputElement).checked = Boolean(value);
5858
} else if (element.type === 'radio') {
59-
elementGroup.forEach((el) => (el.checked = value === el.value));
59+
elementGroup.forEach((el) => ((el as HTMLInputElement).checked = value === el.value));
6060
} else if (element.type === 'file') {
61-
element.files = value instanceof FileList ? value : null;
61+
(element as HTMLInputElement).files = value instanceof FileList ? value : null;
6262
} else {
6363
element.value = String(value ?? '');
6464
}
@@ -89,14 +89,14 @@ function getElementValues(
8989
})();
9090
break;
9191
case 'checkbox':
92-
elValue = isMultiValue ? elementGroup.filter((e) => e.checked).map((e) => e.value) : element.checked;
92+
elValue = isMultiValue ? elementGroup.filter((e) => (e as HTMLInputElement).checked).map((e) => e.value) : (element as HTMLInputElement).checked;
9393
break;
9494
case 'radio':
95-
const foundElement = elementGroup.find((el) => el.checked);
95+
const foundElement = elementGroup.find((el) => (el as HTMLInputElement).checked);
9696
elValue = foundElement ? foundElement.value : null;
9797
break;
9898
case 'file':
99-
elValue = element.files;
99+
elValue = (element as HTMLInputElement).files;
100100
break;
101101
default:
102102
elValue = isMultiValue ? elementGroup.map((v) => v.value) : element.value || null;
@@ -120,7 +120,7 @@ export function createFieldExtract(
120120

121121
let isMultiValue = false;
122122
if (elementGroup[0].type !== 'radio') {
123-
isMultiValue = !elementGroup[0].multiple && elementGroup.length > 1;
123+
isMultiValue = !(elementGroup[0] as HTMLSelectElement).multiple && elementGroup.length > 1;
124124
}
125125

126126
/**

packages/formula/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"include": ["src/**/*", "index.mjs"],
44
"exclude": ["node_modules", "test-app", "tests", "src/**/*.spec.mjs"],
55
"compilerOptions": {
6+
"target": "esnext",
67
"baseUrl": ".",
78
"checkJs": false,
89
"allowJs": true,

0 commit comments

Comments
 (0)