@streamscloud/kit 0.90.1 → 0.91.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/validation/form-validation-handler/form-validation-handler.svelte.d.ts +20 -8
- package/dist/core/validation/form-validation-handler/form-validation-handler.svelte.js +80 -63
- package/dist/core/validation/form-validation-handler/index.d.ts +1 -1
- package/dist/core/validation/form-validation-handler/types.d.ts +4 -0
- package/dist/core/validation/form-validation-handler/validation-helper.d.ts +6 -0
- package/dist/core/validation/form-validation-handler/validation-helper.js +27 -0
- package/dist/ui/form-field/cmp.form-field-validatable.svelte +2 -9
- package/dist/ui/form-field/cmp.form-field-validatable.svelte.d.ts +3 -7
- package/dist/ui/select/cmp.multiselect.svelte.d.ts +1 -1
- package/dist/ui/validatable/cmp.validatable.svelte +9 -46
- package/dist/ui/validatable/cmp.validatable.svelte.d.ts +4 -29
- package/dist/ui/validatable/index.d.ts +1 -2
- package/dist/ui/validatable/types.d.ts +12 -0
- package/package.json +1 -1
|
@@ -3,16 +3,22 @@ import type { IFormHandlerValidator } from './types';
|
|
|
3
3
|
type TypeToValidate<T> = {
|
|
4
4
|
[K in keyof T]: T[K];
|
|
5
5
|
};
|
|
6
|
+
export type ValidateFieldOptions = {
|
|
7
|
+
/** Delay before the validator runs; a newer call for the same field restarts it. */
|
|
8
|
+
debounceMs?: number;
|
|
9
|
+
/** Skip the call when the field still holds the value its last validation ran on (or is scheduled to run on). */
|
|
10
|
+
onlyIfChanged?: boolean;
|
|
11
|
+
};
|
|
6
12
|
export declare class FormValidationHandler<T extends TypeToValidate<T> = TypeToValidate<unknown>, TSubmitResult = void> implements IValidator {
|
|
7
13
|
readonly formStringified: Record<string, string>;
|
|
8
14
|
readonly modified: Record<keyof T, boolean>;
|
|
9
|
-
readonly isValid: boolean;
|
|
10
|
-
readonly isModified: boolean;
|
|
11
15
|
private _form;
|
|
12
16
|
private _errors;
|
|
13
17
|
private _touched;
|
|
14
|
-
private
|
|
18
|
+
private _validatingFields;
|
|
19
|
+
private _isFormValidating;
|
|
15
20
|
private _isSubmitting;
|
|
21
|
+
private _checkedValues;
|
|
16
22
|
private _validator;
|
|
17
23
|
private _initialValues;
|
|
18
24
|
private readonly _onSubmit;
|
|
@@ -29,20 +35,25 @@ export declare class FormValidationHandler<T extends TypeToValidate<T> = TypeToV
|
|
|
29
35
|
[P in keyof Partial<T>]: string;
|
|
30
36
|
}>;
|
|
31
37
|
get touched(): Readonly<Record<keyof T, boolean>>;
|
|
38
|
+
get isValid(): boolean;
|
|
39
|
+
get isModified(): boolean;
|
|
32
40
|
get isValidating(): boolean;
|
|
33
41
|
get isSubmitting(): boolean;
|
|
42
|
+
/** True from the moment a field validation is requested (debounce included) until its result is stored. */
|
|
43
|
+
isFieldValidating: (field: keyof T) => boolean;
|
|
34
44
|
updateField: <K extends keyof T>(field: K, value: T[K]) => void;
|
|
35
45
|
updateValidateField: <K extends keyof T>(field: K, value: T[K]) => Promise<void>;
|
|
36
46
|
updateTouched: (field: keyof T, value: boolean) => void;
|
|
37
47
|
updateTouchedForAllFields: (value: boolean) => void;
|
|
38
48
|
/**
|
|
39
49
|
* Sets an error the schema can't produce (a server-side rejection), marking the field touched so it renders.
|
|
40
|
-
*
|
|
50
|
+
* It stays until the field's value changes, the field is validated explicitly, or the whole form is validated.
|
|
41
51
|
*/
|
|
42
52
|
setFieldError: (field: keyof T, message: string) => void;
|
|
43
|
-
/** Batch {@link setFieldError} — skips `undefined` entries
|
|
53
|
+
/** Batch {@link setFieldError} — skips `undefined` entries. */
|
|
44
54
|
setFieldErrors: (errors: Partial<Record<keyof T, string>>) => void;
|
|
45
|
-
|
|
55
|
+
/** Validates one field. A result that a newer validation of the same field has superseded is dropped. */
|
|
56
|
+
validateField: (field: keyof T, options?: ValidateFieldOptions) => Promise<void>;
|
|
46
57
|
updateInitialValues: (newValues: T) => void;
|
|
47
58
|
updateValidator: (validator: IFormHandlerValidator<T>) => void;
|
|
48
59
|
handleReset: () => void;
|
|
@@ -50,7 +61,8 @@ export declare class FormValidationHandler<T extends TypeToValidate<T> = TypeToV
|
|
|
50
61
|
validate: (values?: T) => Promise<boolean>;
|
|
51
62
|
handleSubmit: (event: Event | null) => Promise<void>;
|
|
52
63
|
dispose(): void;
|
|
53
|
-
private
|
|
54
|
-
private
|
|
64
|
+
private _fieldNames;
|
|
65
|
+
private _rememberCheckedValue;
|
|
66
|
+
private _runValidator;
|
|
55
67
|
}
|
|
56
68
|
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Utils } from '../../utils';
|
|
2
|
+
import { ValidationHelper } from './validation-helper';
|
|
2
3
|
import { dequal as isEqual } from 'dequal/lite';
|
|
3
4
|
const NO_ERROR = '';
|
|
4
5
|
export class FormValidationHandler {
|
|
@@ -6,35 +7,33 @@ export class FormValidationHandler {
|
|
|
6
7
|
if (!this.form) {
|
|
7
8
|
return {};
|
|
8
9
|
}
|
|
9
|
-
return Object.entries(this.form).reduce((acc, [key, value]) => ({ ...acc, [key]:
|
|
10
|
+
return Object.entries(this.form).reduce((acc, [key, value]) => ({ ...acc, [key]: ValidationHelper.stringify(value) }), {});
|
|
10
11
|
});
|
|
11
12
|
modified = $derived.by(() => {
|
|
12
13
|
if (!this.form) {
|
|
13
14
|
return null;
|
|
14
15
|
}
|
|
15
|
-
const object =
|
|
16
|
+
const object = ValidationHelper.fillKeys(this.form, false);
|
|
16
17
|
for (const key in this.form) {
|
|
17
18
|
object[key] = !isEqual(this.form[key], this._initialValues[key]);
|
|
18
19
|
}
|
|
19
20
|
return object;
|
|
20
21
|
});
|
|
21
|
-
isValid = $derived(ValidationUtils.getValues(this.errors).every((field) => field === NO_ERROR));
|
|
22
|
-
isModified = $derived.by(() => {
|
|
23
|
-
return ValidationUtils.getValues(this.modified).includes(true);
|
|
24
|
-
});
|
|
25
22
|
_form = $state(null);
|
|
26
23
|
_errors = $state(null);
|
|
27
24
|
_touched = $state(null);
|
|
28
|
-
|
|
25
|
+
_validatingFields = $state({});
|
|
26
|
+
_isFormValidating = $state(false);
|
|
29
27
|
_isSubmitting = $state(false);
|
|
28
|
+
_checkedValues = new Map();
|
|
30
29
|
_validator;
|
|
31
30
|
_initialValues = $state(null);
|
|
32
31
|
_onSubmit;
|
|
33
32
|
_onReset;
|
|
34
33
|
_initials = {
|
|
35
34
|
values: () => Utils.clone(this._initialValues),
|
|
36
|
-
errors: () =>
|
|
37
|
-
touched: () =>
|
|
35
|
+
errors: () => ValidationHelper.fillKeys(this._initialValues, NO_ERROR),
|
|
36
|
+
touched: () => ValidationHelper.fillKeys(this._initialValues, false)
|
|
38
37
|
};
|
|
39
38
|
constructor(context) {
|
|
40
39
|
this._initialValues = context.initialValues || {};
|
|
@@ -54,36 +53,47 @@ export class FormValidationHandler {
|
|
|
54
53
|
get touched() {
|
|
55
54
|
return this._touched;
|
|
56
55
|
}
|
|
56
|
+
get isValid() {
|
|
57
|
+
return Object.values(this._errors).every((error) => !error);
|
|
58
|
+
}
|
|
59
|
+
get isModified() {
|
|
60
|
+
return Object.values(this.modified).includes(true);
|
|
61
|
+
}
|
|
57
62
|
get isValidating() {
|
|
58
|
-
return this.
|
|
63
|
+
return this._isFormValidating || Object.values(this._validatingFields).some(Boolean);
|
|
59
64
|
}
|
|
60
65
|
get isSubmitting() {
|
|
61
66
|
return this._isSubmitting;
|
|
62
67
|
}
|
|
68
|
+
/** True from the moment a field validation is requested (debounce included) until its result is stored. */
|
|
69
|
+
isFieldValidating = (field) => {
|
|
70
|
+
return !!this._validatingFields[field];
|
|
71
|
+
};
|
|
63
72
|
updateField = (field, value) => {
|
|
64
|
-
|
|
73
|
+
this._form[field] = value;
|
|
65
74
|
};
|
|
66
75
|
updateValidateField = async (field, value) => {
|
|
67
76
|
this.updateField(field, value);
|
|
68
|
-
await this.
|
|
77
|
+
await this.validateField(field);
|
|
69
78
|
};
|
|
70
79
|
updateTouched = (field, value) => {
|
|
71
|
-
|
|
80
|
+
this._touched[field] = value;
|
|
72
81
|
};
|
|
73
82
|
updateTouchedForAllFields = (value) => {
|
|
74
|
-
|
|
75
|
-
ValidationUtils.update(this._touched, key, value);
|
|
76
|
-
});
|
|
83
|
+
this._fieldNames().forEach((field) => this.updateTouched(field, value));
|
|
77
84
|
};
|
|
78
85
|
/**
|
|
79
86
|
* Sets an error the schema can't produce (a server-side rejection), marking the field touched so it renders.
|
|
80
|
-
*
|
|
87
|
+
* It stays until the field's value changes, the field is validated explicitly, or the whole form is validated.
|
|
81
88
|
*/
|
|
82
89
|
setFieldError = (field, message) => {
|
|
83
|
-
|
|
90
|
+
if (!this._checkedValues.has(field)) {
|
|
91
|
+
this._rememberCheckedValue(field);
|
|
92
|
+
}
|
|
93
|
+
this._errors[field] = message;
|
|
84
94
|
this.updateTouched(field, true);
|
|
85
95
|
};
|
|
86
|
-
/** Batch {@link setFieldError} — skips `undefined` entries
|
|
96
|
+
/** Batch {@link setFieldError} — skips `undefined` entries. */
|
|
87
97
|
setFieldErrors = (errors) => {
|
|
88
98
|
Object.keys(errors).forEach((field) => {
|
|
89
99
|
const message = errors[field];
|
|
@@ -93,8 +103,19 @@ export class FormValidationHandler {
|
|
|
93
103
|
this.setFieldError(field, message);
|
|
94
104
|
});
|
|
95
105
|
};
|
|
96
|
-
|
|
97
|
-
|
|
106
|
+
/** Validates one field. A result that a newer validation of the same field has superseded is dropped. */
|
|
107
|
+
validateField = async (field, options = {}) => {
|
|
108
|
+
const { debounceMs = 0, onlyIfChanged = false } = options;
|
|
109
|
+
const previous = this._checkedValues.get(field);
|
|
110
|
+
if (onlyIfChanged && previous && isEqual(previous.value, $state.snapshot(this._form[field]))) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const checked = this._rememberCheckedValue(field);
|
|
114
|
+
this._validatingFields[field] = true;
|
|
115
|
+
if (debounceMs > 0 && !(await ValidationHelper.waitUnlessCancelled(checked, debounceMs))) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
await this._runValidator(field, checked);
|
|
98
119
|
};
|
|
99
120
|
updateInitialValues = (newValues) => {
|
|
100
121
|
this._initialValues = newValues;
|
|
@@ -105,6 +126,9 @@ export class FormValidationHandler {
|
|
|
105
126
|
this.handleReset();
|
|
106
127
|
};
|
|
107
128
|
handleReset = () => {
|
|
129
|
+
this._checkedValues.forEach((checked) => checked.cancel?.());
|
|
130
|
+
this._checkedValues.clear();
|
|
131
|
+
this._validatingFields = {};
|
|
108
132
|
this._form = this._initials.values();
|
|
109
133
|
this._errors = this._initials.errors();
|
|
110
134
|
this._touched = this._initials.touched();
|
|
@@ -132,11 +156,21 @@ export class FormValidationHandler {
|
|
|
132
156
|
validate = async (values) => {
|
|
133
157
|
this.updateTouchedForAllFields(true);
|
|
134
158
|
values = values || this.form;
|
|
135
|
-
this.
|
|
136
|
-
|
|
137
|
-
this.
|
|
138
|
-
|
|
139
|
-
|
|
159
|
+
const checkedOnSubmit = new Map(this._fieldNames().map((field) => [field, this._rememberCheckedValue(field)]));
|
|
160
|
+
this._validatingFields = {};
|
|
161
|
+
this._isFormValidating = true;
|
|
162
|
+
try {
|
|
163
|
+
const result = await this._validator.validate(values);
|
|
164
|
+
checkedOnSubmit.forEach((checked, field) => {
|
|
165
|
+
if (this._checkedValues.get(field) === checked) {
|
|
166
|
+
this._errors[field] = result.errors[field] ?? NO_ERROR;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
return result.isValid;
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
this._isFormValidating = false;
|
|
173
|
+
}
|
|
140
174
|
};
|
|
141
175
|
handleSubmit = async (event) => {
|
|
142
176
|
if (event && event.preventDefault) {
|
|
@@ -160,46 +194,29 @@ export class FormValidationHandler {
|
|
|
160
194
|
this._isSubmitting = false;
|
|
161
195
|
}
|
|
162
196
|
};
|
|
163
|
-
dispose() {
|
|
164
|
-
|
|
197
|
+
dispose() { }
|
|
198
|
+
_fieldNames() {
|
|
199
|
+
return [...new Set([...Object.keys(this._initialValues), ...Object.keys(this._form)])];
|
|
165
200
|
}
|
|
166
|
-
|
|
201
|
+
_rememberCheckedValue(field) {
|
|
202
|
+
this._checkedValues.get(field)?.cancel?.();
|
|
203
|
+
const checked = { value: $state.snapshot(this._form[field]) };
|
|
204
|
+
this._checkedValues.set(field, checked);
|
|
205
|
+
return checked;
|
|
206
|
+
}
|
|
207
|
+
async _runValidator(field, checked) {
|
|
208
|
+
checked.value = $state.snapshot(this._form[field]);
|
|
167
209
|
this.updateTouched(field, true);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
stringify = (value) => {
|
|
174
|
-
if (typeof value !== 'number' && !value) {
|
|
175
|
-
return '';
|
|
176
|
-
}
|
|
177
|
-
if (typeof value === 'string') {
|
|
178
|
-
return value;
|
|
179
|
-
}
|
|
180
|
-
return JSON.stringify(value);
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
class ValidationUtils {
|
|
184
|
-
static update = (object, path, value) => {
|
|
185
|
-
ValidationUtils.set(object, path, value);
|
|
186
|
-
};
|
|
187
|
-
static getValues = (object) => {
|
|
188
|
-
const results = [];
|
|
189
|
-
for (const [, value] of Object.entries(object)) {
|
|
190
|
-
results.push(value);
|
|
210
|
+
try {
|
|
211
|
+
const error = await this._validator.validateAt(this._form, field);
|
|
212
|
+
if (this._checkedValues.get(field) === checked) {
|
|
213
|
+
this._errors[field] = error || NO_ERROR;
|
|
214
|
+
}
|
|
191
215
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
for (const key in object) {
|
|
197
|
-
copy[key] = value;
|
|
216
|
+
finally {
|
|
217
|
+
if (this._checkedValues.get(field) === checked) {
|
|
218
|
+
this._validatingFields[field] = false;
|
|
219
|
+
}
|
|
198
220
|
}
|
|
199
|
-
return copy;
|
|
200
|
-
}
|
|
201
|
-
static set(object, path, value) {
|
|
202
|
-
object[path] = value;
|
|
203
|
-
return object;
|
|
204
221
|
}
|
|
205
222
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { FormValidationHandler } from './form-validation-handler.svelte';
|
|
1
|
+
export { FormValidationHandler, type ValidateFieldOptions } from './form-validation-handler.svelte';
|
|
2
2
|
export type { FormValidateResult, IFormHandlerValidator } from './types';
|
|
3
3
|
export { StubFormValidator } from './stub-form-validator';
|
|
4
4
|
export { YupFormValidator } from './yup-form-validator';
|
|
@@ -2,6 +2,10 @@ export type IFormHandlerValidator<T> = {
|
|
|
2
2
|
validate: (obj: T) => Promise<FormValidateResult<Partial<T>>>;
|
|
3
3
|
validateAt: (obj: T, field: keyof T) => Promise<string | null>;
|
|
4
4
|
};
|
|
5
|
+
export type CheckedValue = {
|
|
6
|
+
value: unknown;
|
|
7
|
+
cancel?: () => void;
|
|
8
|
+
};
|
|
5
9
|
export type FormValidateResult<T> = {
|
|
6
10
|
isValid: boolean;
|
|
7
11
|
errors: {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CheckedValue } from './types';
|
|
2
|
+
export declare class ValidationHelper {
|
|
3
|
+
static fillKeys<TObject, TValue>(object: TObject, value: TValue): Record<keyof TObject, TValue>;
|
|
4
|
+
static stringify(value: unknown): string;
|
|
5
|
+
static waitUnlessCancelled(checked: CheckedValue, ms: number): Promise<boolean>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export class ValidationHelper {
|
|
2
|
+
static fillKeys(object, value) {
|
|
3
|
+
const copy = {};
|
|
4
|
+
for (const key in object) {
|
|
5
|
+
copy[key] = value;
|
|
6
|
+
}
|
|
7
|
+
return copy;
|
|
8
|
+
}
|
|
9
|
+
static stringify(value) {
|
|
10
|
+
if (typeof value !== 'number' && !value) {
|
|
11
|
+
return '';
|
|
12
|
+
}
|
|
13
|
+
if (typeof value === 'string') {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
return JSON.stringify(value);
|
|
17
|
+
}
|
|
18
|
+
static waitUnlessCancelled(checked, ms) {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
const timeout = window.setTimeout(() => resolve(true), ms);
|
|
21
|
+
checked.cancel = () => {
|
|
22
|
+
window.clearTimeout(timeout);
|
|
23
|
+
resolve(false);
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
<script lang="ts" generics="T extends Record<string, unknown>, K extends keyof T & string">import { Validatable } from '../validatable';
|
|
2
2
|
import { default as FormField } from './cmp.form-field.svelte';
|
|
3
|
-
let { handler, name, label, hint, required = false,
|
|
3
|
+
let { handler, name, label, hint, required = false, debounceMs, disableTouchedTracking, errorPlacement, on, children: field } = $props();
|
|
4
4
|
</script>
|
|
5
5
|
|
|
6
|
-
<Validatable
|
|
7
|
-
handler={handler}
|
|
8
|
-
name={name}
|
|
9
|
-
validateOn={validateOn}
|
|
10
|
-
debounceMs={debounceMs}
|
|
11
|
-
disableTouchedTracking={disableTouchedTracking}
|
|
12
|
-
errorPlacement={errorPlacement}
|
|
13
|
-
on={on}>
|
|
6
|
+
<Validatable handler={handler} name={name} debounceMs={debounceMs} disableTouchedTracking={disableTouchedTracking} errorPlacement={errorPlacement} on={on}>
|
|
14
7
|
{#snippet children(binding, validating)}
|
|
15
8
|
<FormField label={label} hint={hint} required={required}>
|
|
16
9
|
{#snippet children(ids)}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import type { FormValidationHandler } from '../../core/validation';
|
|
2
|
-
import { type ErrorPlacement, type FieldBinding
|
|
2
|
+
import { type ErrorPlacement, type FieldBinding } from '../validatable';
|
|
3
3
|
import { type FormFieldIds } from './cmp.form-field.svelte';
|
|
4
4
|
import type { Snippet } from 'svelte';
|
|
5
5
|
declare function $$render<T extends Record<string, unknown>, K extends keyof T & string>(): {
|
|
6
6
|
props: {
|
|
7
|
-
/** Form validation handler instance. */
|
|
8
7
|
handler: FormValidationHandler<T>;
|
|
9
|
-
/** Field name in the form handler. */
|
|
10
8
|
name: K;
|
|
11
9
|
/** Label rendered above the input. String renders as text; Snippet allows extra content (e.g. tooltip icon) next to the text. */
|
|
12
10
|
label?: string | Snippet;
|
|
@@ -14,11 +12,9 @@ declare function $$render<T extends Record<string, unknown>, K extends keyof T &
|
|
|
14
12
|
hint?: string | Snippet;
|
|
15
13
|
/** Appends a danger-colored `*` to the label. */
|
|
16
14
|
required?: boolean;
|
|
17
|
-
/**
|
|
18
|
-
validateOn?: ValidateOnEvent[];
|
|
19
|
-
/** Debounce validator calls in ms. Forwarded to `<Validatable>`. @default 0 */
|
|
15
|
+
/** Delay before the validator runs after the value changes. Forwarded to `<Validatable>`. */
|
|
20
16
|
debounceMs?: number;
|
|
21
|
-
/** Show errors before the field has been touched. Forwarded to `<Validatable>`.
|
|
17
|
+
/** Show errors before the field has been touched. Forwarded to `<Validatable>`. */
|
|
22
18
|
disableTouchedTracking?: boolean;
|
|
23
19
|
/** Where the error message sits. Forwarded to `<Validatable>`. @default 'overlay' */
|
|
24
20
|
errorPlacement?: ErrorPlacement;
|
|
@@ -5,7 +5,7 @@ export type MultiselectInstance = {
|
|
|
5
5
|
};
|
|
6
6
|
import { type MultiselectBaseProps, type SelectOption } from './types';
|
|
7
7
|
declare function $$render<T>(): {
|
|
8
|
-
props: Omit<MultiselectBaseProps<T>, "value" | "
|
|
8
|
+
props: Omit<MultiselectBaseProps<T>, "value" | "debounceMs" | "on" | "loadOptions" | "groupHeader" | "selectionMode" | "parentSource"> & {
|
|
9
9
|
/** Flat option list. Fuse filters in-memory on every keystroke — no debounce. */
|
|
10
10
|
options: SelectOption<T>[];
|
|
11
11
|
/** Current selection — array of values. Order is preserved (and editable when `reorderable`). */
|
|
@@ -1,33 +1,9 @@
|
|
|
1
|
-
<script lang="ts"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { default as FieldFrame } from './field-frame.svelte';
|
|
6
|
-
let { handler, name, validateOn = ['input', 'change', 'blur'], debounceMs = 0, disableTouchedTracking = false, errorPlacement = 'overlay', on, children } = $props();
|
|
7
|
-
// True between a value change and the next completed validation. Hides the stale error in the
|
|
8
|
-
// UI without mutating handler.errors — the model keeps the last known result, the view just
|
|
9
|
-
// doesn't trust it until the validator has caught up.
|
|
10
|
-
let pendingValidation = $state(false);
|
|
11
|
-
// Monotonic counter: each value change and each validation start bumps it. Completing
|
|
12
|
-
// validation clears `pendingValidation` only when the token still matches the latest bump —
|
|
13
|
-
// this discards results from validations that were superseded by further input.
|
|
14
|
-
let activeToken = 0;
|
|
15
|
-
const runValidate = async () => {
|
|
16
|
-
const myToken = ++activeToken;
|
|
17
|
-
try {
|
|
18
|
-
await handler.validateField(name);
|
|
19
|
-
}
|
|
20
|
-
finally {
|
|
21
|
-
// A rejecting validator would otherwise strand pendingValidation, and the children snippet reads it as a never-ending `validating`.
|
|
22
|
-
if (myToken === activeToken) {
|
|
23
|
-
pendingValidation = false;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
const validate = $derived(debounceMs > 0 ? Utils.debounce(runValidate, debounceMs) : runValidate);
|
|
28
|
-
const runs = (mode) => validateOn.includes(mode);
|
|
29
|
-
const showErrors = $derived(!pendingValidation && !!handler.errors[name] && (disableTouchedTracking || !!handler.touched[name]));
|
|
1
|
+
<script lang="ts" generics="T extends Record<string, unknown>, K extends keyof T & string">import { default as FieldFrame } from './field-frame.svelte';
|
|
2
|
+
let { handler, name, debounceMs = 0, disableTouchedTracking = false, errorPlacement = 'overlay', on, children } = $props();
|
|
3
|
+
const validating = $derived(handler.isFieldValidating(name));
|
|
4
|
+
const showErrors = $derived(!validating && !!handler.errors[name] && (disableTouchedTracking || !!handler.touched[name]));
|
|
30
5
|
const messageId = $props.id();
|
|
6
|
+
const validate = () => void handler.validateField(name, { debounceMs, onlyIfChanged: true });
|
|
31
7
|
const field = $derived({
|
|
32
8
|
name,
|
|
33
9
|
value: handler.form[name],
|
|
@@ -36,30 +12,17 @@ const field = $derived({
|
|
|
36
12
|
on: {
|
|
37
13
|
input: (v) => {
|
|
38
14
|
handler.updateField(name, v);
|
|
39
|
-
|
|
40
|
-
pendingValidation = true;
|
|
41
|
-
if (runs('input')) {
|
|
42
|
-
validate();
|
|
43
|
-
}
|
|
15
|
+
validate();
|
|
44
16
|
on?.input?.(v);
|
|
45
17
|
},
|
|
46
18
|
change: (v) => {
|
|
47
19
|
handler.updateField(name, v);
|
|
48
|
-
|
|
49
|
-
pendingValidation = true;
|
|
50
|
-
if (runs('change')) {
|
|
51
|
-
validate();
|
|
52
|
-
}
|
|
20
|
+
validate();
|
|
53
21
|
on?.change?.(v);
|
|
54
22
|
},
|
|
55
23
|
blur: () => {
|
|
56
24
|
handler.updateTouched(name, true);
|
|
57
|
-
|
|
58
|
-
validate();
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
pendingValidation = false;
|
|
62
|
-
}
|
|
25
|
+
validate();
|
|
63
26
|
on?.blur?.();
|
|
64
27
|
}
|
|
65
28
|
}
|
|
@@ -67,7 +30,7 @@ const field = $derived({
|
|
|
67
30
|
</script>
|
|
68
31
|
|
|
69
32
|
<FieldFrame error={showErrors ? handler.errors[name] : null} errorPlacement={errorPlacement} messageId={messageId}>
|
|
70
|
-
{@render children(field,
|
|
33
|
+
{@render children(field, validating)}
|
|
71
34
|
</FieldFrame>
|
|
72
35
|
|
|
73
36
|
<!--
|
|
@@ -1,37 +1,13 @@
|
|
|
1
|
-
export type ValidateOnEvent = 'input' | 'change' | 'blur';
|
|
2
|
-
export type FieldBinding<V> = {
|
|
3
|
-
name: string;
|
|
4
|
-
value: V;
|
|
5
|
-
error: boolean;
|
|
6
|
-
/** Id of the visible error message, set only while the error is shown. */
|
|
7
|
-
'aria-describedby'?: string;
|
|
8
|
-
on: {
|
|
9
|
-
input: (v: V) => void;
|
|
10
|
-
change: (v: V) => void;
|
|
11
|
-
blur: () => void;
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
1
|
import type { FormValidationHandler } from '../../core/validation';
|
|
15
|
-
import type { ErrorPlacement } from './types';
|
|
2
|
+
import type { ErrorPlacement, FieldBinding } from './types';
|
|
16
3
|
import type { Snippet } from 'svelte';
|
|
17
4
|
declare function $$render<T extends Record<string, unknown>, K extends keyof T & string>(): {
|
|
18
5
|
props: {
|
|
19
|
-
/** Form validation handler instance. */
|
|
20
6
|
handler: FormValidationHandler<T>;
|
|
21
|
-
/** Field name in the form handler. */
|
|
22
7
|
name: K;
|
|
23
|
-
/**
|
|
24
|
-
* Which events trigger validation. Value is always updated synchronously on `input`/`change`
|
|
25
|
-
* regardless of this option — it only controls when the validator runs.
|
|
26
|
-
* @default ['input', 'change', 'blur']
|
|
27
|
-
*/
|
|
28
|
-
validateOn?: ValidateOnEvent[];
|
|
29
|
-
/**
|
|
30
|
-
* Debounce validator calls in ms. Value update stays sync; only validation is delayed.
|
|
31
|
-
* @default 0
|
|
32
|
-
*/
|
|
8
|
+
/** Delay before the validator runs after the value changes; the value itself updates at once. */
|
|
33
9
|
debounceMs?: number;
|
|
34
|
-
/** Show errors even when the field has not been touched.
|
|
10
|
+
/** Show errors even when the field has not been touched. */
|
|
35
11
|
disableTouchedTracking?: boolean;
|
|
36
12
|
/**
|
|
37
13
|
* Where the error message sits: `'overlay'` covers whatever is below the field, `'reserved'` keeps a
|
|
@@ -51,8 +27,7 @@ declare function $$render<T extends Record<string, unknown>, K extends keyof T &
|
|
|
51
27
|
/**
|
|
52
28
|
* Receives a wired `FieldBinding<T[K]>` to spread into a kit input (`aria-describedby` points at the visible error message; inputs without that
|
|
53
29
|
* prop ignore it; a control whose value prop is not `value`, e.g. `Checkbox`, is wired by hand), plus a `validating` flag — true from the value
|
|
54
|
-
* change until the validator has caught up, debounce window included
|
|
55
|
-
* excludes `input` and `change` the flag stays true for the whole typing session.
|
|
30
|
+
* change until the validator has caught up, debounce window included.
|
|
56
31
|
*/
|
|
57
32
|
children: Snippet<[FieldBinding<T[K]>, boolean]>;
|
|
58
33
|
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
export { default as Validatable } from './cmp.validatable.svelte';
|
|
2
|
-
export type { FieldBinding, ValidateOnEvent } from './cmp.validatable.svelte';
|
|
3
2
|
export { default as ValidationError } from './cmp.validation-error.svelte';
|
|
4
|
-
export type { ErrorPlacement } from './types';
|
|
3
|
+
export type { ErrorPlacement, FieldBinding } from './types';
|
|
@@ -5,3 +5,15 @@
|
|
|
5
5
|
* - `'inline'` — in normal flow, only while an error is shown: the field grows on error and shrinks back when it clears.
|
|
6
6
|
*/
|
|
7
7
|
export type ErrorPlacement = 'overlay' | 'reserved' | 'inline';
|
|
8
|
+
export type FieldBinding<V> = {
|
|
9
|
+
name: string;
|
|
10
|
+
value: V;
|
|
11
|
+
error: boolean;
|
|
12
|
+
/** Id of the visible error message, set only while the error is shown. */
|
|
13
|
+
'aria-describedby'?: string;
|
|
14
|
+
on: {
|
|
15
|
+
input: (v: V) => void;
|
|
16
|
+
change: (v: V) => void;
|
|
17
|
+
blur: () => void;
|
|
18
|
+
};
|
|
19
|
+
};
|